Module: ActionPolicy::LookupChain

Defined in:
lib/action_policy/lookup_chain.rb

Overview

LookupChain contains resolvers to determine a policy for a record (with additional options).

You can modify the ‘LookupChain.chain` (for example, to add custom resolvers).

Defined Under Namespace

Classes: NamespaceCache

Constant Summary collapse

INSTANCE_POLICY_CLASS =

By self ‘policy_class` method

->(record, **) {
  record.policy_class if record.respond_to?(:policy_class)
}
CLASS_POLICY_CLASS =

By record’s class ‘policy_class` method

->(record, **) {
  record.class.policy_class if record.class.respond_to?(:policy_class)
}
INFER_FROM_CLASS =

Infer from class name

->(record, namespace: nil, strict_namespace: false, **) {
  policy_name = policy_class_name_for(record)
  lookup_within_namespace(policy_name, namespace, strict: strict_namespace)
}
SYMBOL_LOOKUP =

Infer from passed symbol

->(record, namespace: nil, strict_namespace: false, **) {
  next unless record.is_a?(Symbol)

  policy_name = "#{record.camelize}Policy"
  lookup_within_namespace(policy_name, namespace, strict: strict_namespace)
}
CLASSIFY_SYMBOL_LOOKUP =

(Optional) Infer using String#classify if available

->(record, namespace: nil, strict_namespace: false, **) {
  next unless record.is_a?(Symbol)

  policy_name = "#{record.to_s.classify}Policy"
  lookup_within_namespace(policy_name, namespace, strict: strict_namespace)
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.chainObject

Returns the value of attribute chain.



53
54
55
# File 'lib/action_policy/lookup_chain.rb', line 53

def chain
  @chain
end

.namespace_cache_enabledObject Also known as: namespace_cache_enabled?

Returns the value of attribute namespace_cache_enabled.



53
54
55
# File 'lib/action_policy/lookup_chain.rb', line 53

def namespace_cache_enabled
  @namespace_cache_enabled
end

Class Method Details

.call(record, **opts) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/action_policy/lookup_chain.rb', line 57

def call(record, **opts)
  chain.each do |probe|
    val = probe.call(record, **opts)
    return val unless val.nil?
  end
  nil
end