Class: Dry::Ability::Rule
- Inherits:
-
Object
- Object
- Dry::Ability::Rule
- Includes:
- RuleInterface
- Defined in:
- lib/dry/ability/rule.rb
Defined Under Namespace
Classes: Or
Instance Method Summary collapse
- #accessible? ⇒ Boolean
- #attributes_for(account, object) ⇒ Object
- #call(account, object) ⇒ Object
- #filter? ⇒ Boolean
- #register_to(_rules) ⇒ Object
- #scope? ⇒ Boolean
- #scope_for(account, subject) ⇒ Object
- #|(other) ⇒ Object
Methods included from RuleInterface
Instance Method Details
#accessible? ⇒ Boolean
55 56 57 |
# File 'lib/dry/ability/rule.rb', line 55 def accessible? scope? || !@explicit_scope end |
#attributes_for(account, object) ⇒ Object
30 31 32 |
# File 'lib/dry/ability/rule.rb', line 30 def attributes_for(account, object) @constraints.blank? ? {} : F[:eval_values, [account, object]][@constraints] end |
#call(account, object) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/dry/ability/rule.rb', line 22 def call(account, object) if filter? filter.(account, object) else @constraints.blank? || run_constraints(account, object, @constraints) end ^ @inverse end |
#filter? ⇒ Boolean
47 48 49 |
# File 'lib/dry/ability/rule.rb', line 47 def filter? !@filter.nil? end |
#register_to(_rules) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dry/ability/rule.rb', line 59 def register_to(_rules) unless defined?(@_registered) @subjects.each do |subject| _rules.namespace(subject) do |_subject| @actions.each do |action| key = _subject.send(:namespaced, action) pred = _rules._container.delete(key)&.call rules_or = pred | self if pred _subject.register action, (rules_or || self) end end end @_registered = true end end |
#scope? ⇒ Boolean
51 52 53 |
# File 'lib/dry/ability/rule.rb', line 51 def scope? !@scope.nil? end |
#scope_for(account, subject) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/dry/ability/rule.rb', line 34 def scope_for(account, subject) relation = if scope? @scope.arity > 1 ? @scope[account, subject] : @scope[account] else T::Queriable[subject].all end unless @explicit_scope attrs = attributes_for(account, subject) relation = relation.where(attrs) unless attrs.blank? end relation end |
#|(other) ⇒ Object
75 76 77 |
# File 'lib/dry/ability/rule.rb', line 75 def |(other) Or.new([self, other]) end |