Module: ActionPolicy::Policy::Core

Includes:
Behaviours::PolicyFor
Included in:
Base
Defined in:
lib/action_policy/policy/core.rb

Overview

Core policy API

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Behaviours::PolicyFor

#authorization_context, #authorization_namespace, #policy_for

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



60
61
62
# File 'lib/action_policy/policy/core.rb', line 60

def record
  @record
end

#resultObject (readonly)

Returns the value of attribute result.



60
61
62
# File 'lib/action_policy/policy/core.rb', line 60

def result
  @result
end

Class Method Details

.included(base) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/action_policy/policy/core.rb', line 28

def included(base)
  base.extend ClassMethods

  # Generate a new class for each _policy chain_
  # in order to extend it independently
  base.module_eval do
    @result_class = Class.new(ExecutionResult)

    # we need to make this class _named_,
    # 'cause anonymous classes couldn't be marshalled
    base.const_set(:APR, @result_class)
  end
end

Instance Method Details

#__apply__(rule) ⇒ Object

This method performs the rule call. Override or extend it to provide custom functionality (such as caching, pre checks, etc.)



77
78
79
# File 'lib/action_policy/policy/core.rb', line 77

def __apply__(rule)
  public_send(rule)
end

#allowed_to?(rule, record = :__undef__, **options) ⇒ Boolean

Returns a result of applying the specified rule to the specified record. Under the hood a policy class for record is resolved (unless it’s explicitly set through ‘with` option).

If record is ‘nil` then we uses the current policy.

Returns:

  • (Boolean)


95
96
97
98
99
100
101
# File 'lib/action_policy/policy/core.rb', line 95

def allowed_to?(rule, record = :__undef__, **options)
  if record == :__undef__
    __apply__(rule)
  else
    policy_for(record: record, **options).apply(rule)
  end
end

#apply(rule) ⇒ Object

Returns a result of applying the specified rule (true of false). Unlike simply calling a predicate rule (‘policy.manage?`), `apply` also calls pre-checks.



69
70
71
72
# File 'lib/action_policy/policy/core.rb', line 69

def apply(rule)
  @result = self.class.result_class.new
  @result.load __apply__(rule)
end

#initialize(record = nil) ⇒ Object



62
63
64
# File 'lib/action_policy/policy/core.rb', line 62

def initialize(record = nil)
  @record = record
end

#resolve_rule(activity) ⇒ Object

Returns a rule name (policy method name) for activity.

By default, rule name is equal to activity name.

Raises ActionPolicy::UknownRule when rule is not found in policy.

Raises:



108
109
110
111
112
# File 'lib/action_policy/policy/core.rb', line 108

def resolve_rule(activity)
  raise UnknownRule.new(self, activity) unless
    respond_to?(activity)
  activity
end

#with_clean_resultObject

Wrap code that could modify result to prevent the current result modification



83
84
85
86
87
88
# File 'lib/action_policy/policy/core.rb', line 83

def with_clean_result
  was_result = @result
  res = yield
  @result = was_result
  res
end