Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Simple roles system for Rails apps

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
app
config
db/migrate
lib
script
spec
.gitignore
.rspec
Gemfile
MIT-LICENSE
README.textile
Rakefile
VERSION
simple_roles.gemspec

README.textile

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 to 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

  admin_role = Role.find_by_name("admin")
  user = User.new(:name => "stanislaw")
  user.roles_list.should be_empty
  user.roles << :admin
  user.db_roles.should include(admin_role)
  user.roles.roles.should include(:admin)
  user.has_role?(:admin).should be_true
  user.has_roles?(:admin).should be_true
  user.save!
  user.db_roles.should include(admin_role)
  user.roles.should include(:admin)
  user = User.find_by_name "stanislaw"
  user.roles.should include(:admin)
  user.roles.remove(:admin)
  user.roles.should == []
  user.roles.roles.should == []
  user.save!
  user.roles.should == []
  user.roles = [:admin, :user]
  user.roles.should == [:admin, :user]
  user.has_role?(:admin, :user).should be_true
  user.has_roles?([:admin, :user]).should be_true
  user.roles.clear
  user.roles.should be_empty
  user.roles << :admin
  user.db_roles.should include(admin_role)
  user.roles.roles.should include(:admin)
  user.add_role :user
  user.roles.roles.should include(:user)

Todo:

  • Write role groups part
  • Provide some more config options
  • Usage section in this Readme ;).
  • More and better tests
Something went wrong with that request. Please try again.