simple_roles 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. data/.rspec +1 -0
  2. data/Gemfile +22 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +81 -0
  5. data/Rakefile +53 -0
  6. data/VERSION +1 -0
  7. data/app/assets/images/simple_roles/.gitkeep +0 -0
  8. data/app/assets/javascripts/simple_roles/.gitkeep +0 -0
  9. data/app/assets/stylesheets/simple_roles/.gitkeep +0 -0
  10. data/app/controllers/.gitkeep +0 -0
  11. data/app/helpers/.gitkeep +0 -0
  12. data/app/mailers/.gitkeep +0 -0
  13. data/app/models/.gitkeep +0 -0
  14. data/app/models/role.rb +4 -0
  15. data/app/models/user_role.rb +4 -0
  16. data/app/views/.gitkeep +0 -0
  17. data/config/routes.rb +2 -0
  18. data/db/migrate/001_create_user_roles.rb +15 -0
  19. data/db/migrate/002_create_roles.rb +22 -0
  20. data/lib/simple_roles/base.rb +111 -0
  21. data/lib/simple_roles/configuration.rb +33 -0
  22. data/lib/simple_roles/engine.rb +4 -0
  23. data/lib/simple_roles/macros.rb +13 -0
  24. data/lib/simple_roles/roles_array.rb +63 -0
  25. data/lib/simple_roles/version.rb +3 -0
  26. data/lib/simple_roles.rb +19 -0
  27. data/lib/tasks/simple_roles_tasks.rake +4 -0
  28. data/script/rails +6 -0
  29. data/simple_roles.gemspec +196 -0
  30. data/spec/dummy/Rakefile +7 -0
  31. data/spec/dummy/app/assets/javascripts/application.js +9 -0
  32. data/spec/dummy/app/assets/javascripts/default.js +2 -0
  33. data/spec/dummy/app/assets/javascripts/jquery.js +16 -0
  34. data/spec/dummy/app/assets/javascripts/jquery_ujs.js +169 -0
  35. data/spec/dummy/app/assets/javascripts/posts.js +2 -0
  36. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  37. data/spec/dummy/app/assets/stylesheets/default.css +4 -0
  38. data/spec/dummy/app/assets/stylesheets/posts.css +4 -0
  39. data/spec/dummy/app/assets/stylesheets/scaffold.css +56 -0
  40. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  41. data/spec/dummy/app/controllers/default_controller.rb +5 -0
  42. data/spec/dummy/app/controllers/posts_controller.rb +83 -0
  43. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  44. data/spec/dummy/app/helpers/default_helper.rb +2 -0
  45. data/spec/dummy/app/helpers/posts_helper.rb +2 -0
  46. data/spec/dummy/app/mailers/.gitkeep +0 -0
  47. data/spec/dummy/app/models/.gitkeep +0 -0
  48. data/spec/dummy/app/models/post.rb +2 -0
  49. data/spec/dummy/app/models/user.rb +15 -0
  50. data/spec/dummy/app/views/default/index.html.erb +4 -0
  51. data/spec/dummy/app/views/devise/confirmations/new.html.erb +12 -0
  52. data/spec/dummy/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  53. data/spec/dummy/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  54. data/spec/dummy/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  55. data/spec/dummy/app/views/devise/passwords/edit.html.erb +16 -0
  56. data/spec/dummy/app/views/devise/passwords/new.html.erb +12 -0
  57. data/spec/dummy/app/views/devise/registrations/edit.html.erb +25 -0
  58. data/spec/dummy/app/views/devise/registrations/new.html.erb +18 -0
  59. data/spec/dummy/app/views/devise/sessions/new.html.erb +17 -0
  60. data/spec/dummy/app/views/devise/shared/_links.erb +25 -0
  61. data/spec/dummy/app/views/devise/unlocks/new.html.erb +12 -0
  62. data/spec/dummy/app/views/layouts/application.html.erb +15 -0
  63. data/spec/dummy/app/views/posts/_form.html.erb +17 -0
  64. data/spec/dummy/app/views/posts/edit.html.erb +6 -0
  65. data/spec/dummy/app/views/posts/index.html.erb +21 -0
  66. data/spec/dummy/app/views/posts/new.html.erb +5 -0
  67. data/spec/dummy/app/views/posts/show.html.erb +5 -0
  68. data/spec/dummy/config/application.rb +51 -0
  69. data/spec/dummy/config/boot.rb +10 -0
  70. data/spec/dummy/config/database.yml +26 -0
  71. data/spec/dummy/config/database_mysql.yml +29 -0
  72. data/spec/dummy/config/database_pgsql.yml +35 -0
  73. data/spec/dummy/config/environment.rb +5 -0
  74. data/spec/dummy/config/environments/development.rb +24 -0
  75. data/spec/dummy/config/environments/production.rb +52 -0
  76. data/spec/dummy/config/environments/test.rb +39 -0
  77. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  78. data/spec/dummy/config/initializers/devise.rb +204 -0
  79. data/spec/dummy/config/initializers/inflections.rb +10 -0
  80. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  81. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  82. data/spec/dummy/config/initializers/session_store.rb +8 -0
  83. data/spec/dummy/config/initializers/simple_roles.rb +3 -0
  84. data/spec/dummy/config/initializers/wrap_parameters.rb +12 -0
  85. data/spec/dummy/config/locales/devise.en.yml +53 -0
  86. data/spec/dummy/config/locales/en.yml +5 -0
  87. data/spec/dummy/config/routes.rb +13 -0
  88. data/spec/dummy/config.ru +4 -0
  89. data/spec/dummy/db/migrate/010_devise_create_users.rb +29 -0
  90. data/spec/dummy/db/migrate/20110925210726_create_user_roles.rb +15 -0
  91. data/spec/dummy/db/migrate/20110925210727_create_roles.rb +22 -0
  92. data/spec/dummy/db/schema.rb +51 -0
  93. data/spec/dummy/db/seeds.rb +14 -0
  94. data/spec/dummy/log/.gitkeep +0 -0
  95. data/spec/dummy/public/404.html +26 -0
  96. data/spec/dummy/public/422.html +26 -0
  97. data/spec/dummy/public/500.html +26 -0
  98. data/spec/dummy/public/favicon.ico +0 -0
  99. data/spec/dummy/script/rails +6 -0
  100. data/spec/dummy/test/fixtures/posts.yml +11 -0
  101. data/spec/dummy/test/fixtures/users.yml +11 -0
  102. data/spec/dummy/test/functional/default_controller_test.rb +9 -0
  103. data/spec/dummy/test/functional/posts_controller_test.rb +49 -0
  104. data/spec/dummy/test/unit/helpers/default_helper_test.rb +4 -0
  105. data/spec/dummy/test/unit/helpers/posts_helper_test.rb +4 -0
  106. data/spec/dummy/test/unit/post_test.rb +7 -0
  107. data/spec/dummy/test/unit/user_test.rb +7 -0
  108. data/spec/dummy_spec_helper.rb +41 -0
  109. data/spec/integration/main_spec.rb +7 -0
  110. data/spec/integration/messages_spec.rb +62 -0
  111. data/spec/integration/requests/main_spec.rb +21 -0
  112. data/spec/simple_roles/base_spec.rb +191 -0
  113. data/spec/simple_roles/macros_spec.rb +25 -0
  114. data/spec/spec_helper.rb +52 -0
  115. data/spec/support/aliases.rb +0 -0
  116. data/spec/support/controller_macros.rb +26 -0
  117. data/spec/support/database.yml +6 -0
  118. data/spec/support/factories.rb +22 -0
  119. data/spec/support/fixtures/models/.gitkeep +0 -0
  120. data/spec/support/fixtures/models/user.rb +3 -0
  121. data/spec/support/migrations/010_create_users.rb +14 -0
  122. data/spec/support/rspec_helpers.rb +22 -0
  123. metadata +320 -0
@@ -0,0 +1,15 @@
1
+ class CreateUserRoles < ActiveRecord::Migration
2
+ def up
3
+ create_table :user_roles do |t|
4
+ t.integer :user_id
5
+ t.integer :role_id
6
+
7
+ t.timestamps
8
+ end
9
+ add_index :user_roles, [:user_id, :role_id]
10
+ end
11
+
12
+ def down
13
+ drop_table :user_roles
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ class CreateRoles < ActiveRecord::Migration
2
+ def up
3
+ create_table :roles do |t|
4
+ t.string :name
5
+ t.timestamps
6
+ end
7
+
8
+ create_roles
9
+ end
10
+
11
+ def down
12
+ drop_table :roles
13
+ end
14
+
15
+
16
+ def create_roles
17
+ SimpleRoles::Configuration.valid_roles.each do |role|
18
+ Role.create(:name => role.to_s)
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,51 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended to check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(:version => 20110925210727) do
15
+
16
+ create_table "roles", :force => true do |t|
17
+ t.string "name"
18
+ t.datetime "created_at"
19
+ t.datetime "updated_at"
20
+ end
21
+
22
+ create_table "user_roles", :force => true do |t|
23
+ t.integer "user_id"
24
+ t.integer "role_id"
25
+ t.datetime "created_at"
26
+ t.datetime "updated_at"
27
+ end
28
+
29
+ add_index "user_roles", ["user_id", "role_id"], :name => "index_user_roles_on_user_id_and_role_id"
30
+
31
+ create_table "users", :force => true do |t|
32
+ t.string "email", :default => "", :null => false
33
+ t.string "encrypted_password", :limit => 128, :default => "", :null => false
34
+ t.string "reset_password_token"
35
+ t.datetime "reset_password_sent_at"
36
+ t.datetime "remember_created_at"
37
+ t.integer "sign_in_count", :default => 0
38
+ t.datetime "current_sign_in_at"
39
+ t.datetime "last_sign_in_at"
40
+ t.string "current_sign_in_ip"
41
+ t.string "last_sign_in_ip"
42
+ t.string "name"
43
+ t.string "username"
44
+ t.datetime "created_at"
45
+ t.datetime "updated_at"
46
+ end
47
+
48
+ add_index "users", ["email"], :name => "index_users_on_email", :unique => true
49
+ add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
50
+
51
+ end
@@ -0,0 +1,14 @@
1
+ puts "loading seeds"
2
+ User.delete_all
3
+
4
+ ['stanislaw', 'marixa', 'kristian', 'miloviza'].each do |name|
5
+ User.create!(
6
+ :username => name,
7
+ :email => "#{name}@gmail.com",
8
+ :password => "666666",
9
+ :password_confirmation => "666666"
10
+ )
11
+ end
12
+
13
+ first_user = User.find_by_username("stanislaw")
14
+ second_user = User.find_by_username("marixa")
File without changes
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ <p>We've been notified about this issue and we'll take a look at it shortly.</p>
24
+ </div>
25
+ </body>
26
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,11 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
2
+
3
+ # This model initially had no columns defined. If you add columns to the
4
+ # model remove the '{}' from the fixture names and add the columns immediately
5
+ # below each fixture, per the syntax in the comments below
6
+ #
7
+ one: {}
8
+ # column: value
9
+ #
10
+ two: {}
11
+ # column: value
@@ -0,0 +1,11 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
2
+
3
+ # This model initially had no columns defined. If you add columns to the
4
+ # model remove the '{}' from the fixture names and add the columns immediately
5
+ # below each fixture, per the syntax in the comments below
6
+ #
7
+ one: {}
8
+ # column: value
9
+ #
10
+ two: {}
11
+ # column: value
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ class DefaultControllerTest < ActionController::TestCase
4
+ test "should get index" do
5
+ get :index
6
+ assert_response :success
7
+ end
8
+
9
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ class PostsControllerTest < ActionController::TestCase
4
+ setup do
5
+ @post = posts(:one)
6
+ end
7
+
8
+ test "should get index" do
9
+ get :index
10
+ assert_response :success
11
+ assert_not_nil assigns(:posts)
12
+ end
13
+
14
+ test "should get new" do
15
+ get :new
16
+ assert_response :success
17
+ end
18
+
19
+ test "should create post" do
20
+ assert_difference('Post.count') do
21
+ post :create, post: @post.attributes
22
+ end
23
+
24
+ assert_redirected_to post_path(assigns(:post))
25
+ end
26
+
27
+ test "should show post" do
28
+ get :show, id: @post.to_param
29
+ assert_response :success
30
+ end
31
+
32
+ test "should get edit" do
33
+ get :edit, id: @post.to_param
34
+ assert_response :success
35
+ end
36
+
37
+ test "should update post" do
38
+ put :update, id: @post.to_param, post: @post.attributes
39
+ assert_redirected_to post_path(assigns(:post))
40
+ end
41
+
42
+ test "should destroy post" do
43
+ assert_difference('Post.count', -1) do
44
+ delete :destroy, id: @post.to_param
45
+ end
46
+
47
+ assert_redirected_to posts_path
48
+ end
49
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class DefaultHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class PostsHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class PostTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class UserTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -0,0 +1,41 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'rubygems'
3
+
4
+ ENV["RAILS_ENV"] ||= 'test'
5
+
6
+ require File.expand_path("../dummy/config/environment", __FILE__)
7
+ require 'require_all'
8
+ require 'rspec/rails'
9
+ require 'cutter'
10
+ require 'capybara/rails'
11
+ require 'capybara/rspec'
12
+ #require 'shoulda'
13
+ require 'factory_girl'
14
+ require 'sugar-high/dsl'
15
+ require_all File.expand_path('../support', __FILE__)
16
+
17
+ #ActiveRecord::Base.logger = Logger.new(STDERR)
18
+
19
+ RSpec.configure do |config|
20
+ config.include Warden::Test::Helpers, :type => :request
21
+ config.after(:each, :type => :request) {Warden.test_reset!}
22
+
23
+ config.mock_with :rspec
24
+
25
+ config.include Factory::Syntax::Methods
26
+
27
+ config.before(:suite) do
28
+ with ActiveRecord::Base.connection do
29
+ tables.map do |table|
30
+ drop_table table
31
+ end
32
+ end
33
+
34
+ with ActiveRecord::Migrator do
35
+ migrate File.expand_path('../dummy/db/migrate', __FILE__)
36
+ end
37
+
38
+ require "dummy/db/seeds"
39
+ end
40
+ end
41
+
@@ -0,0 +1,7 @@
1
+ require 'dummy_spec_helper'
2
+
3
+ describe "" do
4
+ it "truth" do
5
+ Rails.application.should be_kind_of(Dummy::Application)
6
+ end
7
+ end
@@ -0,0 +1,62 @@
1
+ require 'dummy_spec_helper'
2
+
3
+ module DeviseSessionHelpers
4
+ def login_with email, password
5
+ fill_in "Email", :with => email
6
+ fill_in "Password", :with => password
7
+ click_button "Sign in"
8
+ end
9
+
10
+ def login_user
11
+ visit new_user_session_path
12
+ login_with 'stanislaw@gmail.com', "666666"
13
+ end
14
+ end
15
+
16
+ feature "Messages", %q{
17
+ In order to have...
18
+ As an user
19
+ I want to do something with messages} do
20
+
21
+ background do
22
+ Capybara.reset_sessions!
23
+ end
24
+
25
+ include DeviseSessionHelpers
26
+
27
+ scenario "Show messages index", :js => true do
28
+ login_user
29
+ visit '/carrier'
30
+ end
31
+
32
+ scenario "Show concerto index to musician", :js => true do
33
+ pending
34
+ login_musician
35
+ # save_and_open_page
36
+
37
+ end
38
+
39
+ scenario "Show concerto to musician" do
40
+ pending
41
+
42
+ login_musician
43
+
44
+ visit '/concertos/one' # using friendly id :)
45
+ page.should have_content('one')
46
+ visit '/concertos/two'
47
+ page.should have_content('two')
48
+ end
49
+
50
+ scenario "Show concerto admin index to composer", :js => true do
51
+ pending
52
+ login_composer
53
+
54
+ visit '/concertos/admin'
55
+ # save_and_open_page
56
+
57
+ #puts page.body.inspect
58
+ page.should have_content('one')
59
+ page.should have_content('two')
60
+ end
61
+ end
62
+
@@ -0,0 +1,21 @@
1
+ require 'dummy_spec_helper'
2
+
3
+ describe "Requests" do
4
+ it "truth" do
5
+ Rails.application.should be_kind_of(Dummy::Application)
6
+ end
7
+
8
+ describe "Basic pages" do
9
+ before do
10
+ login_as(User.first)
11
+ end
12
+
13
+ it "should get root" do
14
+ pending
15
+
16
+ get '/'
17
+ response.status.should be(200)
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,191 @@
1
+ require 'spec_helper'
2
+
3
+ SimpleRoles.configure do |config|
4
+ config.valid_roles = [:user, :admin, :editor]
5
+ end
6
+
7
+ describe SimpleRoles::Base do
8
+
9
+ context "Class Methods" do
10
+ subject { User }
11
+
12
+ context "Scopes" do
13
+ before do
14
+ end
15
+
16
+ SimpleRoles::Configuration.valid_roles.each do |vr|
17
+ it {should respond_to(:"#{vr}s")}
18
+ it {should respond_to(:"#{vr}s_ids")}
19
+
20
+ its(:"#{vr}s") { should be_kind_of(Array) }
21
+ end
22
+
23
+ end
24
+ specify { should respond_to(:valid_roles) }
25
+ its(:valid_roles) { should include(:user, :admin)}
26
+ end
27
+
28
+ context "Instance methods" do
29
+ subject {User.new}
30
+
31
+ [:db_roles, :user_roles].each do |meth|
32
+ specify { should respond_to(meth) }
33
+ its(:"#{meth}") { should be_empty }
34
+ end
35
+
36
+ [:roles, :roles_list, :role_groups_list].each do |meth|
37
+ specify { should respond_to(meth) }
38
+ its(:"#{meth}") { should be_empty }
39
+ end
40
+
41
+ context "#roles" do
42
+ it "call on #roles.clear should raise error" do
43
+ lambda {
44
+ roles.clear
45
+ }.should raise_error
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ context "Read API" do
52
+ subject do
53
+ @user ||= User.new(:name => "stanislaw")
54
+ end
55
+
56
+ it "#has_role?, #has_roles?" do
57
+ subject.roles << :admin
58
+ subject.has_role?(:admin).should be_true
59
+ subject.has_role?(:admin, :user).should be_false
60
+ subject.has_roles?(:editor).should be_false
61
+ subject.roles << :user
62
+ subject.has_role?(:admin, :user).should be_true
63
+ subject.has_role?([:admin, :user]).should be_true
64
+ end
65
+
66
+ it "#admin?, #user?, #editor? ..." do
67
+ subject.roles << :admin
68
+ subject.admin?.should be_true
69
+ subject.is_admin?.should be_true
70
+ subject.user?.should be_false
71
+ subject.editor?.should be_false
72
+ subject.roles << :editor
73
+ subject.editor?.should be_true
74
+ end
75
+ end
76
+
77
+ context "Write API" do
78
+ subject do
79
+ @user ||= User.new(:name => "stanislaw")
80
+ end
81
+
82
+ it "#roles= should set roles" do
83
+ subject.roles = :admin
84
+ subject.roles.should == Array.new([:admin])
85
+ subject.roles = :user
86
+ subject.roles.should == Array.new([:user])
87
+ end
88
+
89
+ it "#roles= should set roles if array of strings passed (sh accept strings too!)" do
90
+ subject.roles = 'admin'
91
+ subject.roles.should == Array.new([:admin])
92
+ subject.roles = ['user', 'editor']
93
+ subject.roles.should == Array.new([:user, :editor])
94
+ end
95
+
96
+ it "#roles << should add roles" do
97
+ subject.roles << :admin
98
+ subject.roles.should == Array.new([:admin])
99
+ subject.roles << :user
100
+ subject.roles.should == Array.new([:admin, :user])
101
+ end
102
+
103
+ it "#remove_roles should remove roles" do
104
+ subject.roles << :admin
105
+ subject.roles << :user
106
+ subject.roles << :editor
107
+
108
+ subject.roles.should == Array.new([:admin, :user, :editor])
109
+
110
+ subject.remove_roles :admin
111
+ subject.roles.should == Array.new([:user, :editor])
112
+
113
+ subject.remove_roles :admin, :user, :editor
114
+ subject.roles.should == Array.new([])
115
+ end
116
+ end
117
+
118
+ context "Integration for roles methods" do
119
+ it "should work when #flatten is called over #roles" do
120
+ user = User.new(:name => "stanislaw")
121
+ user.roles << :admin
122
+
123
+ user.roles_list.should == Array.new([:admin])
124
+ user.roles_list.flatten.should == Array.new([:admin])
125
+ end
126
+
127
+ it "should add :roles to accessible_attributes if they are Whitelisted" do
128
+ user = User.new(:name => "stanislaw")
129
+ user.roles << :admin
130
+
131
+ user.roles_list.should include(:admin)
132
+ user.save!
133
+ User.find_by_name!("stanislaw").should be_kind_of(User)
134
+ User.delete_all
135
+
136
+ User.attr_accessible :name
137
+
138
+ user = User.new(:name => "stanislaw")
139
+ user.roles << :admin
140
+ user.roles_list.should include(:admin)
141
+ user.save!
142
+ User.find_by_name!("stanislaw").should be_kind_of(User)
143
+ end
144
+
145
+ pending "should not duplicate roles when adding" do
146
+
147
+ end
148
+
149
+ it "should all work" do
150
+ admin_role = Role.find_by_name("admin")
151
+ user = User.new(:name => "stanislaw")
152
+ user.roles_list.should be_empty
153
+ user.has_any_role?(:admin).should be_false
154
+ user.roles << :admin
155
+ user.db_roles.should include(admin_role)
156
+ user.roles_list.should include(:admin)
157
+ user.roles.should include(:admin)
158
+ user.has_role?(:admin).should be_true
159
+ user.admin?.should be_true
160
+ user.is_admin?.should be_true
161
+ user.has_roles?(:admin).should be_true
162
+ user.save!
163
+ user.db_roles.should include(admin_role)
164
+ user.roles.should include(:admin)
165
+ user = User.find_by_name! "stanislaw"
166
+ user.roles.should include(:admin)
167
+ user.roles.remove(:admin)
168
+ user.roles.should be_empty
169
+ user.save!
170
+ user.roles.should be_empty
171
+ user.roles = [:admin, :user]
172
+ user.roles.should == Array.new([:admin, :user])
173
+ user.has_role?(:admin, :user).should be_true
174
+ user.has_roles?([:admin, :user]).should be_true
175
+ user.db_roles.size.should == 2
176
+ user.roles.clear!
177
+ user.db_roles.should be_empty
178
+ user.roles.should be_empty
179
+ user.roles << :admin
180
+ user.db_roles.should include(admin_role)
181
+ user.roles.should include(:admin)
182
+ user.add_role :user
183
+ user.roles.should include(:user, :admin)
184
+ user.has_any_role?(:user).should be_true
185
+ user.has_any_role?(:user, :admin).should be_true
186
+ user.has_any_role?([:user, :admin])
187
+ user.has_any_role?(:blip).should be_false
188
+ end
189
+ end
190
+
191
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleRoles::Base do
4
+
5
+ context "Macros availability" do
6
+ subject { Module }
7
+ before {
8
+ require 'simple_roles'
9
+ }
10
+
11
+ specify { should be_kind_of(SimpleRoles::Macros) }
12
+ end
13
+
14
+ context "after applying macros" do
15
+ subject { User }
16
+ before do
17
+ class User < ActiveRecord::Base
18
+ simple_roles
19
+ end
20
+ end
21
+
22
+ specify { should be_kind_of(SimpleRoles::Macros) }
23
+ end
24
+
25
+ end