Sha256: de15790e962413e7c1f20b5e3a048dbcb4afc5ba0d64bcf04b9726fcec868c7f

Contents?: true

Size: 907 Bytes

Versions: 8

Compression:

Stored size: 907 Bytes

Contents

# Dealing with Decorators

Ref: [action_policy#7](https://github.com/palkan/action_policy/issues/7).

Since Action Policy [lookup mechanism](./lookup_chain.md) relies on the target
record's class properties (names, methods) it could break when using with _decorators_.

To make `authorize!` and other [behaviour](./behaviour.md) methods work seamlessly with decorated
objects, you might want to _enhance_ the `policy_for` method.

For example, when using the [Draper](https://github.com/drapergem/draper) gem:

```ruby
module ActionPolicy
  module Draper
    def policy_for(record:, **opts)
      # From https://github.com/GoodMeasuresLLC/draper-cancancan/blob/master/lib/draper/cancancan.rb
      record = record.model while record.is_a?(::Draper::Decorator)
      super(record: record, **opts)
    end
  end
end

class ApplicationController < ActionController::Base
  prepend ActionPolicy::Draper
end
```

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
action_policy-0.4.4 docs/decorators.md
action_policy-0.4.3 docs/decorators.md
action_policy-0.4.2 docs/decorators.md
action_policy-0.4.1 docs/decorators.md
action_policy-0.4.0 docs/decorators.md
action_policy-0.3.4 docs/decorators.md
action_policy-0.3.3 docs/decorators.md
action_policy-0.3.2 docs/decorators.md