rolify 2.2.2 → 3.0.0

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 (40) hide show
  1. data/.travis.yml +11 -7
  2. data/CHANGELOG.rdoc +6 -0
  3. data/README.rdoc +36 -4
  4. data/UPGRADE.rdoc +22 -0
  5. data/lib/generators/rolify/role/role_generator.rb +5 -6
  6. data/lib/generators/rolify/role/templates/README-active_record +21 -0
  7. data/lib/generators/rolify/role/templates/README-mongoid +17 -0
  8. data/lib/generators/rolify/role/templates/initializer.rb +5 -6
  9. data/lib/generators/rolify/role/templates/{role.rb → role-active_record.rb} +0 -0
  10. data/lib/generators/rolify/role/templates/role-mongoid.rb +8 -0
  11. data/lib/rolify/adapters/active_record.rb +83 -0
  12. data/lib/rolify/adapters/base.rb +53 -0
  13. data/lib/rolify/adapters/mongoid.rb +87 -0
  14. data/lib/rolify/configure.rb +40 -0
  15. data/lib/rolify/dynamic.rb +21 -0
  16. data/lib/rolify/railtie.rb +20 -0
  17. data/lib/rolify/resource.rb +25 -0
  18. data/lib/rolify/role.rb +32 -110
  19. data/lib/rolify/version.rb +1 -1
  20. data/lib/rolify.rb +35 -0
  21. data/rolify.gemspec +3 -2
  22. data/spec/generators/rolify/role/role_generator_spec.rb +75 -16
  23. data/spec/rolify/config_spec.rb +189 -0
  24. data/spec/rolify/custom_spec.rb +15 -0
  25. data/spec/rolify/resource_spec.rb +317 -0
  26. data/spec/rolify/role_spec.rb +8 -495
  27. data/spec/rolify/shared_contexts.rb +74 -0
  28. data/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +110 -0
  29. data/spec/rolify/shared_examples/shared_examples_for_has_all_roles.rb +71 -0
  30. data/spec/rolify/shared_examples/shared_examples_for_has_any_role.rb +71 -0
  31. data/spec/rolify/shared_examples/shared_examples_for_has_no_role.rb +97 -0
  32. data/spec/rolify/shared_examples/shared_examples_for_has_role_getter.rb +135 -0
  33. data/spec/rolify/shared_examples/shared_examples_for_has_role_setter.rb +92 -0
  34. data/spec/rolify/shared_examples/shared_examples_for_roles.rb +80 -0
  35. data/spec/spec_helper.rb +10 -5
  36. data/spec/support/{models.rb → adapters/active_record.rb} +11 -8
  37. data/spec/support/adapters/mongoid.rb +56 -0
  38. data/spec/support/data.rb +14 -5
  39. metadata +76 -20
  40. data/benchmarks/performance.rb +0 -51
@@ -0,0 +1,317 @@
1
+ require "spec_helper"
2
+
3
+ describe Rolify::Resource do
4
+ before(:all) do
5
+ reset_defaults
6
+ #Role.destroy_all
7
+ User.rolify :role_cname => "Role"
8
+ Forum.resourcify :role_cname => "Role"
9
+ Group.resourcify :role_cname => "Role"
10
+ end
11
+
12
+ # Users
13
+ let(:admin) { User.first }
14
+ let(:tourist) { User.last }
15
+
16
+ # roles
17
+ let!(:forum_role) { admin.has_role("forum", Forum.first) }
18
+ let!(:godfather_role) { admin.has_role("godfather", Forum) }
19
+ let!(:group_role) { admin.has_role("group", Group.last) }
20
+ let!(:tourist_role) { tourist.has_role("forum", Forum.last) }
21
+ let!(:sneaky_role) { tourist.has_role("group", Forum.first) }
22
+
23
+ describe ".with_roles" do
24
+ subject { Group }
25
+
26
+ it { should respond_to(:find_roles).with(1).arguments }
27
+ it { should respond_to(:find_roles).with(2).arguments }
28
+
29
+ context "with a role name as argument" do
30
+ context "on the Forum class" do
31
+ subject { Forum }
32
+
33
+ it "should include Forum instances with forum role" do
34
+ subject.with_role("forum").should include(Forum.first, Forum.last)
35
+ end
36
+ it "should include Forum instances with godfather role" do
37
+ subject.with_role("godfather").should eq(Forum.all)
38
+ end
39
+ end
40
+
41
+ context "on the Group class" do
42
+ subject { Group }
43
+
44
+ it "should include Group instances with group role" do
45
+ subject.with_role("group").should include(Group.last)
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ context "with a role name and a user as arguments" do
52
+ context "on the Forum class" do
53
+ subject { Forum }
54
+
55
+ it "should get all Forum instances binded to the forum role and the admin user" do
56
+ subject.with_role("forum", admin).should include(Forum.first)
57
+ end
58
+
59
+ it "should get all Forum instances binded to the forum role and the tourist user" do
60
+ subject.with_role("forum", tourist).should include(Forum.last)
61
+ end
62
+
63
+ it "should get all Forum instances binded to the godfather role and the admin user" do
64
+ subject.with_role("godfather", admin).should == Forum.all
65
+ end
66
+
67
+ it "should get all Forum instances binded to the godfather role and the tourist user" do
68
+ subject.with_role("godfather", tourist).should be_empty
69
+ end
70
+
71
+ it "should get Forum instances binded to the group role and the tourist user" do
72
+ subject.with_role("group", tourist).should include(Forum.first)
73
+ end
74
+
75
+ it "should not get Forum instances not binded to the group role and the tourist user" do
76
+ subject.with_role("group", tourist).should_not include(Forum.last)
77
+ end
78
+ end
79
+
80
+ context "on the Group class" do
81
+ subject { Group }
82
+
83
+ it "should get all resources binded to the group role and the admin user" do
84
+ subject.with_role("group", admin).should include(Group.last)
85
+ end
86
+
87
+ it "should not get resources not binded to the group role and the admin user" do
88
+ subject.with_role("group", admin).should_not include(Group.first)
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ describe ".find_role" do
95
+
96
+ context "without using a role name parameter" do
97
+
98
+ context "on the Forum class" do
99
+ subject { Forum }
100
+
101
+ it "should get all roles binded to a Forum class or instance" do
102
+ subject.find_roles.should include(forum_role, godfather_role, tourist_role, sneaky_role)
103
+ end
104
+
105
+ it "should not get roles not binded to a Forum class or instance" do
106
+ subject.find_roles.should_not include(group_role)
107
+ end
108
+
109
+ context "using :any parameter" do
110
+ it "should get all roles binded to any Forum class or instance" do
111
+ subject.find_roles(:any, :any).should include(forum_role, godfather_role, tourist_role, sneaky_role)
112
+ end
113
+
114
+ it "should not get roles not binded to a Forum class or instance" do
115
+ subject.find_roles(:any, :any).should_not include(group_role)
116
+ end
117
+ end
118
+ end
119
+
120
+ context "on the Group class" do
121
+ subject { Group }
122
+
123
+ it "should get all roles binded to a Group class or instance" do
124
+ subject.find_roles.should include(group_role)
125
+ end
126
+
127
+ it "should not get roles not binded to a Group class or instance" do
128
+ subject.find_roles.should_not include(forum_role, godfather_role, tourist_role, sneaky_role)
129
+ end
130
+
131
+ context "using :any parameter" do
132
+ it "should get all roles binded to Group class or instance" do
133
+ subject.find_roles(:any, :any).should include(group_role)
134
+ end
135
+
136
+ it "should not get roles not binded to a Group class or instance" do
137
+ subject.find_roles(:any, :any).should_not include(forum_role, godfather_role, tourist_role, sneaky_role)
138
+ end
139
+ end
140
+ end
141
+ end
142
+
143
+ context "using a role name parameter" do
144
+ context "on the Forum class" do
145
+ subject { Forum }
146
+
147
+ context "without using a user parameter" do
148
+ it "should get all roles binded to a Forum class or instance and forum role name" do
149
+ subject.find_roles("forum").should include(forum_role, tourist_role)
150
+ end
151
+
152
+ it "should not get roles not binded to a Forum class or instance and forum role name" do
153
+ subject.find_roles("forum").should_not include(godfather_role, sneaky_role, group_role)
154
+ end
155
+ end
156
+
157
+ context "using a user parameter" do
158
+ it "should get all roles binded to any resource" do
159
+ subject.find_roles("forum", admin).should include(forum_role)
160
+ end
161
+
162
+ it "should not get roles not binded to the admin user and forum role name" do
163
+ subject.find_roles("forum", admin).should_not include(godfather_role, tourist_role, sneaky_role, group_role)
164
+ end
165
+ end
166
+
167
+ context "using :any parameter" do
168
+ it "should get all roles binded to any resource with forum role name" do
169
+ subject.find_roles("forum", :any).should include(forum_role, tourist_role)
170
+ end
171
+
172
+ it "should not get roles not binded to a resource with forum role name" do
173
+ subject.find_roles("forum", :any).should_not include(godfather_role, sneaky_role, group_role)
174
+ end
175
+ end
176
+ end
177
+
178
+ context "on the Group class" do
179
+ subject { Group }
180
+
181
+ context "without using a user parameter" do
182
+ it "should get all roles binded to a Group class or instance and group role name" do
183
+ subject.find_roles("group").should include(group_role)
184
+ end
185
+
186
+ it "should not get roles not binded to a Forum class or instance and forum role name" do
187
+ subject.find_roles("group").should_not include(tourist_role, godfather_role, sneaky_role, forum_role)
188
+ end
189
+ end
190
+
191
+ context "using a user parameter" do
192
+ it "should get all roles binded to any resource" do
193
+ subject.find_roles("group", admin).should include(group_role)
194
+ end
195
+
196
+ it "should not get roles not binded to the admin user and forum role name" do
197
+ subject.find_roles("group", admin).should_not include(godfather_role, tourist_role, sneaky_role, forum_role)
198
+ end
199
+ end
200
+
201
+ context "using :any parameter" do
202
+ it "should get all roles binded to any resource with forum role name" do
203
+ subject.find_roles("group", :any).should include(group_role)
204
+ end
205
+
206
+ it "should not get roles not binded to a resource with forum role name" do
207
+ subject.find_roles("group", :any).should_not include(godfather_role, sneaky_role, forum_role, tourist_role)
208
+ end
209
+ end
210
+ end
211
+ end
212
+
213
+ context "using :any as role name parameter" do
214
+ context "on the Forum class" do
215
+ subject { Forum }
216
+
217
+ context "without using a user parameter" do
218
+ it "should get all roles binded to a Forum class or instance" do
219
+ subject.find_roles(:any).should include(forum_role, godfather_role, tourist_role, sneaky_role)
220
+ end
221
+
222
+ it "should not get roles not binded to a Forum class or instance" do
223
+ subject.find_roles(:any).should_not include(group_role)
224
+ end
225
+ end
226
+
227
+ context "using a user parameter" do
228
+ it "should get all roles binded to a Forum class or instance and admin user" do
229
+ subject.find_roles(:any, admin).should include(forum_role, godfather_role)
230
+ end
231
+
232
+ it "should not get roles not binded to the admin user and Forum class or instance" do
233
+ subject.find_roles(:any, admin).should_not include(tourist_role, sneaky_role, group_role)
234
+ end
235
+ end
236
+
237
+ context "using :any as user parameter" do
238
+ it "should get all roles binded to a Forum class or instance" do
239
+ subject.find_roles(:any, :any).should include(forum_role, godfather_role, tourist_role, sneaky_role)
240
+ end
241
+
242
+ it "should not get roles not binded to a Forum class or instance" do
243
+ subject.find_roles(:any, :any).should_not include(group_role)
244
+ end
245
+ end
246
+ end
247
+
248
+ context "on the Group class" do
249
+ subject { Group }
250
+
251
+ context "without using a user parameter" do
252
+ it "should get all roles binded to a Group class or instance" do
253
+ subject.find_roles(:any).should include(group_role)
254
+ end
255
+
256
+ it "should not get roles not binded to a Group class or instance" do
257
+ subject.find_roles(:any).should_not include(forum_role, godfather_role, tourist_role, sneaky_role)
258
+ end
259
+ end
260
+
261
+ context "using a user parameter" do
262
+ it "should get all roles binded to a Group class or instance and admin user" do
263
+ subject.find_roles(:any, admin).should include(group_role)
264
+ end
265
+
266
+ it "should not get roles not binded to the admin user and Group class or instance" do
267
+ subject.find_roles(:any, admin).should_not include(forum_role, godfather_role, tourist_role, sneaky_role)
268
+ end
269
+ end
270
+
271
+ context "using :any as user parameter" do
272
+ it "should get all roles binded to a Group class or instance" do
273
+ subject.find_roles(:any, :any).should include(group_role)
274
+ end
275
+
276
+ it "should not get roles not binded to a Group class or instance" do
277
+ subject.find_roles(:any, :any).should_not include(forum_role, godfather_role, tourist_role, sneaky_role)
278
+ end
279
+ end
280
+ end
281
+ end
282
+ end
283
+
284
+ describe "#roles" do
285
+ subject { Forum.first }
286
+
287
+ it { should respond_to :roles }
288
+
289
+ context "on a Forum instance" do
290
+ its(:roles) { should include(forum_role, sneaky_role) }
291
+ its(:roles) { subject.should_not include(group_role, godfather_role, tourist_role) }
292
+ end
293
+
294
+ context "on a Group instance" do
295
+ subject { Group.last }
296
+
297
+ its(:roles) { should include(group_role) }
298
+ its(:roles) { should_not include(forum_role, godfather_role, sneaky_role, tourist_role) }
299
+ end
300
+ end
301
+
302
+ describe "#applied_roles" do
303
+ context "on a Forum instance" do
304
+ subject { Forum.first }
305
+
306
+ its(:applied_roles) { should include(forum_role, godfather_role, sneaky_role) }
307
+ its(:applied_roles) { should_not include(group_role, tourist_role) }
308
+ end
309
+
310
+ context "on a Group instance" do
311
+ subject { Group.last }
312
+
313
+ its(:applied_roles) { should include(group_role) }
314
+ its(:applied_roles) { should_not include(forum_role, godfather_role, sneaky_role, tourist_role) }
315
+ end
316
+ end
317
+ end