Bitroles
Simple roles for your models without external tables
Installation
Add this line to your application's Gemfile:
gem 'bitroles'
And then execute:
$ bundle
Or install it yourself as:
$ gem install bitroles
Usage
class MyModel < ActiveRecord::Base
has_roles :admin, :moderator
end
By default bitroles uses roles_mask integer column in your table to store the roles. You can change column name pretty easy:
class MyModel < ActiveRecord::Base
has_roles :admin, :moderator, mask_column: :someothercolumn
end
Available methods
objects = MyModel.with_role(:admin) # Finds all MyModel objects with admin role
obj = objects.first
obj.has_role? :admin # Checks if the object has admin role
obj.is_admin? # Checks the above. It can be is_moderator? or is_whatever? depends on roles given to has_role
obj.roles # Shows all roles of object (strings array)
obj.roles = [:admin, :moderator] # Sets admin and moderator role to obj
obj.admin = true # obj became admin (same for other roles)
obj.admin = false # obj is no longer an admin (same for other roles)
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request