Skip to content

Commit

Permalink
Improve readability of role access list
Browse files Browse the repository at this point in the history
  • Loading branch information
martinemde committed Sep 22, 2024
1 parent 3374305 commit c513739
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/ownership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Ownership < ApplicationRecord

enum :role, { owner: Access::OWNER, maintainer: Access::MAINTAINER }, validate: true, _default: :owner

scope :user_with_minimum_role, ->(user, role) { where(user: user, role: Access.flag_for_role(role)..) }
scope :user_with_minimum_role, ->(user, role) { where(user: user, role: Access.with_minimum_role(role)) }

def self.by_indexed_gem_name
select("ownerships.*, rubygems.name")
Expand Down
4 changes: 4 additions & 0 deletions lib/access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def self.flag_for_role(role)
ROLES.fetch(role)
end

def self.with_minimum_role(role)
Range.new(flag_for_role(role), nil)
end

def self.role_for_flag(flag)
ROLES.key(flag)&.inquiry.tap do |role|
raise ArgumentError, "Unknown role flag: #{flag}" if role.blank?
Expand Down
9 changes: 9 additions & 0 deletions test/lib/access_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ class AccessTest < ActiveSupport::TestCase
end
end
end

context ".with_minimum_role" do
should "return the range of roles for a given permission flag" do
assert_equal Range.new(Access::OWNER, nil), Access.with_minimum_role("owner")
refute_includes Access.with_minimum_role("owner"), Access::MAINTAINER
assert_includes Access.with_minimum_role("owner"), Access::OWNER
assert_includes Access.with_minimum_role("owner"), Access::ADMIN
end
end
end

0 comments on commit c513739

Please sign in to comment.