Roles for Active Record
Roles lets you add a role strategy of choice to your user model.
Roles supports strategies with either an inline attribute on user or a separate Role model (see below).
gem install roles_active_record
Install in Rails app
Insert in Gemfile:
$ bundle install from terminal
Alternatively install using Cream
The following Role strategies are available for Active Record:
Inline attribute on User
These strategies all use an inline attribute on the User model.
Reference to Role
These strategies use a separate Role model (class and table).
Role strategy configuration
The following demonstrates some examples of role strategy configuration.
class User < ActiveRecord::Base include Roles::ActiveRecord strategy :admin_flag, :default valid_roles_are :admin, :guest end
For strategies that use a separate Role model you must call the class method #role_class with the name of the role class
class Bruger < ActiveRecord::Base include Roles::ActiveRecord strategy :one_role, :role_class => :rolle valid_roles_are :admin, :guest ... end
Here the role names have been customized
Default Role classes
The default role classes can currently be included by:
Note: These files will automatically be included when needed under normal conditions.
The gem includes these Rails 3 generators:
Let you populate a User model with a given role strategy
Example: Apply Role strategy admin_flag to the User and make the default roles :admin and :guest available
$ rails g active_record:roles User --strategy admin_flag
Example: Apply Role strategy role_string to the User and make the roles :admin, :guest and :author available
$ rails g active_record:roles_migration User --strategy role_string --roles author
Example: Apply Role strategy one_role to the User model with roles :user, :special and :editor
$ rails g active_record:roles_migration User --strategy one_role --roles user special editor --no-default-roles
Example: Apply Role strategy many_role to the User model with default roles and customizing role class names to BrugerRolle and Rolle
$ rails g active_record:roles_migration User -s one_role -r user editor -rc Rolle -urc BrugerRolle
For the strategies one_role and many_roles the generator also generates the Role and UserRole classes in the app/models dir. If you customize the names of these
classes (using generator arguments), the customized classes will be generated and “linked up” correctly (with has and belongs_to statements).
Roles migration generator
In case you only want to generate Role migrations.
Example: admin_flag Role strategy
$ rails g active_record:roles_migration User --strategy admin_flag
Create reverse migration
$ rails g active_record:roles_migration User --strategy admin_flag --reverse
Note on Patches/Pull Requests
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don’t break it in a
future version unintentionally.
- Commit, do not mess with rakefile, version, or history.
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
- Send me a pull request. Bonus points for topic branches.
Copyright © 2010 Kristian Mandrup. See LICENSE for details.