third-prestige-rolify 3.3.0.rc5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +17 -0
  4. data/CHANGELOG.rdoc +157 -0
  5. data/Gemfile +21 -0
  6. data/LICENSE +20 -0
  7. data/README.md +220 -0
  8. data/Rakefile +34 -0
  9. data/UPGRADE.rdoc +44 -0
  10. data/gemfiles/Gemfile.rails-3.2 +21 -0
  11. data/gemfiles/Gemfile.rails-4.0 +27 -0
  12. data/lib/generators/active_record/rolify_generator.rb +50 -0
  13. data/lib/generators/active_record/templates/README +8 -0
  14. data/lib/generators/active_record/templates/migration.rb +19 -0
  15. data/lib/generators/mongoid/rolify_generator.rb +51 -0
  16. data/lib/generators/mongoid/templates/README-mongoid +4 -0
  17. data/lib/generators/rolify/rolify_generator.rb +35 -0
  18. data/lib/generators/rolify/templates/README +13 -0
  19. data/lib/generators/rolify/templates/initializer.rb +8 -0
  20. data/lib/generators/rolify/templates/role-active_record.rb +11 -0
  21. data/lib/generators/rolify/templates/role-mongoid.rb +17 -0
  22. data/lib/generators/rolify/user_generator.rb +39 -0
  23. data/lib/rolify.rb +57 -0
  24. data/lib/rolify/adapters/active_record/resource_adapter.rb +26 -0
  25. data/lib/rolify/adapters/active_record/role_adapter.rb +86 -0
  26. data/lib/rolify/adapters/active_record/scopes.rb +27 -0
  27. data/lib/rolify/adapters/base.rb +60 -0
  28. data/lib/rolify/adapters/mongoid/resource_adapter.rb +27 -0
  29. data/lib/rolify/adapters/mongoid/role_adapter.rb +89 -0
  30. data/lib/rolify/adapters/mongoid/scopes.rb +27 -0
  31. data/lib/rolify/configure.rb +56 -0
  32. data/lib/rolify/dynamic.rb +21 -0
  33. data/lib/rolify/finders.rb +40 -0
  34. data/lib/rolify/matchers.rb +13 -0
  35. data/lib/rolify/railtie.rb +20 -0
  36. data/lib/rolify/resource.rb +31 -0
  37. data/lib/rolify/role.rb +85 -0
  38. data/lib/rolify/utils.rb +10 -0
  39. data/lib/rolify/version.rb +3 -0
  40. data/rolify.gemspec +30 -0
  41. data/spec/README.rdoc +24 -0
  42. data/spec/generators/rolify/rolify_activerecord_generator_spec.rb +163 -0
  43. data/spec/generators/rolify/rolify_mongoid_generator_spec.rb +112 -0
  44. data/spec/generators_helper.rb +21 -0
  45. data/spec/rolify/config_spec.rb +191 -0
  46. data/spec/rolify/custom_spec.rb +20 -0
  47. data/spec/rolify/matchers_spec.rb +24 -0
  48. data/spec/rolify/namespace_spec.rb +24 -0
  49. data/spec/rolify/resource_spec.rb +389 -0
  50. data/spec/rolify/resourcifed_and_rolifed_spec.rb +24 -0
  51. data/spec/rolify/role_spec.rb +20 -0
  52. data/spec/rolify/shared_contexts.rb +92 -0
  53. data/spec/rolify/shared_examples/shared_examples_for_add_role.rb +92 -0
  54. data/spec/rolify/shared_examples/shared_examples_for_callbacks.rb +65 -0
  55. data/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +151 -0
  56. data/spec/rolify/shared_examples/shared_examples_for_finders.rb +77 -0
  57. data/spec/rolify/shared_examples/shared_examples_for_has_all_roles.rb +71 -0
  58. data/spec/rolify/shared_examples/shared_examples_for_has_any_role.rb +71 -0
  59. data/spec/rolify/shared_examples/shared_examples_for_has_role.rb +135 -0
  60. data/spec/rolify/shared_examples/shared_examples_for_only_has_role.rb +174 -0
  61. data/spec/rolify/shared_examples/shared_examples_for_remove_role.rb +121 -0
  62. data/spec/rolify/shared_examples/shared_examples_for_roles.rb +102 -0
  63. data/spec/rolify/shared_examples/shared_examples_for_scopes.rb +38 -0
  64. data/spec/spec_helper.rb +30 -0
  65. data/spec/support/adapters/active_record.rb +76 -0
  66. data/spec/support/adapters/mongoid.rb +143 -0
  67. data/spec/support/adapters/mongoid.yml +6 -0
  68. data/spec/support/data.rb +25 -0
  69. data/spec/support/schema.rb +52 -0
  70. metadata +254 -0
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require "bundler/setup"
3
+
4
+ require 'rolify'
5
+ require 'rolify/matchers'
6
+ require 'rails/all'
7
+
8
+ require 'coveralls'
9
+ Coveralls.wear_merged!
10
+
11
+ module TestApp
12
+ class Application < ::Rails::Application
13
+ config.root = File.dirname(__FILE__)
14
+ end
15
+ end
16
+
17
+ require 'ammeter/init'
18
+
19
+ ENV['ADAPTER'] ||= 'active_record'
20
+
21
+
@@ -0,0 +1,191 @@
1
+ require "spec_helper"
2
+ require "active_record"
3
+ require "mongoid"
4
+
5
+ class ARUser < ActiveRecord::Base
6
+ extend Rolify
7
+ end
8
+
9
+ class MUser
10
+ include Mongoid::Document
11
+ extend Rolify
12
+ end
13
+
14
+ describe Rolify do
15
+ before do
16
+ Rolify.use_defaults
17
+ end
18
+
19
+ describe :dynamic_shortcuts do
20
+ context "using defaults values" do
21
+ subject { Rolify.dynamic_shortcuts }
22
+
23
+ it { should be_false }
24
+ end
25
+
26
+ context "using custom values" do
27
+ before do
28
+ Rolify.dynamic_shortcuts = true
29
+ end
30
+
31
+ subject { Rolify.dynamic_shortcuts }
32
+
33
+ it { should be_true }
34
+ end
35
+ end
36
+
37
+ describe :orm do
38
+ context "using defaults values" do
39
+ subject { Rolify.orm }
40
+
41
+ it { should eq("active_record") }
42
+
43
+ context "on the User class" do
44
+ before do
45
+ ARUser.rolify
46
+ end
47
+
48
+ subject { ARUser }
49
+
50
+ its("adapter.class") { should be(Rolify::Adapter::RoleAdapter) }
51
+ end
52
+
53
+ context "on the Forum class" do
54
+ before do
55
+ Forum.resourcify
56
+ end
57
+
58
+ subject { Forum }
59
+
60
+ its("adapter.class") { should be(Rolify::Adapter::ResourceAdapter) }
61
+ end
62
+ end
63
+
64
+ context "using custom values" do
65
+ context "using :orm setter method" do
66
+ before do
67
+ Rolify.orm = "mongoid"
68
+ end
69
+
70
+ subject { Rolify.orm }
71
+
72
+ it { should eq("mongoid") }
73
+
74
+ context "on the User class" do
75
+ before do
76
+ MUser.rolify
77
+ end
78
+
79
+ subject { MUser }
80
+
81
+ its("adapter.class") { should be(Rolify::Adapter::RoleAdapter) }
82
+ end
83
+
84
+ context "on the Forum class" do
85
+ before do
86
+ Forum.resourcify
87
+ end
88
+
89
+ subject { Forum }
90
+
91
+ its("adapter.class") { should be(Rolify::Adapter::ResourceAdapter) }
92
+ end
93
+ end
94
+
95
+ context "using :use_mongoid method" do
96
+ before do
97
+ Rolify.use_mongoid
98
+ end
99
+
100
+ subject { Rolify.orm }
101
+
102
+ it { should eq("mongoid") }
103
+
104
+ context "on the User class" do
105
+ before do
106
+ MUser.rolify
107
+ end
108
+
109
+ subject { MUser }
110
+
111
+ its("adapter.class") { should be(Rolify::Adapter::RoleAdapter) }
112
+ end
113
+
114
+ context "on the Forum class" do
115
+ before do
116
+ Forum.resourcify
117
+ end
118
+
119
+ subject { Forum }
120
+
121
+ its("adapter.class") { should be(Rolify::Adapter::ResourceAdapter) }
122
+ end
123
+ end
124
+ end
125
+
126
+ describe :dynamic_shortcuts do
127
+ context "using defaults values" do
128
+ subject { Rolify.dynamic_shortcuts }
129
+
130
+ it { should be_false }
131
+ end
132
+
133
+ context "using custom values" do
134
+ context "using :dynamic_shortcuts setter method" do
135
+ before do
136
+ Rolify.dynamic_shortcuts = true
137
+ end
138
+
139
+ subject { Rolify.dynamic_shortcuts }
140
+
141
+ it { should be_true }
142
+ end
143
+
144
+ context "using :use_dynamic_shortcuts method" do
145
+ before do
146
+ Rolify.use_dynamic_shortcuts
147
+ end
148
+
149
+ subject { Rolify.dynamic_shortcuts }
150
+
151
+ it { should be_true }
152
+ end
153
+ end
154
+ end
155
+ end
156
+
157
+ describe :configure do
158
+ before do
159
+ Rolify.configure do |config|
160
+ config.dynamic_shortcuts = true
161
+ config.orm = "mongoid"
162
+ end
163
+ end
164
+
165
+ its(:dynamic_shortcuts) { should be_true }
166
+ its(:orm) { should eq("mongoid") }
167
+
168
+ context "on the User class" do
169
+ before do
170
+ MUser.rolify
171
+ end
172
+
173
+ subject { MUser }
174
+
175
+ it { should satisfy { |u| u.include? Rolify::Role }}
176
+ it { should satisfy { |u| u.singleton_class.include? Rolify::Dynamic } }
177
+ its("adapter.class") { should be(Rolify::Adapter::RoleAdapter) }
178
+ end
179
+
180
+ context "on the Forum class" do
181
+ before do
182
+ Forum.resourcify
183
+ end
184
+
185
+ subject { Forum }
186
+
187
+ it { should satisfy { |u| u.include? Rolify::Resource }}
188
+ its("adapter.class") { should be(Rolify::Adapter::ResourceAdapter) }
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,20 @@
1
+ require "spec_helper"
2
+ require "rolify/shared_examples/shared_examples_for_roles"
3
+ require "rolify/shared_examples/shared_examples_for_dynamic"
4
+ require "rolify/shared_examples/shared_examples_for_scopes"
5
+ require "rolify/shared_examples/shared_examples_for_callbacks"
6
+
7
+ describe "Using Rolify with custom User and Role class names" do
8
+ def user_class
9
+ Customer
10
+ end
11
+
12
+ def role_class
13
+ Privilege
14
+ end
15
+
16
+ it_behaves_like Rolify::Role
17
+ it_behaves_like "Role.scopes"
18
+ it_behaves_like Rolify::Dynamic
19
+ it_behaves_like "Rolify.callbacks"
20
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'have_role', focus: true do
4
+ let(:object) { Object.new }
5
+
6
+ it 'delegates to has_role?' do
7
+ object.should_receive(:has_role?).with(:read, 'Resource') { true }
8
+ object.should have_role(:read, 'Resource')
9
+ end
10
+
11
+ it 'reports a nice failure message for should' do
12
+ object.should_receive(:has_role?) { false }
13
+ expect{
14
+ object.should have_role(:read, 'Resource')
15
+ }.to raise_error('expected to have role :read "Resource"')
16
+ end
17
+
18
+ it 'reports a nice failure message for should_not' do
19
+ object.should_receive(:has_role?) { true }
20
+ expect{
21
+ object.should_not have_role(:read, 'Resource')
22
+ }.to raise_error('expected not to have role :read "Resource"')
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+ require "rolify/shared_examples/shared_examples_for_roles"
3
+ require "rolify/shared_examples/shared_examples_for_dynamic"
4
+ require "rolify/shared_examples/shared_examples_for_scopes"
5
+ require "rolify/shared_examples/shared_examples_for_callbacks"
6
+
7
+ describe "Rolify.namespace" do
8
+ def user_class
9
+ Admin::Moderator
10
+ end
11
+
12
+ def role_class
13
+ Admin::Right
14
+ end
15
+
16
+ def join_table
17
+ "moderators_rights"
18
+ end
19
+
20
+ it_behaves_like Rolify::Role
21
+ it_behaves_like "Role.scopes"
22
+ it_behaves_like Rolify::Dynamic
23
+ it_behaves_like "Rolify.callbacks"
24
+ end
@@ -0,0 +1,389 @@
1
+ require "spec_helper"
2
+
3
+ describe Rolify::Resource do
4
+ before(:all) do
5
+ reset_defaults
6
+ User.rolify
7
+ Forum.resourcify
8
+ Group.resourcify
9
+ Team.resourcify
10
+ Role.destroy_all
11
+ end
12
+
13
+ # Users
14
+ let(:admin) { User.first }
15
+ let(:tourist) { User.last }
16
+ let(:captain) { User.where(:login => "god").first }
17
+
18
+ # roles
19
+ let!(:forum_role) { admin.add_role(:forum, Forum.first) }
20
+ let!(:godfather_role) { admin.add_role(:godfather, Forum) }
21
+ let!(:group_role) { admin.add_role(:group, Group.last) }
22
+ let!(:grouper_role) { admin.add_role(:grouper, Group.first) }
23
+ let!(:tourist_role) { tourist.add_role(:forum, Forum.last) }
24
+ let!(:sneaky_role) { tourist.add_role(:group, Forum.first) }
25
+ let!(:captain_role) { captain.add_role(:captain, Team.first) }
26
+ let!(:player_role) { captain.add_role(:player, Team.last) }
27
+
28
+ describe ".with_roles" do
29
+ subject { Group }
30
+
31
+ it { should respond_to(:find_roles).with(1).arguments }
32
+ it { should respond_to(:find_roles).with(2).arguments }
33
+
34
+ context "with a role name as argument" do
35
+ context "on the Forum class" do
36
+ subject { Forum }
37
+
38
+ it "should include Forum instances with forum role" do
39
+ subject.with_role(:forum).should =~ [ Forum.first, Forum.last ]
40
+ end
41
+
42
+ it "should include Forum instances with godfather role" do
43
+ subject.with_role(:godfather).should =~ Forum.all.to_a
44
+ end
45
+
46
+ it "should be able to modify the resource", :if => ENV['ADAPTER'] == 'active_record' do
47
+ forum_resource = subject.with_role(:forum).first
48
+ forum_resource.name = "modified name"
49
+ expect(forum_resource.save).not_to raise_error
50
+ end
51
+ end
52
+
53
+ context "on the Group class" do
54
+ subject { Group }
55
+
56
+ it "should include Group instances with group role" do
57
+ subject.with_role(:group).should =~ [ Group.last ]
58
+ end
59
+ end
60
+
61
+ context "on a Group instance" do
62
+ subject { Group.last }
63
+
64
+ it "should ignore nil entries" do
65
+ subject.subgroups.with_role(:group).should =~ [ ]
66
+ end
67
+ end
68
+ end
69
+
70
+ context "with an array of role names as argument" do
71
+ context "on the Group class" do
72
+ subject { Group }
73
+
74
+ it "should include Group instances with both group and grouper roles" do
75
+ subject.with_roles([:group, :grouper]).should =~ [ Group.first, Group.last ]
76
+ end
77
+ end
78
+ end
79
+
80
+ context "with a role name and a user as arguments" do
81
+ context "on the Forum class" do
82
+ subject { Forum }
83
+
84
+ it "should get all Forum instances binded to the forum role and the admin user" do
85
+ subject.with_role(:forum, admin).should =~ [ Forum.first ]
86
+ end
87
+
88
+ it "should get all Forum instances binded to the forum role and the tourist user" do
89
+ subject.with_role(:forum, tourist).should =~ [ Forum.last ]
90
+ end
91
+
92
+ it "should get all Forum instances binded to the godfather role and the admin user" do
93
+ subject.with_role(:godfather, admin).should =~ Forum.all.to_a
94
+ end
95
+
96
+ it "should get all Forum instances binded to the godfather role and the tourist user" do
97
+ subject.with_role(:godfather, tourist).should be_empty
98
+ end
99
+
100
+ it "should get Forum instances binded to the group role and the tourist user" do
101
+ subject.with_role(:group, tourist).should =~ [ Forum.first ]
102
+ end
103
+
104
+ it "should not get Forum instances not binded to the group role and the tourist user" do
105
+ subject.with_role(:group, tourist).should_not include(Forum.last)
106
+ end
107
+ end
108
+
109
+ context "on the Group class" do
110
+ subject { Group }
111
+
112
+ it "should get all resources binded to the group role and the admin user" do
113
+ subject.with_role(:group, admin).should =~ [ Group.last ]
114
+ end
115
+
116
+ it "should not get resources not binded to the group role and the admin user" do
117
+ subject.with_role(:group, admin).should_not include(Group.first)
118
+ end
119
+ end
120
+ end
121
+
122
+ context "with an array of role names and a user as arguments" do
123
+ context "on the Forum class" do
124
+ subject { Forum }
125
+
126
+ it "should get Forum instances binded to the forum and group roles and the tourist user" do
127
+ subject.with_roles([:forum, :group], tourist).should =~ [ Forum.first, Forum.last ]
128
+ end
129
+
130
+ end
131
+
132
+ context "on the Group class" do
133
+ subject { Group }
134
+
135
+ it "should get Group instances binded to the group and grouper roles and the admin user" do
136
+ subject.with_roles([:group, :grouper], admin).should =~ [ Group.first, Group.last ]
137
+ end
138
+
139
+ end
140
+ end
141
+
142
+ context "with a model not having ID column" do
143
+ subject { Team }
144
+
145
+ it "should find Team instance using team_code column" do
146
+ subject.with_roles([:captain, :player], captain).should =~ [ Team.first, Team.last ]
147
+ end
148
+ end
149
+ end
150
+
151
+ describe ".find_role" do
152
+
153
+ context "without using a role name parameter" do
154
+
155
+ context "on the Forum class" do
156
+ subject { Forum }
157
+
158
+ it "should get all roles binded to a Forum class or instance" do
159
+ subject.find_roles.to_a.should =~ [ forum_role, godfather_role, tourist_role, sneaky_role ]
160
+ end
161
+
162
+ it "should not get roles not binded to a Forum class or instance" do
163
+ subject.find_roles.should_not include(group_role)
164
+ end
165
+
166
+ context "using :any parameter" do
167
+ it "should get all roles binded to any Forum class or instance" do
168
+ subject.find_roles(:any, :any).to_a.should =~ [ forum_role, godfather_role, tourist_role, sneaky_role ]
169
+ end
170
+
171
+ it "should not get roles not binded to a Forum class or instance" do
172
+ subject.find_roles(:any, :any).should_not include(group_role)
173
+ end
174
+ end
175
+ end
176
+
177
+ context "on the Group class" do
178
+ subject { Group }
179
+
180
+ it "should get all roles binded to a Group class or instance" do
181
+ subject.find_roles.to_a.should =~ [ group_role, grouper_role ]
182
+ end
183
+
184
+ it "should not get roles not binded to a Group class or instance" do
185
+ subject.find_roles.should_not include(forum_role, godfather_role, tourist_role, sneaky_role)
186
+ end
187
+
188
+ context "using :any parameter" do
189
+ it "should get all roles binded to Group class or instance" do
190
+ subject.find_roles(:any, :any).to_a.should =~ [ group_role, grouper_role ]
191
+ end
192
+
193
+ it "should not get roles not binded to a Group class or instance" do
194
+ subject.find_roles(:any, :any).should_not include(forum_role, godfather_role, tourist_role, sneaky_role)
195
+ end
196
+ end
197
+ end
198
+ end
199
+
200
+ context "using a role name parameter" do
201
+ context "on the Forum class" do
202
+ subject { Forum }
203
+
204
+ context "without using a user parameter" do
205
+ it "should get all roles binded to a Forum class or instance and forum role name" do
206
+ subject.find_roles(:forum).should include(forum_role, tourist_role)
207
+ end
208
+
209
+ it "should not get roles not binded to a Forum class or instance and forum role name" do
210
+ subject.find_roles(:forum).should_not include(godfather_role, sneaky_role, group_role)
211
+ end
212
+ end
213
+
214
+ context "using a user parameter" do
215
+ it "should get all roles binded to any resource" do
216
+ subject.find_roles(:forum, admin).to_a.should =~ [ forum_role ]
217
+ end
218
+
219
+ it "should not get roles not binded to the admin user and forum role name" do
220
+ subject.find_roles(:forum, admin).should_not include(godfather_role, tourist_role, sneaky_role, group_role)
221
+ end
222
+ end
223
+
224
+ context "using :any parameter" do
225
+ it "should get all roles binded to any resource with forum role name" do
226
+ subject.find_roles(:forum, :any).should include(forum_role, tourist_role)
227
+ end
228
+
229
+ it "should not get roles not binded to a resource with forum role name" do
230
+ subject.find_roles(:forum, :any).should_not include(godfather_role, sneaky_role, group_role)
231
+ end
232
+ end
233
+ end
234
+
235
+ context "on the Group class" do
236
+ subject { Group }
237
+
238
+ context "without using a user parameter" do
239
+ it "should get all roles binded to a Group class or instance and group role name" do
240
+ subject.find_roles(:group).should include(group_role)
241
+ end
242
+
243
+ it "should not get roles not binded to a Forum class or instance and forum role name" do
244
+ subject.find_roles(:group).should_not include(tourist_role, godfather_role, sneaky_role, forum_role)
245
+ end
246
+ end
247
+
248
+ context "using a user parameter" do
249
+ it "should get all roles binded to any resource" do
250
+ subject.find_roles(:group, admin).should include(group_role)
251
+ end
252
+
253
+ it "should not get roles not binded to the admin user and forum role name" do
254
+ subject.find_roles(:group, admin).should_not include(godfather_role, tourist_role, sneaky_role, forum_role)
255
+ end
256
+ end
257
+
258
+ context "using :any parameter" do
259
+ it "should get all roles binded to any resource with forum role name" do
260
+ subject.find_roles(:group, :any).should include(group_role)
261
+ end
262
+
263
+ it "should not get roles not binded to a resource with forum role name" do
264
+ subject.find_roles(:group, :any).should_not include(godfather_role, sneaky_role, forum_role, tourist_role)
265
+ end
266
+ end
267
+ end
268
+ end
269
+
270
+ context "using :any as role name parameter" do
271
+ context "on the Forum class" do
272
+ subject { Forum }
273
+
274
+ context "without using a user parameter" do
275
+ it "should get all roles binded to a Forum class or instance" do
276
+ subject.find_roles(:any).should include(forum_role, godfather_role, tourist_role, sneaky_role)
277
+ end
278
+
279
+ it "should not get roles not binded to a Forum class or instance" do
280
+ subject.find_roles(:any).should_not include(group_role)
281
+ end
282
+ end
283
+
284
+ context "using a user parameter" do
285
+ it "should get all roles binded to a Forum class or instance and admin user" do
286
+ subject.find_roles(:any, admin).should include(forum_role, godfather_role)
287
+ end
288
+
289
+ it "should not get roles not binded to the admin user and Forum class or instance" do
290
+ subject.find_roles(:any, admin).should_not include(tourist_role, sneaky_role, group_role)
291
+ end
292
+ end
293
+
294
+ context "using :any as user parameter" do
295
+ it "should get all roles binded to a Forum class or instance" do
296
+ subject.find_roles(:any, :any).should include(forum_role, godfather_role, tourist_role, sneaky_role)
297
+ end
298
+
299
+ it "should not get roles not binded to a Forum class or instance" do
300
+ subject.find_roles(:any, :any).should_not include(group_role)
301
+ end
302
+ end
303
+ end
304
+
305
+ context "on the Group class" do
306
+ subject { Group }
307
+
308
+ context "without using a user parameter" do
309
+ it "should get all roles binded to a Group class or instance" do
310
+ subject.find_roles(:any).should include(group_role)
311
+ end
312
+
313
+ it "should not get roles not binded to a Group class or instance" do
314
+ subject.find_roles(:any).should_not include(forum_role, godfather_role, tourist_role, sneaky_role)
315
+ end
316
+ end
317
+
318
+ context "using a user parameter" do
319
+ it "should get all roles binded to a Group class or instance and admin user" do
320
+ subject.find_roles(:any, admin).should include(group_role)
321
+ end
322
+
323
+ it "should not get roles not binded to the admin user and Group class or instance" do
324
+ subject.find_roles(:any, admin).should_not include(forum_role, godfather_role, tourist_role, sneaky_role)
325
+ end
326
+ end
327
+
328
+ context "using :any as user parameter" do
329
+ it "should get all roles binded to a Group class or instance" do
330
+ subject.find_roles(:any, :any).should include(group_role)
331
+ end
332
+
333
+ it "should not get roles not binded to a Group class or instance" do
334
+ subject.find_roles(:any, :any).should_not include(forum_role, godfather_role, tourist_role, sneaky_role)
335
+ end
336
+ end
337
+ end
338
+ end
339
+ end
340
+
341
+ describe "#roles" do
342
+ subject { Forum.first }
343
+
344
+ it { should respond_to :roles }
345
+
346
+ context "on a Forum instance" do
347
+ its(:roles) { should eq([ forum_role, sneaky_role ]) }
348
+ its(:roles) { should_not include(group_role, godfather_role, tourist_role) }
349
+ end
350
+
351
+ context "on a Group instance" do
352
+ subject { Group.last }
353
+
354
+ its(:roles) { should eq([ group_role ]) }
355
+ its(:roles) { should_not include(forum_role, godfather_role, sneaky_role, tourist_role) }
356
+
357
+ context "when deleting a Group instance" do
358
+ subject do
359
+ Group.create(:name => "to delete")
360
+ end
361
+
362
+ before do
363
+ subject.roles.create :name => "group_role1"
364
+ subject.roles.create :name => "group_role2"
365
+ end
366
+
367
+ it "should remove the roles binded to this instance" do
368
+ expect { subject.destroy }.to change { Role.count }.by(-2)
369
+ end
370
+ end
371
+ end
372
+ end
373
+
374
+ describe "#applied_roles" do
375
+ context "on a Forum instance" do
376
+ subject { Forum.first }
377
+
378
+ its(:applied_roles) { should =~ [ forum_role, godfather_role, sneaky_role ] }
379
+ its(:applied_roles) { should_not include(group_role, tourist_role) }
380
+ end
381
+
382
+ context "on a Group instance" do
383
+ subject { Group.last }
384
+
385
+ its(:applied_roles) { should =~ [ group_role ] }
386
+ its(:applied_roles) { should_not include(forum_role, godfather_role, sneaky_role, tourist_role) }
387
+ end
388
+ end
389
+ end