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

One policy for all #495

Closed
Sahar-Remodeling opened this issue Nov 15, 2017 · 2 comments
Closed

One policy for all #495

Sahar-Remodeling opened this issue Nov 15, 2017 · 2 comments

Comments

@Sahar-Remodeling
Copy link

I have large number of controllers/module and I need one policy (applicationPolicy) to handle their common actions. Do I need to create a policy class for each controller (record)?

@wayne5540
Copy link

wayne5540 commented Nov 20, 2017

You can simply override policy(record)

ex:

class ApplicationController < ActionController::Base
  include Pundit
  # ...
  private

  def policy(record)
    policies[record] ||= ApplicationPolicy.new(pundit_user, record)
  end
end

@tom-lord
Copy link

...But in general, I would recommend creating blank policies for each model.

This makes it clear to other developers that there is no special behaviour (without having to unpick how you've over-ridden the policy class). It also makes the code easy/obvious how to change in future, when special rules do need to apply for a model.

In addition, note that blank policy classes will be auto-generated when you create a new model via rails generate.

@ingemar ingemar closed this as completed Feb 13, 2018
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

4 participants