Search code examples
ruby-on-railsactiveadminpundit

Pundit authorization on activeadmin custom page


In a Rails 4 app with activeadmin gem (current master branch) I use Pundit for authorization. It works well for ressources but I don't manage to make it work for pages.

Given for example :

ActiveAdmin.register_page "Home" do
    content do
        para "some text"
    end
end

How would I authorize it for specific user ?

By reading the Pundit readme I tried with the following code but it does not work

class HomePolicy < Struct.new(:user, :home)
  def index?
    true
  end

  def show?
    true
  end
end

Any idea ?


Solution

  • Here is an example policy that I'm using for the dashboard. I have placed it under policies/active_admin/page_policy.rb. Maybe this might be of some help

    class ActiveAdmin::PagePolicy
      attr_reader :user, :record
    
      def initialize(user, record)
        @user = user
        @record = record
      end
    
      def show?
        case record.name
        when 'Dashboard'
          true
        else
          user.admin?
        end
      end
    end