Class: Ability
- Inherits:
-
Object
- Object
- Ability
- Defined in:
- lib/ability.rb
Overview
Ability struct that should house the ability definitions.
Instance Attribute Summary collapse
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Class Method Summary collapse
-
.all ⇒ Object
Get all abilities from all definitions.
-
.all_codes(_query_string = nil) ⇒ Object
Get all ability codes from all definitions.
-
.match(expression) ⇒ Object
Match abilities based on a matching string or regex.
-
.policy_path ⇒ Object
Path to the policy folder.
-
.where(*queries) ⇒ Object
Filter abilities based on namespace.
Instance Method Summary collapse
-
#initialize(code:, name:, namespace: nil, description: '', actions: []) ⇒ Ability
constructor
Constructor for Ability object code - The code of the ability (see definitions.rb) name - The name of the ability (see definitions.rb) namespace - The namespace of the ability.
Constructor Details
#initialize(code:, name:, namespace: nil, description: '', actions: []) ⇒ Ability
Constructor for Ability object code - The code of the ability (see definitions.rb) name - The name of the ability (see definitions.rb) namespace - The namespace of the ability. You don’t need to worry about this. description - The description of the ability (see definitions.rb) actions - Controller actions that the ability can check against (see definitions.rb)
13 14 15 16 17 18 19 |
# File 'lib/ability.rb', line 13 def initialize(code:, name:, namespace: nil, description: '', actions: []) @code = code @name = name @description = description @actions = actions @namespace = namespace end |
Instance Attribute Details
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
5 6 7 |
# File 'lib/ability.rb', line 5 def actions @actions end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
5 6 7 |
# File 'lib/ability.rb', line 5 def code @code end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
5 6 7 |
# File 'lib/ability.rb', line 5 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/ability.rb', line 5 def name @name end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
5 6 7 |
# File 'lib/ability.rb', line 5 def namespace @namespace end |
Class Method Details
.all ⇒ Object
Get all abilities from all definitions.
25 26 27 |
# File 'lib/ability.rb', line 25 def all definitions.map { |ability| instantiate(**ability) } end |
.all_codes(_query_string = nil) ⇒ Object
Get all ability codes from all definitions. Unused args. Need to allow filtering through namespace.
31 32 33 |
# File 'lib/ability.rb', line 31 def all_codes(_query_string = nil) definitions.pluck(:code) end |
.match(expression) ⇒ Object
Match abilities based on a matching string or regex. The matcher is based on the namespace.
55 56 57 58 59 60 |
# File 'lib/ability.rb', line 55 def match(expression) case expression.class.to_s when 'String' then all.select { |ability| ability.namespace.to_s.match?(/#{trim(expression).camelize}/) } when 'Regexp' then regex_matcher(expression) end end |
.policy_path ⇒ Object
Path to the policy folder.
68 69 70 |
# File 'lib/ability.rb', line 68 def policy_path @policy_path ||= Rails.root.join('app/policies') end |
.where(*queries) ⇒ Object
Filter abilities based on namespace.
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ability.rb', line 36 def where(*queries) results = [] queries.each do |query| case query.class.to_s when 'String' results += all.select { |ability| ability.namespace.to_s == "Policies::#{trim(query).camelize}" } when 'Module', 'Class' results += all.select { |ability| ability.namespace == query } end end results end |