roles_active_record 0.2.5 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/Gemfile +13 -5
  2. data/Rakefile +13 -14
  3. data/VERSION +1 -1
  4. data/development.sqlite3 +0 -0
  5. data/lib/roles_active_record/role.rb +3 -0
  6. data/lib/roles_active_record/strategy/multi/many_roles.rb +38 -41
  7. data/lib/roles_active_record/strategy/multi/roles_mask.rb +22 -23
  8. data/lib/roles_active_record/strategy/multi.rb +54 -0
  9. data/lib/roles_active_record/strategy/shared.rb +26 -0
  10. data/lib/roles_active_record/strategy/single/admin_flag.rb +19 -18
  11. data/lib/roles_active_record/strategy/single/one_role.rb +35 -26
  12. data/lib/roles_active_record/strategy/single/role_string.rb +24 -19
  13. data/lib/roles_active_record/strategy/single.rb +34 -0
  14. data/roles_active_record.gemspec +54 -49
  15. data/spec/fixtures/many_roles_setup.rb +1 -1
  16. data/spec/fixtures/one_role_setup.rb +1 -1
  17. data/spec/migrations/many_roles/002_create_roles.rb +0 -1
  18. data/spec/migrations/roles_mask/005_add_roles_mask_to_users.rb +1 -1
  19. data/spec/roles_active_record/strategy/api.rb +214 -0
  20. data/spec/roles_active_record/strategy/api_examples.rb +213 -0
  21. data/spec/roles_active_record/strategy/multi/many_roles_spec.rb +27 -0
  22. data/spec/roles_active_record/strategy/multi/roles_mask_spec.rb +23 -0
  23. data/spec/roles_active_record/strategy/single/admin_flag_spec.rb +27 -0
  24. data/spec/roles_active_record/strategy/single/one_role_spec.rb +19 -0
  25. data/spec/roles_active_record/strategy/single/role_string_spec.rb +23 -0
  26. data/spec/roles_active_record/strategy/user_setup.rb +20 -0
  27. data/spec/spec_helper.rb +6 -1
  28. metadata +49 -63
  29. data/roles_for_ar.gemspec +0 -165
  30. data/spec/roles_active_record/admin_flag_spec.rb +0 -71
  31. data/spec/roles_active_record/many_roles_spec.rb +0 -69
  32. data/spec/roles_active_record/one_role_spec.rb +0 -68
  33. data/spec/roles_active_record/role_string_spec.rb +0 -68
  34. data/spec/roles_active_record/roles_mask_spec.rb +0 -68
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{roles_active_record}
8
- s.version = "0.2.5"
8
+ s.version = "0.3.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = %q{2010-10-11}
12
+ s.date = %q{2010-11-20}
13
13
  s.description = %q{Makes it easy to set a role strategy on your User model in Active Record}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -38,8 +38,11 @@ Gem::Specification.new do |s|
38
38
  "lib/roles_active_record/namespaces.rb",
39
39
  "lib/roles_active_record/role.rb",
40
40
  "lib/roles_active_record/strategy.rb",
41
+ "lib/roles_active_record/strategy/multi.rb",
41
42
  "lib/roles_active_record/strategy/multi/many_roles.rb",
42
43
  "lib/roles_active_record/strategy/multi/roles_mask.rb",
44
+ "lib/roles_active_record/strategy/shared.rb",
45
+ "lib/roles_active_record/strategy/single.rb",
43
46
  "lib/roles_active_record/strategy/single/admin_flag.rb",
44
47
  "lib/roles_active_record/strategy/single/one_role.rb",
45
48
  "lib/roles_active_record/strategy/single/role_string.rb",
@@ -47,7 +50,6 @@ Gem::Specification.new do |s|
47
50
  "lib/views/_single_role_selector.erb.html",
48
51
  "logging.log",
49
52
  "roles_active_record.gemspec",
50
- "roles_for_ar.gemspec",
51
53
  "sandbox/Rakefile",
52
54
  "sandbox/add_role_to_users_migration.erb",
53
55
  "sandbox/create_roles_migration.erb",
@@ -72,7 +74,6 @@ Gem::Specification.new do |s|
72
74
  "spec/migrations/role_string/002_add_role_string_to_users.rb",
73
75
  "spec/migrations/roles_mask/005_add_roles_mask_to_users.rb",
74
76
  "spec/migrations/users/001_create_users.rb",
75
- "spec/roles_active_record/admin_flag_spec.rb",
76
77
  "spec/roles_active_record/generators/roles_generator_spec.rb",
77
78
  "spec/roles_active_record/generators/roles_migration/admin_flag_spec.rb",
78
79
  "spec/roles_active_record/generators/roles_migration/many_roles_spec.rb",
@@ -80,10 +81,14 @@ Gem::Specification.new do |s|
80
81
  "spec/roles_active_record/generators/roles_migration/role_string_spec.rb",
81
82
  "spec/roles_active_record/generators/roles_migration/roles_mask_spec.rb",
82
83
  "spec/roles_active_record/generators/setup_generator_spec.rb",
83
- "spec/roles_active_record/many_roles_spec.rb",
84
- "spec/roles_active_record/one_role_spec.rb",
85
- "spec/roles_active_record/role_string_spec.rb",
86
- "spec/roles_active_record/roles_mask_spec.rb",
84
+ "spec/roles_active_record/strategy/api.rb",
85
+ "spec/roles_active_record/strategy/api_examples.rb",
86
+ "spec/roles_active_record/strategy/multi/many_roles_spec.rb",
87
+ "spec/roles_active_record/strategy/multi/roles_mask_spec.rb",
88
+ "spec/roles_active_record/strategy/single/admin_flag_spec.rb",
89
+ "spec/roles_active_record/strategy/single/one_role_spec.rb",
90
+ "spec/roles_active_record/strategy/single/role_string_spec.rb",
91
+ "spec/roles_active_record/strategy/user_setup.rb",
87
92
  "spec/spec_helper.rb",
88
93
  "tmp/rails/app/models/user.rb",
89
94
  "tmp/rails/config/routes.rb",
@@ -112,7 +117,6 @@ Gem::Specification.new do |s|
112
117
  "spec/migrations/role_string/002_add_role_string_to_users.rb",
113
118
  "spec/migrations/roles_mask/005_add_roles_mask_to_users.rb",
114
119
  "spec/migrations/users/001_create_users.rb",
115
- "spec/roles_active_record/admin_flag_spec.rb",
116
120
  "spec/roles_active_record/generators/roles_generator_spec.rb",
117
121
  "spec/roles_active_record/generators/roles_migration/admin_flag_spec.rb",
118
122
  "spec/roles_active_record/generators/roles_migration/many_roles_spec.rb",
@@ -120,10 +124,14 @@ Gem::Specification.new do |s|
120
124
  "spec/roles_active_record/generators/roles_migration/role_string_spec.rb",
121
125
  "spec/roles_active_record/generators/roles_migration/roles_mask_spec.rb",
122
126
  "spec/roles_active_record/generators/setup_generator_spec.rb",
123
- "spec/roles_active_record/many_roles_spec.rb",
124
- "spec/roles_active_record/one_role_spec.rb",
125
- "spec/roles_active_record/role_string_spec.rb",
126
- "spec/roles_active_record/roles_mask_spec.rb",
127
+ "spec/roles_active_record/strategy/api.rb",
128
+ "spec/roles_active_record/strategy/api_examples.rb",
129
+ "spec/roles_active_record/strategy/multi/many_roles_spec.rb",
130
+ "spec/roles_active_record/strategy/multi/roles_mask_spec.rb",
131
+ "spec/roles_active_record/strategy/single/admin_flag_spec.rb",
132
+ "spec/roles_active_record/strategy/single/one_role_spec.rb",
133
+ "spec/roles_active_record/strategy/single/role_string_spec.rb",
134
+ "spec/roles_active_record/strategy/user_setup.rb",
127
135
  "spec/spec_helper.rb"
128
136
  ]
129
137
 
@@ -132,48 +140,45 @@ Gem::Specification.new do |s|
132
140
  s.specification_version = 3
133
141
 
134
142
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
135
- s.add_development_dependency(%q<rspec>, [">= 2.0.0.rc"])
136
- s.add_development_dependency(%q<database_cleaner>, ["~> 0.5.2"])
137
- s.add_development_dependency(%q<generator-spec>, ["~> 0.6.5"])
138
- s.add_runtime_dependency(%q<activerecord>, ["~> 3.0.0"])
139
- s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0"])
140
- s.add_runtime_dependency(%q<arel>, ["~> 1.0.0"])
141
- s.add_runtime_dependency(%q<meta_where>, ["~> 0.9.3"])
142
- s.add_runtime_dependency(%q<sugar-high>, ["~> 0.2.11"])
143
+ s.add_development_dependency(%q<rspec>, [">= 2.0"])
144
+ s.add_development_dependency(%q<database_cleaner>, [">= 0.5"])
145
+ s.add_development_dependency(%q<generator-spec>, [">= 0.7.0"])
146
+ s.add_runtime_dependency(%q<activerecord>, ["~> 3.0"])
147
+ s.add_runtime_dependency(%q<activesupport>, ["~> 3.0"])
148
+ s.add_runtime_dependency(%q<arel>, ["~> 1.0"])
149
+ s.add_runtime_dependency(%q<meta_where>, [">= 0.9"])
150
+ s.add_runtime_dependency(%q<sugar-high>, ["~> 0.3.0"])
143
151
  s.add_runtime_dependency(%q<require_all>, ["~> 1.2.0"])
144
- s.add_runtime_dependency(%q<roles_generic>, ["~> 0.2.5"])
145
- s.add_runtime_dependency(%q<rails3_artifactor>, [">= 0.2.6"])
146
- s.add_runtime_dependency(%q<logging_assist>, [">= 0.1.5"])
147
- s.add_runtime_dependency(%q<migration_assist>, [">= 0.1.5"])
152
+ s.add_runtime_dependency(%q<roles_generic>, [">= 0.3.0"])
153
+ s.add_runtime_dependency(%q<rails3_artifactor>, ["~> 0.3.1"])
154
+ s.add_runtime_dependency(%q<logging_assist>, ["~> 0.1.6"])
148
155
  else
149
- s.add_dependency(%q<rspec>, [">= 2.0.0.rc"])
150
- s.add_dependency(%q<database_cleaner>, ["~> 0.5.2"])
151
- s.add_dependency(%q<generator-spec>, ["~> 0.6.5"])
152
- s.add_dependency(%q<activerecord>, ["~> 3.0.0"])
153
- s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
154
- s.add_dependency(%q<arel>, ["~> 1.0.0"])
155
- s.add_dependency(%q<meta_where>, ["~> 0.9.3"])
156
- s.add_dependency(%q<sugar-high>, ["~> 0.2.11"])
156
+ s.add_dependency(%q<rspec>, [">= 2.0"])
157
+ s.add_dependency(%q<database_cleaner>, [">= 0.5"])
158
+ s.add_dependency(%q<generator-spec>, [">= 0.7.0"])
159
+ s.add_dependency(%q<activerecord>, ["~> 3.0"])
160
+ s.add_dependency(%q<activesupport>, ["~> 3.0"])
161
+ s.add_dependency(%q<arel>, ["~> 1.0"])
162
+ s.add_dependency(%q<meta_where>, [">= 0.9"])
163
+ s.add_dependency(%q<sugar-high>, ["~> 0.3.0"])
157
164
  s.add_dependency(%q<require_all>, ["~> 1.2.0"])
158
- s.add_dependency(%q<roles_generic>, ["~> 0.2.5"])
159
- s.add_dependency(%q<rails3_artifactor>, [">= 0.2.6"])
160
- s.add_dependency(%q<logging_assist>, [">= 0.1.5"])
161
- s.add_dependency(%q<migration_assist>, [">= 0.1.5"])
165
+ s.add_dependency(%q<roles_generic>, [">= 0.3.0"])
166
+ s.add_dependency(%q<rails3_artifactor>, ["~> 0.3.1"])
167
+ s.add_dependency(%q<logging_assist>, ["~> 0.1.6"])
162
168
  end
163
169
  else
164
- s.add_dependency(%q<rspec>, [">= 2.0.0.rc"])
165
- s.add_dependency(%q<database_cleaner>, ["~> 0.5.2"])
166
- s.add_dependency(%q<generator-spec>, ["~> 0.6.5"])
167
- s.add_dependency(%q<activerecord>, ["~> 3.0.0"])
168
- s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
169
- s.add_dependency(%q<arel>, ["~> 1.0.0"])
170
- s.add_dependency(%q<meta_where>, ["~> 0.9.3"])
171
- s.add_dependency(%q<sugar-high>, ["~> 0.2.11"])
170
+ s.add_dependency(%q<rspec>, [">= 2.0"])
171
+ s.add_dependency(%q<database_cleaner>, [">= 0.5"])
172
+ s.add_dependency(%q<generator-spec>, [">= 0.7.0"])
173
+ s.add_dependency(%q<activerecord>, ["~> 3.0"])
174
+ s.add_dependency(%q<activesupport>, ["~> 3.0"])
175
+ s.add_dependency(%q<arel>, ["~> 1.0"])
176
+ s.add_dependency(%q<meta_where>, [">= 0.9"])
177
+ s.add_dependency(%q<sugar-high>, ["~> 0.3.0"])
172
178
  s.add_dependency(%q<require_all>, ["~> 1.2.0"])
173
- s.add_dependency(%q<roles_generic>, ["~> 0.2.5"])
174
- s.add_dependency(%q<rails3_artifactor>, [">= 0.2.6"])
175
- s.add_dependency(%q<logging_assist>, [">= 0.1.5"])
176
- s.add_dependency(%q<migration_assist>, [">= 0.1.5"])
179
+ s.add_dependency(%q<roles_generic>, [">= 0.3.0"])
180
+ s.add_dependency(%q<rails3_artifactor>, ["~> 0.3.1"])
181
+ s.add_dependency(%q<logging_assist>, ["~> 0.1.6"])
177
182
  end
178
183
  end
179
184
 
@@ -4,5 +4,5 @@ class User < ActiveRecord::Base
4
4
  strategy :many_roles, :default
5
5
  role_class :role
6
6
 
7
- valid_roles_are :admin, :guest
7
+ valid_roles_are :admin, :guest, :user
8
8
  end
@@ -4,5 +4,5 @@ class User < ActiveRecord::Base
4
4
  strategy :one_role, :default
5
5
  role_class :role
6
6
 
7
- valid_roles_are :admin, :guest
7
+ valid_roles_are :admin, :guest, :user
8
8
  end
@@ -1,7 +1,6 @@
1
1
  class CreateRoles < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :roles do |t|
4
- t.integer :role_id
5
4
  t.string :name
6
5
  t.timestamps
7
6
  end
@@ -1,7 +1,7 @@
1
1
  class AddRolesMaskToUsers < ActiveRecord::Migration
2
2
  def self.up
3
3
  change_table :users do |t|
4
- t.integer :roles_mask, :default => 1
4
+ t.integer :roles_mask, :default => 0
5
5
  end
6
6
  end
7
7
 
@@ -0,0 +1,214 @@
1
+ class Role
2
+ end
3
+
4
+ describe 'Roles Generic API' do
5
+ before :each do
6
+ default_user_setup
7
+ end
8
+
9
+ describe '#in_role' do
10
+ it "should return first user matching role" do
11
+ if User.respond_to? :in_role
12
+ User.in_role(:guest).first.name.should == 'Guest user'
13
+ User.in_role(:admin).first.name.should == 'Admin user'
14
+ end
15
+ end
16
+ end
17
+
18
+ describe '#in_roles' do
19
+ it "should return first user matching role" do
20
+ if User.respond_to? :in_roles
21
+ User.in_roles(:guest, :user).first.name.should == 'Guest user'
22
+ User.in_roles(:admin, :guest).should be_empty
23
+ end
24
+ end
25
+ end
26
+
27
+
28
+ # it "should be true that a User that includes Roles::Generic has a complete Roles::Generic interface" do
29
+ # # mutation API
30
+ # [:roles=, :role=, :add_roles, :add_role, :remove_role, :remove_roles, :exchange_roles, :exchange_role].each do |api_method|
31
+ # @admin_user.respond_to?(api_method).should be_true
32
+ # end
33
+ #
34
+ # # inspection API
35
+ # [:valid_role?, :valid_roles?, :has_roles?, :has_role?, :has?, :is?, :roles, :roles_list, :admin?].each do |api_method|
36
+ # @admin_user.respond_to?(api_method).should be_true
37
+ # end
38
+ #
39
+ # # class method API
40
+ # [:valid_role?, :valid_roles?, :valid_roles].each do |class_api_method|
41
+ # @admin_user.class.respond_to?(class_api_method).should be_true
42
+ # end
43
+ # end
44
+ #
45
+ # describe '#valid_role?' do
46
+ # it "should be true that the admin user has a valid role of :guest" do
47
+ # # @admin_user.valid_role?(:guest).should be_true
48
+ # end
49
+ #
50
+ # it "should be true that the User class has a valid role of :guest" do
51
+ # # User.valid_role?(:guest).should be_true
52
+ # end
53
+ # end
54
+
55
+ # describe '#valid_roles' do
56
+ # it "should be true that the admin user has a valid role of :guest" do
57
+ # # @admin_user.valid_roles.should include(:guest, :admin)
58
+ # end
59
+ #
60
+ # it "should be true that the User class has a valid role of :guest" do
61
+ # User.valid_roles.should include(:guest, :admin)
62
+ # end
63
+ # end
64
+ #
65
+ # describe '#valid_roles?' do
66
+ # it "should be true that the admin user has a valid role of :guest" do
67
+ # @admin_user.valid_roles?(:guest, :admin).should be_true
68
+ # end
69
+ #
70
+ # it "should be true that the User class has a valid role of :guest" do
71
+ # User.valid_roles?(:guest, :admin).should be_true
72
+ # end
73
+ # end
74
+ #
75
+ # describe '#has_role?' do
76
+ # it "should have admin user role to :admin and not to :user" do
77
+ # @admin_user.has_role?(:user).should be_false
78
+ # @admin_user.has_role?(:admin).should be_true
79
+ # end
80
+ #
81
+ # it "should be true that guest user has role :user and not :admin" do
82
+ # @guest_user.has_role?(:guest).should be_true
83
+ # @guest_user.has_role?(:admin).should be_false
84
+ # end
85
+ # end
86
+ #
87
+ # describe '#has?' do
88
+ # it "should be true that the admin_user has the :admin role" do
89
+ # @admin_user.has?(:admin).should be_true
90
+ # end
91
+ #
92
+ # it "should NOT be true that the admin_user has the :admin role" do
93
+ # @guest_user.has?(:admin).should be_false
94
+ # end
95
+ # end
96
+ #
97
+ # describe '#has_roles?' do
98
+ # it "should be true that the admin_user has the roles :admin" do
99
+ # @admin_user.has_roles?(:admin).should be_true
100
+ # end
101
+ #
102
+ # it "should NOT be true that the user has the roles :admin" do
103
+ # @guest_user.has_roles?(:admin).should be_false
104
+ # end
105
+ # end
106
+ #
107
+ # describe '#roles_list' do
108
+ # it "should be true that the first role of admin_user is the :admin role" do
109
+ # @admin_user.roles_list.should include(:admin)
110
+ # end
111
+ #
112
+ # it "should be true that the first role of admin_user is the :user role" do
113
+ # case @normal_user.class.role_strategy.multiplicity
114
+ # when :single
115
+ # if @normal_user.class.role_strategy.name == :admin_flag
116
+ # @normal_user.roles_list.should include(:guest)
117
+ # else
118
+ # @normal_user.roles_list.should include(:user)
119
+ # end
120
+ # when :multi
121
+ # @normal_user.roles_list.should include(:user, :guest)
122
+ # end
123
+ # end
124
+ # end
125
+ #
126
+ # describe '#roles' do
127
+ # it "should be true that the roles of admin_user is an array with the role :admin" do
128
+ # roles = @admin_user.roles
129
+ # if roles.kind_of? Role
130
+ # roles.name.to_sym.should == :admin
131
+ # elsif roles.kind_of? Array
132
+ # if @normal_user.class.role_strategy.type == :complex
133
+ # roles.first.name.to_sym.should == :admin
134
+ # else
135
+ # roles.first.to_sym.should == :admin
136
+ # end
137
+ # else
138
+ # roles.to_sym.should == :admin
139
+ # end
140
+ # end
141
+ # end
142
+ #
143
+ # describe '#admin?' do
144
+ # it "should be true that admin_user is in the :admin role" do
145
+ # @admin_user.admin?.should be_true
146
+ # end
147
+ #
148
+ # it "should NOT be true that the user is in the :admin role" do
149
+ # @guest_user.admin?.should be_false
150
+ # end
151
+ # end
152
+ #
153
+ # describe '#is?' do
154
+ # it "should be true that admin_user is in the :admin role" do
155
+ # @admin_user.is?(:admin).should be_true
156
+ # end
157
+ #
158
+ # it "should NOT be true that the user is in the :admin role" do
159
+ # @guest_user.is?(:admin).should be_false
160
+ # end
161
+ # end
162
+ #
163
+ # describe '#roles=' do
164
+ # it "should set user role to :admin" do
165
+ # @guest_user.roles = :admin
166
+ # @guest_user.has_role?(:admin).should be_true
167
+ # @guest_user.roles = :guest
168
+ # end
169
+ # end
170
+ #
171
+ # describe '#exchange_roles' do
172
+ # it "should exchange user role :user with role :admin" do
173
+ # @guest_user.exchange_role :guest, :with => :admin
174
+ # @guest_user.has?(:guest).should be_false
175
+ # @guest_user.has?(:admin).should be_true
176
+ # end
177
+ #
178
+ # it "should exchange user role :admin with roles :user and :guest" do
179
+ # @admin_user.exchange_role :admin, :with => [:user, :guest]
180
+ # puts "Role: #{@admin_user.role}"
181
+ # @admin_user.has?(:admin).should be_false
182
+ #
183
+ # case @admin_user.class.role_strategy.multiplicity
184
+ # when :single
185
+ # if @normal_user.class.role_strategy.name == :admin_flag
186
+ # @admin_user.has?(:admin).should be_false
187
+ # @admin_user.has?(:guest).should be_true
188
+ # else
189
+ # @admin_user.has?(:user).should be_true
190
+ # @admin_user.has?(:guest).should be_false
191
+ # @admin_user.has?(:admin).should be_false
192
+ # end
193
+ # when :multi
194
+ # @admin_user.has_role?(:user).should be_true
195
+ # @admin_user.has_role?(:guest).should be_true
196
+ # @admin_user.has?(:admin).should be_false
197
+ # end
198
+ # end
199
+ # end
200
+ #
201
+ # describe '#remove_roles' do
202
+ # it "should remove user role :admin using #remove_roles" do
203
+ # @admin_user.remove_roles :admin
204
+ # @admin_user.has_role?(:admin).should_not be_true
205
+ # end
206
+ #
207
+ # it "should remove user role :admin using #remove_role" do
208
+ # @guest_user.add_role :admin
209
+ # @guest_user.has_role?(:admin).should be_true
210
+ # @guest_user.remove_role :admin
211
+ # @guest_user.has_role?(:admin).should_not be_true
212
+ # end
213
+ # end
214
+ end
@@ -0,0 +1,213 @@
1
+ describe "Roles for Active Record: #{api_name}" do
2
+ before do
3
+ api_migrate
4
+ end
5
+
6
+ before :each do
7
+ api_fixture
8
+ end
9
+
10
+ require "roles_active_record/strategy/user_setup.rb"
11
+
12
+ before do
13
+ default_user_setup
14
+ end
15
+
16
+ describe '#in_role' do
17
+ it "should return first user matching role" do
18
+ if User.respond_to? :in_role
19
+ User.in_role(:guest).first.name.should == 'Guest user'
20
+ User.in_role(:admin).first.name.should == 'Admin user'
21
+ end
22
+ end
23
+ end
24
+
25
+ describe '#in_any_role' do
26
+ it "should return first user matching role" do
27
+ if User.respond_to? :in_roles
28
+ User.in_any_role(:guest, :user).first.name.should == 'Guest user'
29
+ User.in_any_role(:admin, :guest).should be_empty
30
+ end
31
+ end
32
+ end
33
+
34
+ it "should be true that a User that includes Roles::Generic has a complete Roles::Generic interface" do
35
+ # mutation API
36
+ [:roles=, :role=, :add_roles, :add_role, :remove_role, :remove_roles, :exchange_roles, :exchange_role].each do |api_method|
37
+ @admin_user.respond_to?(api_method).should be_true
38
+ end
39
+
40
+ # inspection API
41
+ [:valid_role?, :valid_roles?, :has_roles?, :has_role?, :has?, :is?, :roles, :roles_list, :admin?].each do |api_method|
42
+ @admin_user.respond_to?(api_method).should be_true
43
+ end
44
+
45
+ # class method API
46
+ [:valid_role?, :valid_roles?, :valid_roles].each do |class_api_method|
47
+ @admin_user.class.respond_to?(class_api_method).should be_true
48
+ end
49
+ end
50
+
51
+ describe '#valid_role?' do
52
+ it "should be true that the admin user has a valid role of :guest" do
53
+ # @admin_user.valid_role?(:guest).should be_true
54
+ end
55
+
56
+ it "should be true that the User class has a valid role of :guest" do
57
+ # User.valid_role?(:guest).should be_true
58
+ end
59
+ end
60
+
61
+ describe '#valid_roles' do
62
+ it "should be true that the admin user has a valid role of :guest" do
63
+ # @admin_user.valid_roles.should include(:guest, :admin)
64
+ end
65
+
66
+ it "should be true that the User class has a valid role of :guest" do
67
+ User.valid_roles.should include(:guest, :admin)
68
+ end
69
+ end
70
+
71
+ describe '#valid_roles?' do
72
+ it "should be true that the admin user has a valid role of :guest" do
73
+ @admin_user.valid_roles?(:guest, :admin).should be_true
74
+ end
75
+
76
+ it "should be true that the User class has a valid role of :guest" do
77
+ User.valid_roles?(:guest, :admin).should be_true
78
+ end
79
+ end
80
+
81
+ describe '#has_role?' do
82
+ it "should have admin user role to :admin and not to :user" do
83
+ @admin_user.has_role?(:user).should be_false
84
+ @admin_user.has_role?(:admin).should be_true
85
+ end
86
+
87
+ it "should be true that guest user has role :guest and not :admin" do
88
+ puts "Guest user: #{@guest_user.roles_list}"
89
+ @guest_user.has_role?(:guest).should be_true
90
+ @guest_user.has_role?(:admin).should be_false
91
+ end
92
+ end
93
+
94
+ describe '#has?' do
95
+ it "should be true that the admin_user has the :admin role" do
96
+ @admin_user.has?(:admin).should be_true
97
+ end
98
+
99
+ it "should NOT be true that the admin_user has the :admin role" do
100
+ @guest_user.has?(:admin).should be_false
101
+ end
102
+ end
103
+
104
+ describe '#has_roles?' do
105
+ it "should be true that the admin_user has the roles :admin" do
106
+ # @admin_user.has_roles?(:admin).should be_true
107
+ end
108
+
109
+ it "should NOT be true that the user has the roles :admin" do
110
+ @guest_user.has_roles?(:admin).should be_false
111
+ end
112
+ end
113
+
114
+ describe '#roles_list' do
115
+ it "should be true that the first role of admin_user is the :admin role" do
116
+ @admin_user.roles_list.should include(:admin)
117
+ end
118
+
119
+ it "should be true that the first role of admin_user is the :user role" do
120
+ case @normal_user.class.role_strategy.multiplicity
121
+ when :single
122
+ #if @normal_user.class.role_strategy.name == :admin_flag
123
+ @normal_user.roles_list.should include(:guest)
124
+ # else
125
+ # @normal_user.roles_list.should include(:user)
126
+ #end
127
+ when :multi
128
+ puts "Norm: #{@normal_user.roles}"
129
+ @normal_user.roles_list.should include(:user, :guest)
130
+ end
131
+ end
132
+ end
133
+
134
+ describe '#roles' do
135
+ it "should be true that the roles of admin_user is an array with the role :admin" do
136
+ roles = @admin_user.roles
137
+ if defined?(Role) && roles.kind_of?(Role)
138
+ roles.name.to_sym.should == :admin
139
+ elsif roles.kind_of? Array
140
+ if @normal_user.class.role_strategy.type == :complex
141
+ roles.first.name.to_sym.should == :admin
142
+ end
143
+ if @normal_user.class.role_strategy.name == :admin_flag
144
+ roles.first.should == true
145
+ end
146
+ else
147
+ roles.to_sym.should == :admin
148
+ end
149
+ end
150
+ end
151
+
152
+ describe '#admin?' do
153
+ it "should be true that admin_user is in the :admin role" do
154
+ @admin_user.admin?.should be_true
155
+ end
156
+
157
+ it "should NOT be true that the user is in the :admin role" do
158
+ @guest_user.admin?.should be_false
159
+ end
160
+ end
161
+
162
+ describe '#is?' do
163
+ it "should be true that admin_user is in the :admin role" do
164
+ @admin_user.is?(:admin).should be_true
165
+ end
166
+
167
+ it "should NOT be true that the user is in the :admin role" do
168
+ @guest_user.is?(:admin).should be_false
169
+ end
170
+ end
171
+
172
+ describe '#roles=' do
173
+ it "should set user role to :admin" do
174
+ @guest_user.roles = :admin
175
+ @guest_user.has_role?(:admin).should be_true
176
+ @guest_user.roles = :guest
177
+ end
178
+ end
179
+
180
+ describe '#exchange_roles' do
181
+ it "should exchange user role :user with role :admin" do
182
+ @guest_user.exchange_role :guest, :with => :admin
183
+ @guest_user.has?(:guest).should be_false
184
+ @guest_user.has?(:admin).should be_true
185
+ end
186
+
187
+ it "should exchange user role :admin with roles :user and :guest" do
188
+ case @admin_user.class.role_strategy.multiplicity
189
+ when :single
190
+ lambda { @admin_user.exchange_role :admin, :with => [:user, :guest] }.should raise_error(ArgumentError)
191
+ when :multi
192
+ @admin_user.exchange_role :admin, :with => [:user, :guest]
193
+ @admin_user.has_role?(:user).should be_true
194
+ @admin_user.has_role?(:guest).should be_true
195
+ @admin_user.has?(:admin).should be_false
196
+ end
197
+ end
198
+ end
199
+
200
+ describe '#remove_roles' do
201
+ it "should remove user role :admin using #remove_roles" do
202
+ @admin_user.remove_roles :admin
203
+ @admin_user.has_role?(:admin).should_not be_true
204
+ end
205
+
206
+ it "should remove user role :admin using #remove_role" do
207
+ @guest_user.add_role :admin
208
+ @guest_user.has_role?(:admin).should be_true
209
+ @guest_user.remove_role :admin
210
+ @guest_user.has_role?(:admin).should_not be_true
211
+ end
212
+ end
213
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+ use_roles_strategy :many_roles
3
+
4
+ class User < ActiveRecord::Base
5
+ include Roles::ActiveRecord
6
+
7
+ strategy :many_roles, :default
8
+ valid_roles_are :admin, :guest, :user
9
+ end
10
+
11
+ def api_migrate
12
+ migrate('many_roles')
13
+ end
14
+
15
+ def api_fixture
16
+ load 'fixtures/many_roles_setup.rb'
17
+ end
18
+
19
+ def api_name
20
+ :many_roles
21
+ end
22
+
23
+ load 'roles_active_record/strategy/api_examples.rb'
24
+
25
+
26
+
27
+
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+ use_roles_strategy :roles_mask
3
+
4
+ class User < ActiveRecord::Base
5
+ include Roles::ActiveRecord
6
+
7
+ strategy :roles_mask, :default
8
+ valid_roles_are :admin, :guest, :user
9
+ end
10
+
11
+ def api_migrate
12
+ migrate('roles_mask')
13
+ end
14
+
15
+ def api_name
16
+ :roles_mask
17
+ end
18
+
19
+ load 'roles_active_record/strategy/api_examples.rb'
20
+
21
+
22
+
23
+