Skip to content

Commit 4445abc

Browse files
Merge pull request #2430 from coreinfrastructure/more_docs
Improve model docs, add utility method
2 parents 8e45214 + e1364b8 commit 4445abc

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

app/models/additional_right.rb

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,52 @@
11
# frozen_string_literal: true
22

3-
# Model class for additional right data management.
4-
#
3+
# Copyright 2015-2017, the Linux Foundation, IDA, and the
4+
# OpenSSF Best Practices badge contributors
5+
# SPDX-License-Identifier: MIT
56

7+
# Model class for additional rights data management.
8+
#
9+
# This model represents the many-to-many relationship between users and
10+
# projects for granting additional editing permissions beyond project
11+
# ownership. It serves as a join table that allows multiple users to have
12+
# edit rights on a single project, and allows users to have edit rights
13+
# on multiple projects they don't own.
14+
#
15+
# Currently mere *presence* is what matters; it gives the user edit rights
16+
# to that project. In the future this *could* have 1+ additional fields
17+
# identifying the specific additional rights of a user over a project.
18+
#
19+
# Database schema:
20+
# - project_id: Foreign key to projects table
21+
# - user_id: Foreign key to users table
22+
# - created_at: When the right was granted
23+
# - updated_at: When the record was last modified
24+
#
25+
# Indexes:
26+
# - Unique index on [user_id, project_id] prevents duplicate rights
27+
# - Individual indexes on project_id and user_id for query performance
628
class AdditionalRight < ApplicationRecord
7-
# List additional rights of users for a given project.
8-
# This is a simple associative table (between project and user).
9-
# Currently mere *presence* is what matters; it gives the user edit rights
10-
# to that project. In the future this *could* have 1+ additional fields
11-
# identifying the specific additional rights of a user over a project.
29+
# Associates this additional right with a specific project.
30+
# When the project is destroyed, all associated additional rights are
31+
# deleted.
32+
# @return [Project] the project this right applies to
1233
belongs_to :project
34+
35+
# Associates this additional right with a specific user.
36+
# When the user is destroyed, all their additional rights are deleted.
37+
# @return [User] the user who has this additional right
1338
belongs_to :user
39+
40+
# Validates that both project and user exist and are valid.
41+
# NOTE: These explicit validations are required because this application
42+
# sets belongs_to_required_by_default = false in its configuration.
43+
validates :project, presence: true
44+
validates :user, presence: true
45+
46+
# Finds all additional rights for a specific project.
47+
# This scope can be used instead of AdditionalRight.where(project_id: id)
48+
# for better readability and consistency.
49+
# @param project_id [Integer] the ID of the project
50+
# @return [ActiveRecord::Relation<AdditionalRight>] rights for project
51+
scope :for_project, ->(project_id) { where(project_id: project_id) }
1452
end

app/models/application_record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
# Copyright 2015-2017, the Linux Foundation, IDA, and the
3+
# Copyright the Linux Foundation, IDA, and the
44
# OpenSSF Best Practices badge contributors
55
# SPDX-License-Identifier: MIT
66

app/models/project.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class Project < ApplicationRecord
231231
# @return [String] sorted array of user IDs as string
232232
def additional_rights_to_s
233233
# "distinct" shouldn't be needed; it's purely defensive here
234-
list = AdditionalRight.where(project_id: id).distinct.pluck(:user_id)
234+
list = AdditionalRight.for_project(id).distinct.pluck(:user_id)
235235
list.sort.to_s # Use list.sort.to_s[1..-2] to remove surrounding [ and ]
236236
end
237237

test/controllers/projects_controller_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def only_correct_criteria_selectable(level)
352352
user_id: users(:test_user_mark).id,
353353
project_id: @project.id
354354
).save!
355-
assert_equal 2, AdditionalRight.where(project_id: @project.id).count
355+
assert_equal 2, AdditionalRight.for_project(@project.id).count
356356
log_in_as(@project.user)
357357
# Run patch (the point of the test), which invokes the 'update' method
358358
patch "/en/projects/#{@project.id}", params: {
@@ -362,7 +362,7 @@ def only_correct_criteria_selectable(level)
362362
}
363363
# TODO: Weird http/https discrepancy in test
364364
# assert_redirected_to project_path(@project, locale: :en)
365-
assert_equal 0, AdditionalRight.where(project_id: @project.id).count
365+
assert_equal 0, AdditionalRight.for_project(@project.id).count
366366
end
367367

368368
# Negative test
@@ -375,7 +375,7 @@ def only_correct_criteria_selectable(level)
375375
user_id: users(:test_user_mark).id,
376376
project_id: @project.id
377377
).save!
378-
assert_equal 2, AdditionalRight.where(project_id: @project.id).count
378+
assert_equal 2, AdditionalRight.for_project(@project.id).count
379379
log_in_as(users(:test_user_melissa))
380380
# Run patch (the point of the test), which invokes the 'update' method
381381
patch "/en/projects/#{@project.id}", params: {
@@ -386,7 +386,7 @@ def only_correct_criteria_selectable(level)
386386
# tries to remove an "additional rights" user, we just ignore it.
387387
# If we add an error report, we should check for that error report here.
388388
assert_redirected_to root_path(locale: 'en')
389-
assert_equal 2, AdditionalRight.where(project_id: @project.id).count
389+
assert_equal 2, AdditionalRight.for_project(@project.id).count
390390
end
391391

392392
# Negative test

0 commit comments

Comments
 (0)