class ActiveAdmin::PunditAdapter

Public Instance Methods

authorized?(action, subject = nil) click to toggle source
# File lib/active_admin/pundit_adapter.rb, line 7
def authorized?(action, subject = nil)
  policy = retreive_policy(subject)
  action = format_action(action, subject)

  policy.class.method_defined?(action) && policy.send(action)
end
format_action(action, subject) click to toggle source
# File lib/active_admin/pundit_adapter.rb, line 29
def format_action(action, subject)
  # https://github.com/elabs/pundit/blob/master/lib/generators/pundit/install/templates/application_policy.rb
  case action
  when Auth::CREATE  then :create?
  when Auth::UPDATE  then :update?
  when Auth::READ    then subject.is_a?(Class) ? :index? : :show?
  when Auth::DESTROY then subject.is_a?(Class) ? :destroy_all? : :destroy?
  else "#{action}?"
  end
end
retreive_policy(subject) click to toggle source
# File lib/active_admin/pundit_adapter.rb, line 21
def retreive_policy(subject)
  case subject
  when nil   then Pundit.policy!(user, resource)
  when Class then Pundit.policy!(user, subject.new)
  else Pundit.policy!(user, subject)
  end
end
scope_collection(collection, action = Auth::READ) click to toggle source
# File lib/active_admin/pundit_adapter.rb, line 14
def scope_collection(collection, action = Auth::READ)
  # scoping is appliable only to read/index action
  # which means there is no way how to scope other actions
  Pundit.policy_scope!(user, collection)
end