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)

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.

Overloads:

  • - (<permit_person>) people_for(resources)

    Finds all of the subjects that have authorizations for the given resources. Where "people" is the plural name of your subject model.

    Parameters:

    • resource (permit_authorizable, nil, :any, <permit_authorizable, nil>)

      the resources to find authorizations for. :any may be given to find matches for any resource.

    Returns:

    • (<permit_person>)

      a unique list of the people with authorizations for the resource.

  • - (<permit_person>) people_as(roles)

    Finds all of the subjects that have authorizations for the given role(s). Where "people" is the plural name of your subject model.

    Parameters:

    • roles (permit_role, String, Symbol, <permit_role, String, Symbol>)

      the roles to find authorizations for.

    Returns:

    • (<permit_person>)

      a unique list of the people with authorizations for the role(s).

  • - (<permit_role>) roles_for(resources)

    Finds all of the roles authorized for the given resources. Where "roles" is the plural name of your role model.

    Parameters:

    • resource (permit_authorizable, nil, :any, <permit_authorizable, nil>)

      the resources to find authorizations for. :any may be given to find matches for any resource.

    Returns:

    • (<permit_role>)

      a unique list of roles authorized for the 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).

Parameters:

  • roles (permit_role, String, Symbol, <permit_role, String, Symbol>)

    the roles to find authorizations for.

Returns:

  • (<permit_authorization>)

    the authorizations found for the role(s).



28
29
30
31
# File 'lib/models/association.rb', line 28

def as(roles)
  conditions = authorization_conditions(roles, :any)
  find(:all, :conditions => conditions)
end

- (<permit_authorization>) for(resources)

Finds all authorizations for the given resources

Parameters:

  • resource (permit_authorizable, nil, :any, <permit_authorizable, nil>)

    the resources to find authorizations for. :any may be given to find matches for any resource.

Returns:

  • (<permit_authorization>)

    the authorizations found for the resource.



18
19
20
21
# File 'lib/models/association.rb', line 18

def for(resources)
  conditions = authorization_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).

Parameters:

  • resource (permit_authorizable, nil, :any)

    the resource to find authorizations for. :any may be given to find matches for any resource.

  • roles (permit_role, String, Symbol, <permit_role, String, Symbol>)

    the roles to find authorizations for.

Returns:

  • (<permit_authorization>)

    the authorizations found for the resource and role(s)



40
41
42
43
# File 'lib/models/association.rb', line 40

def for_resources_as(resources, roles)
  conditions = authorization_conditions(roles, resources)
  find(:all, :conditions => conditions)
end

- (<permit_authorizable>) resources_as(roles)

Finds all of the resources authorized for the given role(s).

Parameters:

  • roles (permit_role, String, Symbol, <permit_role, String, Symbol>)

    the roles to find authorizations for.

Returns:

  • (<permit_authorizable>)

    a unique list of resources authorized for the role(s).



51
52
53
# File 'lib/models/association.rb', line 51

def resources_as(roles)
  as(roles).collect(&:resource).uniq
end