Class: Shamu::Security::ActiveRecordPolicy
- Defined in:
- lib/shamu/security/active_record_policy.rb
Overview
Extends the standard Policy class to add ActiveRecord::Relation refinements based on granted policies.
Instance Attribute Summary
Attributes inherited from Policy
Dependencies collapse
-
#refine(*actions, model_class) {|relation, additional_context| ... }
Declare a refinement that should be applied to an ActiveRecord::Relation for the given actions.
Instance Method Summary collapse
-
#refine_relation(action, relation, additional_context = nil) ⇒ ActiveRecord::Relation
Refine an ActiveRecord::Relation to select only those records permitted for the given
action.
Methods inherited from Policy
#alias_action, #authorize!, #deny, #in_role?, #initialize, #permissions, #permit, #permit?, #when_elevated
Methods included from Roles
expand_roles, role, role_defined?, roles
Constructor Details
This class inherits a constructor from Shamu::Security::Policy
Instance Method Details
#refine(*actions, model_class) {|relation, additional_context| ... }
This method returns an undefined value.
Declare a refinement that should be applied to an ActiveRecord::Relation for the given actions. #refine_relation will yield the relation to any matching refinement to reduce the scope of available records available for projection.
93 94 95 |
# File 'lib/shamu/security/active_record_policy.rb', line 93 def refine( *actions, model_class, &block ) refinements << PolicyRefinement.new( ( actions ), model_class, block ) end |
#refine_relation(action, relation, additional_context = nil) ⇒ ActiveRecord::Relation
Refine an ActiveRecord::Relation to select only those records
permitted for the given action.
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/shamu/security/active_record_policy.rb', line 49 def refine_relation( action, relation, additional_context = nil ) refined = false refinements.each do |refinement| if refinement.match?( action, relation ) refined = true relation = refinement.apply( relation, additional_context ) || relation end end refined ? relation : relation.none end |