Module: Permit::Support
- Included in:
- Models::AssociationExtensions, PermitRule, PermitRules
- Defined in:
- lib/permit/support.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary (collapse)
- - (Object) authorization_conditions(roles, resources, person = nil) protected
- - (Object) get_role(role) protected
- - (Object) get_roles(roles) protected
-
- (Array) permit_arrayify(o)
Converts an object to an array of that object if it is not already one.
- - (Object) resource_conditions(resources, params = {}) protected
- - (Object) role_condition(roles, params = {}) protected
Instance Method Details
- (Object) authorization_conditions(roles, resources, person = nil) (protected)
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/permit/support.rb', line 35 def (roles, resources, person = nil) params = {} query = [] if person query << "#{Permit::Config.person_class.name.foreign_key} = :person_id" params[:person_id] = person.id end query << role_condition(roles, params) query << resource_conditions(resources, params) [query.compact.join(" AND "), params] end |
- (Object) get_role(role) (protected)
80 81 82 |
# File 'lib/permit/support.rb', line 80 def get_role(role) role.is_a?(Permit::Config.role_class) ? role : Permit::Config.role_class.find_by_key(role) end |
- (Object) get_roles(roles) (protected)
84 85 86 |
# File 'lib/permit/support.rb', line 84 def get_roles(roles) permit_arrayify(roles).collect {|r| get_role(r)}.compact end |
- (Array) permit_arrayify(o)
Converts an object to an array of that object if it is not already one.
30 31 32 |
# File 'lib/permit/support.rb', line 30 def permit_arrayify(o) Array===o ? o : [o] end |
- (Object) resource_conditions(resources, params = {}) (protected)
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/permit/support.rb', line 62 def resource_conditions(resources, params = {}) constraints = [] permit_arrayify(resources).each_with_index do |resource, idx| type, id = case resource when :any then return nil when nil then [nil, nil] else [resource.class.resource_type, resource.id] end type_key = "resource_type_#{idx}".to_sym id_key = "resource_id_#{idx}".to_sym params.merge! type_key => type, id_key => id op = type.nil? ? 'is' : '=' constraints << "(resource_type #{op} #{type_key.inspect} AND resource_id #{op} #{id_key.inspect})" end return "(" << constraints.join(" OR ") << ")" end |
- (Object) role_condition(roles, params = {}) (protected)
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/permit/support.rb', line 48 def role_condition(roles, params = {}) return nil unless roles r = get_roles(roles) ids = r.collect {|role| role.id} if ids.empty? return nil else params[:role_ids] = ids "#{Permit::Config.role_class.name.foreign_key} in (:role_ids)" end end |