Class: Regulator::ActiveAdminAdapter

Inherits:
ActiveAdmin::AuthorizationAdapter
  • Object
show all
Defined in:
lib/regulator/active_admin_adapter.rb

Instance Method Summary collapse

Instance Method Details

#authorized?(action, subject = nil) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
# File 'lib/regulator/active_admin_adapter.rb', line 3

def authorized?(action, subject = nil)
  policy = retrieve_policy(subject)
  action = format_action(action, subject)
  policy.respond_to?(action) && policy.public_send(action)
end

#format_action(action, subject) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/regulator/active_admin_adapter.rb', line 23

def format_action(action, subject)
  # https://github.com/elabs/regulator/blob/master/lib/generators/regulator/install/templates/application_policy.rb
  case action
  when ActiveAdmin::Auth::CREATE  then :create?
  when ActiveAdmin::Auth::UPDATE  then :update?
  when ActiveAdmin::Auth::READ    then subject.is_a?(Class) ? :index? : :show?
  when ActiveAdmin::Auth::DESTROY then subject.is_a?(Class) ? :destroy_all? : :destroy?
  else "#{action}?"
  end
end

#retrieve_policy(subject) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/regulator/active_admin_adapter.rb', line 15

def retrieve_policy(subject)
  case subject
  when nil   then Regulator.policy!(user, resource, self.resource.controller)
  when Class then Regulator.policy!(user, subject.new, self.resource.controller)
  else Regulator.policy!(user, subject, self.resource.controller)
  end
end

#scope_collection(collection, action = Auth::READ) ⇒ Object



9
10
11
12
13
# File 'lib/regulator/active_admin_adapter.rb', line 9

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
  Regulator.policy_scope!(user, collection, self.resource.controller)
end