simple_roles 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/CHANGELOG.md +21 -0
  2. data/Gemfile +2 -4
  3. data/README.md +100 -17
  4. data/Rakefile +6 -12
  5. data/db/migrate/001_create_user_roles.rb +5 -4
  6. data/db/migrate/002_create_roles.rb +6 -3
  7. data/lib/simple_roles/configuration.rb +64 -11
  8. data/lib/simple_roles/engine.rb +2 -0
  9. data/lib/simple_roles/macros.rb +2 -5
  10. data/lib/simple_roles/many/persistence.rb +44 -0
  11. data/lib/simple_roles/many/roles_methods.rb +88 -0
  12. data/lib/simple_roles/many.rb +5 -0
  13. data/lib/simple_roles/one/persistence.rb +13 -0
  14. data/lib/simple_roles/one/roles_methods.rb +38 -0
  15. data/lib/simple_roles/one.rb +5 -0
  16. data/lib/simple_roles/packager.rb +18 -0
  17. data/lib/simple_roles/version.rb +1 -1
  18. data/lib/simple_roles.rb +27 -12
  19. data/simple_roles.gemspec +36 -30
  20. data/spec/dummy/config/database.yml +2 -13
  21. data/spec/dummy/config/environments/development.rb +0 -3
  22. data/spec/dummy/config/initializers/simple_roles.rb +2 -1
  23. data/spec/integration/main_spec.rb +6 -6
  24. data/spec/integration/requests/main_spec.rb +16 -17
  25. data/spec/simple_roles/configuration_spec.rb +66 -0
  26. data/spec/simple_roles/integration_many_spec.rb +47 -0
  27. data/spec/simple_roles/macros_spec.rb +25 -10
  28. data/spec/{support/aliases.rb → simple_roles/many/persistence_spec.rb} +0 -0
  29. data/spec/simple_roles/many_spec.rb +186 -0
  30. data/spec/simple_roles/one_spec.rb +77 -0
  31. data/spec/spec_helper.rb +40 -21
  32. data/spec/support/controller_macros.rb +1 -18
  33. data/spec/support/database.yml +1 -1
  34. data/spec/support/factories.rb +6 -17
  35. data/spec/support/migrations/010_create_one_users.rb +16 -0
  36. data/spec/support/migrations/{010_create_users.rb → 011_create_users.rb} +3 -3
  37. data/spec/support/{fixtures/models → models}/.gitkeep +0 -0
  38. data/spec/support/models/one_user.rb +2 -0
  39. data/spec/support/{fixtures/models → models}/user.rb +0 -1
  40. data/spec/support/setup_roles.rb +5 -0
  41. data/spec/support/transaction.rb +30 -0
  42. data/spec/transaction_spec.rb +15 -0
  43. metadata +176 -137
  44. data/lib/simple_roles/base.rb +0 -111
  45. data/lib/simple_roles/roles_array.rb +0 -63
  46. data/spec/integration/messages_spec.rb +0 -62
  47. data/spec/simple_roles/base_spec.rb +0 -191
  48. data/spec/support/rspec_helpers.rb +0 -22
data/simple_roles.gemspec CHANGED
@@ -4,19 +4,20 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{simple_roles}
8
- s.version = "0.0.5"
7
+ s.name = "simple_roles"
8
+ s.version = "0.0.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = [%q{stanislaw}]
12
- s.date = %q{2011-10-14}
13
- s.description = %q{Simple Role System for Rails Apps}
14
- s.email = %q{s.pankevich@gmail.com}
11
+ s.authors = ["stanislaw"]
12
+ s.date = "2012-06-27"
13
+ s.description = "Simple Role System for Rails Apps"
14
+ s.email = "s.pankevich@gmail.com"
15
15
  s.extra_rdoc_files = [
16
16
  "README.md"
17
17
  ]
18
18
  s.files = [
19
19
  ".rspec",
20
+ "CHANGELOG.md",
20
21
  "Gemfile",
21
22
  "MIT-LICENSE",
22
23
  "README.md",
@@ -36,11 +37,16 @@ Gem::Specification.new do |s|
36
37
  "db/migrate/001_create_user_roles.rb",
37
38
  "db/migrate/002_create_roles.rb",
38
39
  "lib/simple_roles.rb",
39
- "lib/simple_roles/base.rb",
40
40
  "lib/simple_roles/configuration.rb",
41
41
  "lib/simple_roles/engine.rb",
42
42
  "lib/simple_roles/macros.rb",
43
- "lib/simple_roles/roles_array.rb",
43
+ "lib/simple_roles/many.rb",
44
+ "lib/simple_roles/many/persistence.rb",
45
+ "lib/simple_roles/many/roles_methods.rb",
46
+ "lib/simple_roles/one.rb",
47
+ "lib/simple_roles/one/persistence.rb",
48
+ "lib/simple_roles/one/roles_methods.rb",
49
+ "lib/simple_roles/packager.rb",
44
50
  "lib/simple_roles/version.rb",
45
51
  "lib/tasks/simple_roles_tasks.rake",
46
52
  "script/rails",
@@ -125,25 +131,31 @@ Gem::Specification.new do |s|
125
131
  "spec/dummy/test/unit/user_test.rb",
126
132
  "spec/dummy_spec_helper.rb",
127
133
  "spec/integration/main_spec.rb",
128
- "spec/integration/messages_spec.rb",
129
134
  "spec/integration/requests/main_spec.rb",
130
- "spec/simple_roles/base_spec.rb",
135
+ "spec/simple_roles/configuration_spec.rb",
136
+ "spec/simple_roles/integration_many_spec.rb",
131
137
  "spec/simple_roles/macros_spec.rb",
138
+ "spec/simple_roles/many/persistence_spec.rb",
139
+ "spec/simple_roles/many_spec.rb",
140
+ "spec/simple_roles/one_spec.rb",
132
141
  "spec/spec_helper.rb",
133
- "spec/support/aliases.rb",
134
142
  "spec/support/controller_macros.rb",
135
143
  "spec/support/database.yml",
136
144
  "spec/support/factories.rb",
137
- "spec/support/fixtures/models/.gitkeep",
138
- "spec/support/fixtures/models/user.rb",
139
- "spec/support/migrations/010_create_users.rb",
140
- "spec/support/rspec_helpers.rb"
145
+ "spec/support/migrations/010_create_one_users.rb",
146
+ "spec/support/migrations/011_create_users.rb",
147
+ "spec/support/models/.gitkeep",
148
+ "spec/support/models/one_user.rb",
149
+ "spec/support/models/user.rb",
150
+ "spec/support/setup_roles.rb",
151
+ "spec/support/transaction.rb",
152
+ "spec/transaction_spec.rb"
141
153
  ]
142
- s.homepage = %q{http://github.com/stanislaw/simple_roles}
143
- s.licenses = [%q{MIT}]
144
- s.require_paths = [%q{lib}]
145
- s.rubygems_version = %q{1.8.8}
146
- s.summary = %q{Rails Engine providing Role System for Rails apps}
154
+ s.homepage = "http://github.com/stanislaw/simple_roles"
155
+ s.licenses = ["MIT"]
156
+ s.require_paths = ["lib"]
157
+ s.rubygems_version = "1.8.19"
158
+ s.summary = "Rails Engine providing Role System for Rails apps"
147
159
 
148
160
  if s.respond_to? :specification_version then
149
161
  s.specification_version = 3
@@ -152,15 +164,13 @@ Gem::Specification.new do |s|
152
164
  s.add_runtime_dependency(%q<require_all>, [">= 0"])
153
165
  s.add_runtime_dependency(%q<sugar-high>, [">= 0"])
154
166
  s.add_runtime_dependency(%q<sweetloader>, [">= 0"])
155
- s.add_development_dependency(%q<activerecord>, [">= 0"])
167
+ s.add_runtime_dependency(%q<activerecord>, [">= 0"])
156
168
  s.add_development_dependency(%q<rake-kit>, [">= 0"])
157
169
  s.add_development_dependency(%q<devise>, [">= 0"])
158
170
  s.add_development_dependency(%q<mysql2>, [">= 0"])
159
171
  s.add_development_dependency(%q<jeweler>, [">= 0"])
160
172
  s.add_development_dependency(%q<cutter>, [">= 0"])
161
- s.add_development_dependency(%q<rails>, [">= 0"])
162
- s.add_development_dependency(%q<rspec-rails>, [">= 0"])
163
- s.add_development_dependency(%q<capybara>, [">= 0"])
173
+ s.add_development_dependency(%q<rspec>, [">= 0"])
164
174
  s.add_development_dependency(%q<factory_girl>, [">= 0"])
165
175
  else
166
176
  s.add_dependency(%q<require_all>, [">= 0"])
@@ -172,9 +182,7 @@ Gem::Specification.new do |s|
172
182
  s.add_dependency(%q<mysql2>, [">= 0"])
173
183
  s.add_dependency(%q<jeweler>, [">= 0"])
174
184
  s.add_dependency(%q<cutter>, [">= 0"])
175
- s.add_dependency(%q<rails>, [">= 0"])
176
- s.add_dependency(%q<rspec-rails>, [">= 0"])
177
- s.add_dependency(%q<capybara>, [">= 0"])
185
+ s.add_dependency(%q<rspec>, [">= 0"])
178
186
  s.add_dependency(%q<factory_girl>, [">= 0"])
179
187
  end
180
188
  else
@@ -187,9 +195,7 @@ Gem::Specification.new do |s|
187
195
  s.add_dependency(%q<mysql2>, [">= 0"])
188
196
  s.add_dependency(%q<jeweler>, [">= 0"])
189
197
  s.add_dependency(%q<cutter>, [">= 0"])
190
- s.add_dependency(%q<rails>, [">= 0"])
191
- s.add_dependency(%q<rspec-rails>, [">= 0"])
192
- s.add_dependency(%q<capybara>, [">= 0"])
198
+ s.add_dependency(%q<rspec>, [">= 0"])
193
199
  s.add_dependency(%q<factory_girl>, [">= 0"])
194
200
  end
195
201
  end
@@ -3,7 +3,7 @@ development:
3
3
  host: localhost
4
4
  database: simple_roles_development
5
5
  username: root
6
- password: root
6
+ password:
7
7
  encoding: utf8
8
8
 
9
9
  test:
@@ -11,16 +11,5 @@ test:
11
11
  host: localhost
12
12
  database: simple_roles_test
13
13
  username: root
14
- password: root
14
+ password:
15
15
  encoding: utf8
16
-
17
- production:
18
- adapter: mysql2
19
- host: localhost
20
- port: 5432
21
- database: simple_roles_production
22
- username: pgsql
23
- password: pgsql
24
- schema_search_path: public
25
- encoding: utf8
26
- template: template0
@@ -13,9 +13,6 @@ Dummy::Application.configure do
13
13
  config.consider_all_requests_local = true
14
14
  config.action_controller.perform_caching = false
15
15
 
16
- # Don't care if the mailer can't send
17
- config.action_mailer.raise_delivery_errors = false
18
-
19
16
  # Print deprecation notices to the Rails logger
20
17
  config.active_support.deprecation = :log
21
18
 
@@ -1,3 +1,4 @@
1
1
  SimpleRoles.configure do |config|
2
- config.valid_roles = [:user, :admin, :editor]
2
+ strategy :many
3
+ valid_roles :user, :admin, :editor
3
4
  end
@@ -1,7 +1,7 @@
1
- require 'dummy_spec_helper'
1
+ require 'spec_helper'
2
2
 
3
- describe "" do
4
- it "truth" do
5
- Rails.application.should be_kind_of(Dummy::Application)
6
- end
7
- end
3
+ # describe "" do
4
+ # pending "truth" do
5
+ # Rails.application.should be_kind_of(Dummy::Application)
6
+ # end
7
+ # end
@@ -1,21 +1,20 @@
1
- require 'dummy_spec_helper'
1
+ require 'spec_helper'
2
2
 
3
- describe "Requests" do
4
- it "truth" do
5
- Rails.application.should be_kind_of(Dummy::Application)
6
- end
3
+ # describe "Requests" do
4
+ # pending "truth" do
5
+ # Rails.application.should be_kind_of(Dummy::Application)
6
+ # end
7
7
 
8
- describe "Basic pages" do
9
- before do
10
- login_as(User.first)
11
- end
8
+ # pending "Basic pages" do
9
+ # before do
10
+ # login_as(User.first)
11
+ # end
12
12
 
13
- it "should get root" do
14
- pending
13
+ # it "should get root" do
14
+ # pending
15
15
 
16
- get '/'
17
- response.status.should be(200)
18
- end
19
-
20
- end
21
- end
16
+ # get '/'
17
+ # response.status.should be(200)
18
+ # end
19
+ # end
20
+ # end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleRoles::Configuration do
4
+ subject { SimpleRoles::Configuration }
5
+
6
+ its(:default_strategy) { should == :one }
7
+ its(:strategy) { should == :one }
8
+ its(:strategy_class) { should == SimpleRoles::One }
9
+
10
+ describe ".strategy" do
11
+ it "should retrieve current strategy if no argument passed" do
12
+ subject.strategy.should == :one
13
+ end
14
+
15
+ it "should set current strategy if strategy passed as argument" do
16
+ subject.strategy :many
17
+ subject.strategy.should == :many
18
+ end
19
+
20
+ it "should not allow wrong strategies" do
21
+ expect {
22
+ subject.strategy :wrong!
23
+ }.to raise_error
24
+ end
25
+ end
26
+
27
+ describe ".strategy=" do
28
+ it "should set current strategy if strategy passed as argument" do
29
+ subject.strategy = :one
30
+ subject.strategy.should == :one
31
+
32
+ subject.strategy = :many
33
+ subject.strategy.should == :many
34
+ end
35
+
36
+ it "should not allow wrong strategies" do
37
+ expect {
38
+ subject.strategy = :wrong!
39
+ }.to raise_error
40
+ end
41
+ end
42
+
43
+ describe ".valid_roles=" do
44
+ it "should set valid roles" do
45
+ valid_roles_before = SimpleRoles::Configuration.valid_roles
46
+
47
+ SimpleRoles::Configuration.valid_roles = [:user]
48
+ SimpleRoles::Configuration.valid_roles.should == [:user]
49
+
50
+ SimpleRoles::Configuration.valid_roles = valid_roles_before
51
+ SimpleRoles::Configuration.valid_roles.should == valid_roles_before
52
+ end
53
+ end
54
+
55
+ describe ".valid_roles" do
56
+ it "should also set valid roles if they passed as arg" do
57
+ valid_roles_before = SimpleRoles::Configuration.valid_roles
58
+
59
+ SimpleRoles::Configuration.valid_roles :user
60
+ SimpleRoles::Configuration.valid_roles.should == [:user]
61
+
62
+ SimpleRoles::Configuration.valid_roles valid_roles_before
63
+ SimpleRoles::Configuration.valid_roles.should == valid_roles_before
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ SimpleRoles.configure do |config|
4
+ config.valid_roles = [:user, :admin, :editor]
5
+ end
6
+
7
+ describe 'Integration for SimpleRoles::Many' do
8
+ before do
9
+ setup_roles
10
+ SimpleRoles::Packager.package User, :many
11
+ end
12
+
13
+ it "should all work" do
14
+ admin_role = Role.find_by_name("admin")
15
+ user = User.new(:name => "stanislaw")
16
+ user.roles_list.should be_empty
17
+ user.has_any_role?(:admin).should be_false
18
+ user.roles = [ :admin ]
19
+ user.roles_list.should include(:admin)
20
+ user.roles.should include(:admin)
21
+ user.has_role?(:admin).should be_true
22
+ user.admin?.should be_true
23
+ user.is_admin?.should be_true
24
+ user.has_roles?(:admin).should be_true
25
+ user.save!
26
+ user.roles.should include(:admin)
27
+ user = User.find_by_name! "stanislaw"
28
+ user.roles.should include(:admin)
29
+ user.remove_role(:admin)
30
+ user.roles.should be_empty
31
+ user.save!
32
+ user.roles.should be_empty
33
+ user.roles = [:admin, :user]
34
+ user.roles.should == Array.new([:admin, :user])
35
+ user.has_role?(:admin, :user).should be_true
36
+ user.has_roles?([:admin, :user]).should be_true
37
+ user.roles.size.should == 2
38
+ user.roles = [:admin]
39
+ user.roles.should include(:admin)
40
+ user.add_role :user
41
+ user.roles.should include(:user, :admin)
42
+ user.has_any_role?(:user).should be_true
43
+ user.has_any_role?(:user, :admin).should be_true
44
+ user.has_any_role?([:user, :admin])
45
+ user.has_any_role?(:blip).should be_false
46
+ end
47
+ end
@@ -1,25 +1,40 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe SimpleRoles::Base do
4
-
3
+ describe 'SimpleRoles Macros' do
4
+
5
5
  context "Macros availability" do
6
6
  subject { Module }
7
- before {
8
- require 'simple_roles'
9
- }
10
-
11
- specify { should be_kind_of(SimpleRoles::Macros) }
7
+ before { require 'simple_roles' }
8
+ specify { should be_kind_of SimpleRoles::Macros }
12
9
  end
13
10
 
14
- context "after applying macros" do
11
+ context "When Macros is applied" do
15
12
  subject { User }
13
+
14
+ specify { should be_kind_of SimpleRoles::Macros }
15
+
16
16
  before do
17
17
  class User < ActiveRecord::Base
18
- simple_roles
18
+ simple_roles do
19
+ strategy :many
20
+ end
21
+ end
22
+ end
23
+
24
+ context "Changes in User" do
25
+ specify { should include SimpleRoles::Many::RolesMethods }
26
+ specify { should include SimpleRoles::Many::RolesMethods }
27
+
28
+ [:roles, :roles_list, :add_role, :roles=, :remove_role].each do |meth|
29
+ specify { subject.new.should respond_to meth }
19
30
  end
20
31
  end
21
32
 
22
- specify { should be_kind_of(SimpleRoles::Macros) }
33
+ context "Changes in SimpleRoles::Configuration" do
34
+ it "should set strategy" do
35
+ SimpleRoles::Configuration.strategy.should == :many
36
+ end
37
+ end
23
38
  end
24
39
 
25
40
  end
@@ -0,0 +1,186 @@
1
+ require 'spec_helper'
2
+
3
+ SimpleRoles.configure do |config|
4
+ config.valid_roles = [:user, :admin, :editor]
5
+ end
6
+
7
+ describe SimpleRoles::Many do
8
+ let(:user) { create :user }
9
+
10
+ before(:each) do
11
+ setup_roles
12
+ SimpleRoles.package User, :many
13
+ end
14
+
15
+ context "Class Methods" do
16
+ subject { User }
17
+
18
+ specify { should respond_to(:valid_roles) }
19
+ its(:valid_roles) { should include(:user, :admin)}
20
+ end
21
+
22
+ context "Instance methods" do
23
+ subject { User.new }
24
+
25
+ [:roles, :roles_list].each do |meth|
26
+ specify { should respond_to(meth) }
27
+ its(:"#{meth}") { should be_empty }
28
+ end
29
+ end
30
+
31
+ context "Read API" do
32
+ subject do
33
+ user
34
+ end
35
+
36
+ it "#has_role?, #has_roles?" do
37
+ subject.roles = [:admin]
38
+
39
+ subject.has_role?(:admin).should be_true
40
+ subject.has_role?(:admin, :user).should be_false
41
+ subject.has_roles?(:editor).should be_false
42
+
43
+ subject.roles = [ :admin, :user ]
44
+
45
+ subject.has_role?(:admin, :user).should be_true
46
+ subject.has_role?([:admin, :user]).should be_true
47
+ end
48
+
49
+ it "#admin?, #user?, #editor? ..." do
50
+ subject.roles = [ :admin ]
51
+ subject.admin?.should be_true
52
+ subject.is_admin?.should be_true
53
+ subject.user?.should be_false
54
+ subject.editor?.should be_false
55
+ subject.roles = [ :editor ]
56
+ subject.editor?.should be_true
57
+ end
58
+ end
59
+
60
+ context "Write API" do
61
+ subject do
62
+ user
63
+ end
64
+
65
+ it "#roles= should set roles" do
66
+ subject.roles = :admin
67
+ subject.roles.should == Array.new([:admin])
68
+ subject.roles = :user
69
+ subject.roles.should == Array.new([:user])
70
+ end
71
+
72
+ it "#roles= should set roles if array of strings passed (sh accept strings too!)" do
73
+ subject.roles = 'admin'
74
+ subject.roles.should == Array.new([:admin])
75
+ subject.roles = ['user', 'editor']
76
+ subject.roles.should == Array.new([:user, :editor])
77
+ end
78
+
79
+ it "#remove_roles should remove roles" do
80
+ subject.roles = [ :admin, :user, :editor ]
81
+
82
+ subject.roles.should == Array.new([:admin, :user, :editor])
83
+
84
+ subject.remove_roles :admin
85
+ subject.roles.should == Array.new([:user, :editor])
86
+
87
+ subject.remove_roles :admin, :user, :editor
88
+ subject.roles.should == Array.new([])
89
+ end
90
+ end
91
+
92
+ context "Integration for roles methods" do
93
+ it "should work when #flatten is called over #roles" do
94
+ user.roles = [ :admin ]
95
+
96
+ user.roles_list.should == Array.new([:admin])
97
+ user.roles_list.flatten.should == Array.new([:admin])
98
+ end
99
+
100
+ it "should add :roles to accessible_attributes if they are Whitelisted" do
101
+ user.roles = [ :admin ]
102
+
103
+ user.roles_list.should include(:admin)
104
+ user.save!
105
+ User.find_by_name!("stanislaw").should be_kind_of(User)
106
+ User.delete_all
107
+
108
+ User.attr_accessible :name
109
+
110
+ user = User.new(:name => "stanislaw")
111
+ user.roles = [ :admin ]
112
+ user.roles_list.should include(:admin)
113
+ user.save!
114
+ User.find_by_name!("stanislaw").should be_kind_of(User)
115
+ end
116
+
117
+ it "should not duplicate roles when adding" do
118
+ user.roles = [ :admin ]
119
+ user.roles.should == [ :admin ]
120
+
121
+ user.add_role :admin
122
+
123
+ user.roles.should == [ :admin ]
124
+ end
125
+ end
126
+
127
+ describe ".package" do
128
+ describe "Persistence" do
129
+ it "should set roles" do
130
+ user.roles = :admin
131
+ user.roles.should == [:admin]
132
+ end
133
+ end
134
+
135
+ describe "Roles methods" do
136
+ describe "#set_role" do
137
+ it "should set role" do
138
+ user.set_role(:admin)
139
+ user.roles.should == [:admin]
140
+ end
141
+
142
+ it "should persist role" do
143
+ user.set_role(:admin)
144
+ user.reload
145
+ user.roles.should == [:admin]
146
+ end
147
+ end
148
+
149
+ describe "Dynamic scopes" do
150
+ subject { User }
151
+ SimpleRoles.config.valid_roles.each do |r|
152
+ it { should respond_to :"#{r}s" }
153
+ it { should respond_to(:"#{r}s_ids") }
154
+
155
+ its(:"#{r}s") { should be_kind_of(Array) }
156
+ end
157
+ end
158
+
159
+ describe "Dynamic methods" do
160
+ SimpleRoles.config.valid_roles.each do |r|
161
+ specify { user.should respond_to :"#{r}?" }
162
+ end
163
+
164
+ describe "#user?, #admin?, ..." do
165
+ specify do
166
+ user.set_role(:admin)
167
+ user.admin?.should == true
168
+
169
+ user.set_role(:user)
170
+ user.user?.should == true
171
+ end
172
+ end
173
+
174
+ describe "#is_user?, #is_admin?, ..." do
175
+ specify do
176
+ user.set_role(:admin)
177
+ user.is_admin?.should == true
178
+
179
+ user.set_role(:user)
180
+ user.is_user?.should == true
181
+ end
182
+ end
183
+ end
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleRoles::One do
4
+ subject { SimpleRoles::One }
5
+ let(:user) { create :one_user, :role => 'user' }
6
+
7
+ describe ".package" do
8
+ before(:all) do
9
+ SimpleRoles::Packager.package OneUser, :one
10
+ end
11
+
12
+ describe "Persistence" do
13
+ it "should read role as symbol" do
14
+ user.update_attribute :role, 'user'
15
+ user.role.should == :user
16
+ end
17
+
18
+ it "should set roles" do
19
+ user.role = :admin
20
+ user.role.should == :admin
21
+ end
22
+
23
+ it "should persist roles" do
24
+ user.role = :admin
25
+ user.save
26
+ user.role.should == :admin
27
+ end
28
+ end
29
+
30
+ describe "Roles methods" do
31
+ describe "#set_role" do
32
+ it "should set role" do
33
+ user.set_role(:admin)
34
+ user.role.should == :admin
35
+ end
36
+
37
+ it "should persist role" do
38
+ user.set_role(:admin)
39
+ user.reload
40
+ user.role.should == :admin
41
+ end
42
+ end
43
+
44
+ describe "Dynamic scopes" do
45
+ SimpleRoles.config.valid_roles.each do |r|
46
+ specify { OneUser.should respond_to :"#{r}s" }
47
+ end
48
+ end
49
+
50
+ describe "Dynamic methods" do
51
+ SimpleRoles.config.valid_roles.each do |r|
52
+ specify { user.should respond_to :"#{r}?" }
53
+ end
54
+
55
+ describe "#user?, #admin?, ..." do
56
+ specify do
57
+ user.set_role(:admin)
58
+ user.admin?.should == true
59
+
60
+ user.set_role(:user)
61
+ user.user?.should == true
62
+ end
63
+ end
64
+
65
+ describe "#is_user?, #is_admin?, ..." do
66
+ specify do
67
+ user.set_role(:admin)
68
+ user.is_admin?.should == true
69
+
70
+ user.set_role(:user)
71
+ user.is_user?.should == true
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end