Class: Dry::Ability::Rule

Inherits:
Object
  • Object
show all
Includes:
RuleInterface
Defined in:
lib/dry/ability/rule.rb

Defined Under Namespace

Classes: Or

Instance Method Summary collapse

Methods included from RuleInterface

#[]

Instance Method Details

#accessible?Boolean

Returns:

  • (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(, object)
  @constraints.blank? ? {} : F[:eval_values, [, object]][@constraints]
end

#call(account, object) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/dry/ability/rule.rb', line 22

def call(, object)
  if filter?
    filter.(, object)
  else
    @constraints.blank? || run_constraints(, object, @constraints)
  end ^ @inverse
end

#filter?Boolean

Returns:

  • (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

Returns:

  • (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(, subject)
  relation = if scope?
    @scope.arity > 1 ? @scope[, subject] : @scope[]
  else
    T::Queriable[subject].all
  end
  unless @explicit_scope
    attrs = attributes_for(, 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