Class: ActiveAdmin::RegulatorAdapter
- Inherits:
-
AuthorizationAdapter
- Object
- AuthorizationAdapter
- ActiveAdmin::RegulatorAdapter
- Defined in:
- lib/generators/regulator/adapter/templates/regulator_active_admin_adapter.rb
Instance Method Summary collapse
- #authorized?(action, subject = nil) ⇒ Boolean
- #format_action(action, subject) ⇒ Object
- #retrieve_policy(subject) ⇒ Object
- #scope_collection(collection, action = Auth::READ) ⇒ Object
Instance Method Details
#authorized?(action, subject = nil) ⇒ Boolean
14 15 16 17 18 19 |
# File 'lib/generators/regulator/adapter/templates/regulator_active_admin_adapter.rb', line 14 def (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
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/generators/regulator/adapter/templates/regulator_active_admin_adapter.rb', line 47 def format_action(action, subject) # https://github.com/elabs/regulator/blob/master/lib/generators/regulator/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 |
#retrieve_policy(subject) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/generators/regulator/adapter/templates/regulator_active_admin_adapter.rb', line 33 def retrieve_policy(subject) case subject when nil then Regulator.policy!(user, resource, regulator_policy_namespace) when Class then Regulator.policy!(user, subject.new, regulator_policy_namespace) else Regulator.policy!(user, subject, regulator_policy_namespace) end rescue Regulator::NotDefinedError => e if default_policy_class default_policy(user, subject) else raise e end end |
#scope_collection(collection, action = Auth::READ) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/generators/regulator/adapter/templates/regulator_active_admin_adapter.rb', line 21 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, regulator_policy_namespace) rescue Regulator::NotDefinedError => e if default_policy_class && default_policy_class.const_defined?(:Scope) default_policy_class::Scope.new(user, collection).resolve else raise e end end |