Module: Challah::Rolls::Role

Defined in:
lib/challah/rolls/role.rb

Overview

AuthableRole is used to extend functionality to a model in your app named Role. By default, this model already exists within the challah engine.

The Role model is used to group together sets of permissions that can be assigned to users.

Roles are not used to detect features or options for a user. Instead, you should always use permissions as the most granular level of detail within your app.

For example, to restrict a piece of your application to a certain user, you should create a new permission level that restricts it. Then, you can assign that permission to roles and users on an ad hoc basis.

In most cases roles should not be accessed directly, and only be used as a means to quickly assign a user to several permissions. In other words, checking to see what role a user is assigned to in your app is probably not a great idea. Use permission checks instead.

The administrator role, which is included in the seeds file by default, is automatically able to access all permissions. All subsequently added permissions will also be added to the administrator role.

Validations

A role requires that a unique name be provided.

Associations

The following associations are set on this model by default:

  • Has many users

  • Has many permissions

The join table (permission_roles) is also included, but likely does not need to be accessed directly.

Customizing the Role model

By default, the Role model is included within the gem engine. However, if you wish to include it within your app for any customizations, you can do so by creating a model file named role.rb and adding the authable_role line near the top of the class.

Examples:

app/models/role.rb

class Role < ActiveRecord::Base
  # Set up all role methods from challah gem
  authable_role

  # Your customizations here..
end

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Instance Method Summary collapse

Instance Method Details

#challah_roleObject

This method sets up the Role class with all baked in methods.

A role requires the presence of the name and default_path attributes.

Once this method has been called, the InstanceMethods and ClassMethods modules will be accessibile within the Role model.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/challah/rolls/role.rb', line 57

def challah_role
  unless included_modules.include?(InstanceMethods)
    include InstanceMethods
    extend ClassMethods
  end

  class_eval do
    # Validations
    ################################################################

    validates :name, :presence => true, :uniqueness => true

    # Relationships
    ################################################################

    has_many :permission_roles,   :dependent => :destroy

    has_many :permissions,        :through => :permission_roles,
                                  :order => 'permissions.name'

    has_many :users,              :order => 'users.first_name, users.last_name'

    # Scoped Finders
    ################################################################

    default_scope order('roles.name')

    # Callbacks
    ################################################################

    after_save :save_permission_keys

    # Attributes
    ################################################################

    attr_accessible :description, :default_path, :locked, :name
  end
end