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

best way to authorize noncrud actions with pundit? #100

Closed
dryaf opened this issue Jan 30, 2014 · 2 comments
Closed

best way to authorize noncrud actions with pundit? #100

dryaf opened this issue Jan 30, 2014 · 2 comments

Comments

@dryaf
Copy link

dryaf commented Jan 30, 2014

# policy
class ContactPolicy
  attr_reader :user, :contact

  def initialize(user, contact)
    @user = user
    @contact = contact
  end

  def number?
    true
  end

# controller

    class ContactsController < ApplicationController

      before_filter :load_object, :only => [:show, :edit, :update, :destroy]

      def number
        num = {number: Contacts.count}
        authorize num  # would look for the HashPolicy not in ContactPolicy
        render json: num
      end

should i just do ?

authorize Contact

@thomasklemm
Copy link
Collaborator

class ContactPolicy
  attr_reader :user, :contact

  def initialize(user, contact)
    @user = user
    @contact = contact
  end

  def count?
    true
  end
end
class ContactsController < ApplicationController
  before_filter :load_object, :only => [:show, :edit, :update, :destroy]

  def number
    num = {number: Contact.count}
    authorize Contact, :count?  # give this a try
    render json: num
  end
end

@dryaf
Copy link
Author

dryaf commented Jan 30, 2014

sweet. thx

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