Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tag: v0.0.4
Fetching contributors…

Cannot retrieve contributors at this time

82 lines (56 sloc) 2.173 kB

SimpleRoles

SimpleRoles is a Rails Engine providing simple Role System for any Rails 3 app. It was created as demo role-system to accompany CanTango gem initial installiation and usage. Intended to be very easy to setup & use.

It even seems to be good for being used as a real role system inspite of (or due to ;)) its almost maximum simplicity.

If you are looking for a real serious roles system solution try Troles gem created by Kristian Mandrup

Installiation

Prerequisites

SimpleRoles only assumes you have User model

Not a Gem yet

include in Gemfile:

gem 'simple_roles', :git => "git://github.com/stanislaw/simple_roles.git"
bundle update

Set up valid roles you're gonna have in your app

Create file simple_roles.rb in config/initializers and write there:

# config/initializers/simple_roles.rb
SimpleRoles.configure do |config|
  config.valid_roles = [:user, :admin, :editor]
end

Copy and migrate SimpleRoles migrations by following rake task:

rake simple_roles_engine:install:migrations
rake db:migrate

Note! Migrations are based on roles you set up as valid (see previous step). If you do not create initializer with valid_roles, then valid_roles will be set up to defaults: :user and :admin.

And finally include 'simple_roles' macros in your User model:

class User
  simple_roles
end

Usage

user.roles => #<RolesArray: {}>

user.roles = :admin
user.roles # => #<RolesArray: {:admin}>
user.roles_list # => #<RolesArray: {:admin}>
user.admin? # => true
user.is_admin? # => true

user.roles << :user
user.roles # => #<RolesArray: {:admin, :user}>
user.is_user? # => true

user.add_role :editor
user.roles # => #<RolesArray: #{:admin, :user, :editor}>

user.remove_role :user
user.roles # => #<RolesArray: {:admin, :editor}>
user.has_role?(:admin) # => true
user.has_any_role?(:admin, :blip) # => true
user.has_role?(:blogger) # => false

Todo:

  • Write role groups part
  • Provide some more config options
  • More and better tests
Jump to Line
Something went wrong with that request. Please try again.