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

[question]Can I call other policy's method? #93

Closed
liubin opened this issue Jan 21, 2014 · 2 comments
Closed

[question]Can I call other policy's method? #93

liubin opened this issue Jan 21, 2014 · 2 comments

Comments

@liubin
Copy link

liubin commented Jan 21, 2014

Hi

for example , I have two models with the has_many relations:

Goods has_many Orders

I want to ensure only the Goods' owner can modify the orders.
I created two policy classes

class GoodsPolicy
   def admin?
     # some approach to judge weather the user is an admin and can modify the goods.
   end
end

class OrderPolicy
    def edit?
      authorize @order.goods, :admin?
    end
end

To use the authorize method in a policy class, I must add this to the OrderPolicy class:

  include Pundit

  def current_user
    @user
  end

My question is that this is an right usage of Pundit? Or there is an more ideal approach?

BR.

@deefour
Copy link
Contributor

deefour commented Jan 21, 2014

There are others here better informed on the best practice for what you're trying to do, but there's definitely no need to call authorize from within the policy.

class OrderPolicy
  def edit?
    GoodsPolicy.new(@user, @order.goods).admin?
  end
end

@liubin
Copy link
Author

liubin commented Jan 22, 2014

@deefour Thank you, that great!

@liubin liubin closed this as completed Jan 22, 2014
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