Sha256: bc5f556bfcb91572e23b2b03df557a594811d1b587d1ba048558530094d7d0ec

Contents?: true

Size: 1.17 KB

Versions: 20

Compression:

Stored size: 1.17 KB

Contents

# Custom Lookup Chain

Action Policy's lookup chain is just an array of _probes_ (lambdas with a specific interface).

The lookup process itself is pretty simple:
- Call the first probe;
- Return the result if it is not `nil`;
- Go to the next probe.

You can override the default chain with your own. For example:

```ruby
ActionPolicy::LookupChain.chain = [
  # Probe accepts record as the first argument
  # and arbitrary options (passed to `authorize!` / `allowed_to?` call)
  lambda do |record, **options|
    # your custom lookup logic
  end
]
```

## NullPolicy example

Let's consider a simple example of extending the existing lookup chain with one more probe.

Suppose that we want to have a fallback policy (policy used when none found for the resource) instead of raising an `ActionPolicy::NotFound` error.

Let's call this policy a `NullPolicy`:

```ruby
class NullPolicy < ActionPolicy::Base
  default_rule :any?

  def any?
    false
  end
end
```

Here we use the [default rule](aliases.md#default-rule) to handle any rule applied.

Now we need to add a simple probe to the end of our lookup chain:

```ruby
ActionPolicy::LookupChain.chain << ->(_, _) { NullPolicy }
```

That's it!

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
action_policy-0.4.4 docs/custom_lookup_chain.md
action_policy-0.4.3 docs/custom_lookup_chain.md
action_policy-0.4.2 docs/custom_lookup_chain.md
action_policy-0.4.1 docs/custom_lookup_chain.md
action_policy-0.4.0 docs/custom_lookup_chain.md
action_policy-0.3.4 docs/custom_lookup_chain.md
action_policy-0.3.3 docs/custom_lookup_chain.md
action_policy-0.3.2 docs/custom_lookup_chain.md
action_policy-0.3.1 docs/custom_lookup_chain.md
action_policy-0.3.0 docs/custom_lookup_chain.md
action_policy-0.3.0.beta1 docs/custom_lookup_chain.md
action_policy-0.2.4 docs/custom_lookup_chain.md
action_policy-0.2.3 docs/custom_lookup_chain.md
action_policy-0.2.2 docs/custom_lookup_chain.md
action_policy-0.2.0 docs/custom_lookup_chain.md
action_policy-0.1.4 docs/custom_lookup_chain.md
action_policy-0.1.3 docs/custom_lookup_chain.md
action_policy-0.1.2 docs/custom_lookup_chain.md
action_policy-0.1.1 docs/custom_lookup_chain.md
action_policy-0.1.0 docs/custom_lookup_chain.md