Module: Permit::Models::AssociationExtensions
- Includes:
- Support
- Defined in:
- lib/models/association.rb
Overview
Defines a set of methods to extend the has_many :authorizations associations to help with querying for common cases. Some of these methods do not show up in the documentation because they are dynamically created with class_eval so that they can be explicit to the models you use for the Person and Role models. See the documentation for #method_missing for additionally created methods.
Class Method Summary (collapse)
Instance Method Summary (collapse)
-
- (<permit_authorization>) as(roles)
Finds all authorizations for the given role(s).
-
- (<permit_authorization>) for(resources)
Finds all authorizations for the given resources.
-
- (<permit_authorization>) for_resources_as(resources, roles)
Finds all authorizations for the given resource and role(s).
-
- (Object) method_missing(*args, &block)
Defines three methods used for getting your subject models for a resource, or as various roles, as well as role models for a given resource.
-
- (<permit_authorizable>) resources_as(roles)
Finds all of the resources authorized for the given role(s).
Methods included from Support
#authorization_conditions, #get_role, #get_roles, #permit_arrayify, #resource_conditions, #role_condition
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (<permit_person>) people_for(resources) - (<permit_person>) people_as(roles) - (<permit_role>) roles_for(resources)
Defines three methods used for getting your subject models for a resource, or as various roles, as well as role models for a given resource.
87 |
# File 'lib/models/association.rb', line 87 def method_missing(*args, &block); super; end |
Class Method Details
+ (Object) extended(klass)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/models/association.rb', line 89 def self.extended(klass) class_eval <<-END def #{Permit::Config.person_class.plural_class_symbol.to_s}_for(resources) self.for(resources).collect(&:#{Permit::Config.person_class.class_symbol.to_s}).uniq end def #{Permit::Config.person_class.plural_class_symbol.to_s}_as(roles) as(roles).collect(&:#{Permit::Config.person_class.class_symbol.to_s}).uniq end def #{Permit::Config.role_class.plural_class_symbol.to_s}_for(resources) self.for(resources).collect(&:#{Permit::Config.role_class.class_symbol.to_s}).uniq end END end |
Instance Method Details
- (<permit_authorization>) as(roles)
Finds all authorizations for the given role(s).
28 29 30 31 |
# File 'lib/models/association.rb', line 28 def as(roles) conditions = (roles, :any) find(:all, :conditions => conditions) end |
- (<permit_authorization>) for(resources)
Finds all authorizations for the given resources
18 19 20 21 |
# File 'lib/models/association.rb', line 18 def for(resources) conditions = (nil, resources) find(:all, :conditions => conditions) end |
- (<permit_authorization>) for_resources_as(resources, roles)
Finds all authorizations for the given resource and role(s).
40 41 42 43 |
# File 'lib/models/association.rb', line 40 def for_resources_as(resources, roles) conditions = (roles, resources) find(:all, :conditions => conditions) end |
- (<permit_authorizable>) resources_as(roles)
Finds all of the resources authorized for the given role(s).
51 52 53 |
# File 'lib/models/association.rb', line 51 def resources_as(roles) as(roles).collect(&:resource).uniq end |