Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Policy lookup does not work with SimpleDelegator #73

Closed
pcriv opened this issue Jun 17, 2019 · 2 comments
Closed

Policy lookup does not work with SimpleDelegator #73

pcriv opened this issue Jun 17, 2019 · 2 comments

Comments

@pcriv
Copy link

pcriv commented Jun 17, 2019

Tell us about your environment

Ruby Version:

2.6.3

Framework Version (Rails, whatever):

None.

Action Policy Version:

0.3.1

What did you do?

Used SimpleDelegator to decorate an object and send it to the policy.

What did you expect to happen?

Expect the correct policy to be resolved.

What actually happened?

~/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/action_policy-0.3.1/lib/action_policy.rb:32:in `lookup': Couldn't find policy class for #<Post:0x00007fe5234431c0> (ActionPolicy::NotFound)

How to reproduce?

#!/usr/bin/env ruby

require "bundler/inline"

gemfile true do
  source "https://rubygems.org"

  gem "action_policy"
end

require "action_policy"

class User
end

class Post
end

class PostPolicy < ActionPolicy::Base
  def update?
    true
  end
end

class PostDecorator < SimpleDelegator
end

class PostUpdateAction
  include ActionPolicy::Behaviour

  # provide authorization subject (performer)
  authorize :user

  attr_reader :user

  def initialize(user)
    @user = user
  end

  def call(post)
    authorize! post, to: :update?
  end
end

user = User.new
post = Post.new

puts PostUpdateAction.new(user).call(post)

post_decorator = PostDecorator.new(post)

puts PostUpdateAction.new(user).call(post_decorator)
@palkan palkan added enhancement New feature or request and removed enhancement New feature or request labels Jun 17, 2019
@palkan
Copy link
Owner

palkan commented Jun 20, 2019

Hey @pablocrivella,

This is not the expected behaviour: policy lookup relies on the object class (it's name or .policy_name / .policy_class methods, see docs).

In your case, the class of the object is PostDecorator (not Post) and we can only infer from it the PostDecoratorPolicy, which doesn't exist—and we fail.

We (from the library perspective) cannot know beforehand, whether you wan to use PostDecoratorPolicy or PostPolicy, that should be decide by the developer.

One workaround is to pass the underlying object to the lookup chain instead of a decorator. See, for example, the Draper use-case: https://actionpolicy.evilmartians.io/#/./decorators.

Another workaround is to specify the policy name explicitly:

class PostDecorator < SimpleDelegator
  def self.policy_name
    "PostPolicy"
  end
end

@pcriv
Copy link
Author

pcriv commented Jun 21, 2019

Understood! Thanks for your explanation @palkan :)

@pcriv pcriv closed this as completed Jun 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants