/roles-field

自己编写的用户角色 Ruby Gem. 目前用在 eshare 项目中。

Primary LanguageRuby

roles-field

Gem Version Build Status Code Climate Coverage Status

Use for

To add simple roles feature to a model (like user) in a integer field.

Install

include in Gemfile:

gem 'roles-field'

Usage

in a model with a field named 'roles_mask' (you can use any other field name):

# generate a model with a integer field
create_table :users, :force => true do |t|
  t.column :roles_mask, :integer
end

class User < ActiveRecord::Base
  # you can change to another field
  roles_field :roles_mask, :roles => [:admin, :manager, :teacher, :student]
end

then

@user.roles
# -> []

@user.role = :manager
@user.save
@user.roles
# -> [:manager]

@user.set_role :admin
@user.save
@user.roles
# -> [:admin]

@user.role? :admin
@user.is_admin?
# -> true

@user.role? :teacher
@user.is_teacher?
# -> false

@user.role
# -> :admin
# return user's first role

User.with_role :admin
# -> scope [...]

TODO

to support multi-roles setting