canard 0.5.0.pre → 0.6.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +5 -5
  2. data/.hound.yml +3 -0
  3. data/.rubocop.yml +27 -0
  4. data/.rubocop_todo.yml +37 -0
  5. data/.travis.yml +1 -1
  6. data/Gemfile +7 -6
  7. data/README.md +40 -37
  8. data/Rakefile +5 -2
  9. data/canard.gemspec +8 -7
  10. data/lib/ability.rb +14 -13
  11. data/lib/canard.rb +4 -2
  12. data/lib/canard/abilities.rb +7 -9
  13. data/lib/canard/adapters/active_record.rb +32 -29
  14. data/lib/canard/adapters/mongoid.rb +18 -11
  15. data/lib/canard/find_abilities.rb +8 -9
  16. data/lib/canard/railtie.rb +11 -16
  17. data/lib/canard/user_model.rb +66 -67
  18. data/lib/canard/version.rb +3 -1
  19. data/lib/generators/ability_definition.rb +16 -14
  20. data/lib/generators/canard/ability/ability_generator.rb +16 -12
  21. data/lib/generators/rspec/ability/ability_generator.rb +9 -9
  22. data/lib/tasks/canard.rake +6 -6
  23. data/test/abilities/administrators.rb +2 -2
  24. data/test/canard/abilities_test.rb +14 -21
  25. data/test/canard/ability_test.rb +40 -52
  26. data/test/canard/adapters/active_record_test.rb +71 -135
  27. data/test/canard/adapters/mongoid_test.rb +61 -132
  28. data/test/canard/canard_test.rb +8 -10
  29. data/test/canard/find_abilities_test.rb +9 -11
  30. data/test/canard/user_model_test.rb +22 -32
  31. data/test/dummy/Rakefile +3 -1
  32. data/test/dummy/app/abilities/admins.rb +4 -4
  33. data/test/dummy/app/abilities/authors.rb +3 -3
  34. data/test/dummy/app/abilities/editors.rb +2 -2
  35. data/test/dummy/app/abilities/guests.rb +3 -3
  36. data/test/dummy/app/abilities/users.rb +4 -4
  37. data/test/dummy/app/controllers/application_controller.rb +2 -0
  38. data/test/dummy/app/models/activity.rb +3 -1
  39. data/test/dummy/app/models/member.rb +3 -3
  40. data/test/dummy/app/models/mongoid_user.rb +5 -3
  41. data/test/dummy/app/models/plain_ruby_non_user.rb +2 -2
  42. data/test/dummy/app/models/plain_ruby_user.rb +3 -3
  43. data/test/dummy/app/models/post.rb +3 -1
  44. data/test/dummy/app/models/user.rb +3 -2
  45. data/test/dummy/app/models/user_without_role.rb +4 -4
  46. data/test/dummy/app/models/user_without_role_mask.rb +3 -3
  47. data/test/dummy/config.ru +3 -1
  48. data/test/dummy/config/application.rb +9 -8
  49. data/test/dummy/config/boot.rb +4 -2
  50. data/test/dummy/config/environment.rb +3 -1
  51. data/test/dummy/config/environments/development.rb +2 -0
  52. data/test/dummy/config/environments/test.rb +4 -2
  53. data/test/dummy/config/initializers/secret_token.rb +2 -0
  54. data/test/dummy/config/initializers/session_store.rb +3 -1
  55. data/test/dummy/config/initializers/wrap_parameters.rb +3 -1
  56. data/test/dummy/config/mongoid3.yml +5 -1
  57. data/test/dummy/config/routes.rb +2 -0
  58. data/test/dummy/db/migrate/20120430083231_initialize_db.rb +7 -7
  59. data/test/dummy/db/schema.rb +11 -11
  60. data/test/dummy/script/rails +4 -2
  61. data/test/support/reloadable.rb +14 -15
  62. data/test/test_helper.rb +7 -13
  63. metadata +18 -18
  64. data/test/dummy/config/mongoid2.yml +0 -9
@@ -1,46 +1,28 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
  require 'bson'
3
5
 
4
- # Make this test compatible with ruby 1.8.7
5
- begin
6
- BSON::ObjectId.new <=> BSON::ObjectId.new
7
- rescue NoMethodError
8
- class BSON::ObjectId
9
- def <=>(other)
10
- self.to_s <=> other.to_s
11
- end
12
- end
13
- end
14
-
15
6
  describe Canard::Adapters::Mongoid do
16
-
17
7
  describe 'acts_as_user' do
18
-
19
8
  describe 'with a role_mask' do
20
-
21
9
  describe 'and :roles => [] specified' do
22
-
23
10
  it 'sets the valid_roles for the class' do
24
- MongoidUser.valid_roles.must_equal [:viewer, :author, :admin]
11
+ MongoidUser.valid_roles.must_equal %i[viewer author admin]
25
12
  end
26
-
27
13
  end
28
-
29
14
  end
30
-
31
15
  end
32
16
 
33
- describe "scopes" do
34
-
35
- describe "on an Mongoid model with roles" do
36
-
17
+ describe 'scopes' do
18
+ describe 'on an Mongoid model with roles' do
37
19
  before do
38
20
  @no_role = MongoidUser.create
39
- @admin_author_viewer = MongoidUser.create(:roles => [:admin, :author, :viewer])
40
- @author_viewer = MongoidUser.create(:roles => [:author, :viewer])
41
- @viewer = MongoidUser.create(:roles => [:viewer])
42
- @admin_only = MongoidUser.create(:roles => [:admin])
43
- @author_only = MongoidUser.create(:roles => [:author])
21
+ @admin_author_viewer = MongoidUser.create(roles: %i[admin author viewer])
22
+ @author_viewer = MongoidUser.create(roles: %i[author viewer])
23
+ @viewer = MongoidUser.create(roles: [:viewer])
24
+ @admin_only = MongoidUser.create(roles: [:admin])
25
+ @author_only = MongoidUser.create(roles: [:author])
44
26
  end
45
27
 
46
28
  after do
@@ -49,25 +31,23 @@ describe Canard::Adapters::Mongoid do
49
31
 
50
32
  subject { MongoidUser }
51
33
 
52
- it "adds a scope to return instances with each role" do
34
+ it 'adds a scope to return instances with each role' do
53
35
  subject.must_respond_to :admins
54
36
  subject.must_respond_to :authors
55
37
  subject.must_respond_to :viewers
56
38
  end
57
39
 
58
- it "adds a scope to return instances without each role" do
40
+ it 'adds a scope to return instances without each role' do
59
41
  subject.must_respond_to :non_admins
60
42
  subject.must_respond_to :non_authors
61
43
  subject.must_respond_to :non_viewers
62
44
  end
63
45
 
64
- describe "finding instances with a role" do
65
-
66
- describe "admins scope" do
67
-
46
+ describe 'finding instances with a role' do
47
+ describe 'admins scope' do
68
48
  subject { MongoidUser.admins.sort_by(&:id) }
69
49
 
70
- it "returns only admins" do
50
+ it 'returns only admins' do
71
51
  subject.must_equal [@admin_author_viewer, @admin_only].sort_by(&:id)
72
52
  end
73
53
 
@@ -77,14 +57,12 @@ describe Canard::Adapters::Mongoid do
77
57
  subject.wont_include @author_only
78
58
  subject.wont_include @viewer
79
59
  end
80
-
81
60
  end
82
61
 
83
- describe "authors scope" do
84
-
62
+ describe 'authors scope' do
85
63
  subject { MongoidUser.authors.sort_by(&:id) }
86
64
 
87
- it "returns only authors" do
65
+ it 'returns only authors' do
88
66
  subject.must_equal [@admin_author_viewer, @author_viewer, @author_only].sort_by(&:id)
89
67
  end
90
68
 
@@ -93,14 +71,12 @@ describe Canard::Adapters::Mongoid do
93
71
  subject.wont_include @admin_only
94
72
  subject.wont_include @viewer
95
73
  end
96
-
97
74
  end
98
75
 
99
- describe "viewers scope" do
100
-
76
+ describe 'viewers scope' do
101
77
  subject { MongoidUser.viewers.sort_by(&:id) }
102
78
 
103
- it "returns only viewers" do
79
+ it 'returns only viewers' do
104
80
  subject.must_equal [@admin_author_viewer, @author_viewer, @viewer].sort_by(&:id)
105
81
  end
106
82
 
@@ -109,18 +85,14 @@ describe Canard::Adapters::Mongoid do
109
85
  subject.wont_include @admin_only
110
86
  subject.wont_include @author_only
111
87
  end
112
-
113
88
  end
114
-
115
89
  end
116
90
 
117
- describe "finding instances without a role" do
118
-
119
- describe "non_admins scope" do
120
-
91
+ describe 'finding instances without a role' do
92
+ describe 'non_admins scope' do
121
93
  subject { MongoidUser.non_admins.sort_by(&:id) }
122
94
 
123
- it "returns only non_admins" do
95
+ it 'returns only non_admins' do
124
96
  subject.must_equal [@no_role, @author_viewer, @viewer, @author_only].sort_by(&:id)
125
97
  end
126
98
 
@@ -128,14 +100,12 @@ describe Canard::Adapters::Mongoid do
128
100
  subject.wont_include @admin_author_viewer
129
101
  subject.wont_include @admin_only
130
102
  end
131
-
132
103
  end
133
104
 
134
- describe "non_authors scope" do
135
-
105
+ describe 'non_authors scope' do
136
106
  subject { MongoidUser.non_authors.sort_by(&:id) }
137
107
 
138
- it "returns only non_authors" do
108
+ it 'returns only non_authors' do
139
109
  subject.must_equal [@no_role, @viewer, @admin_only].sort_by(&:id)
140
110
  end
141
111
 
@@ -144,14 +114,12 @@ describe Canard::Adapters::Mongoid do
144
114
  subject.wont_include @author_viewer
145
115
  subject.wont_include @author_only
146
116
  end
147
-
148
117
  end
149
118
 
150
- describe "non_viewers scope" do
151
-
119
+ describe 'non_viewers scope' do
152
120
  subject { MongoidUser.non_viewers.sort_by(&:id) }
153
121
 
154
- it "returns only non_viewers" do
122
+ it 'returns only non_viewers' do
155
123
  subject.must_equal [@no_role, @admin_only, @author_only].sort_by(&:id)
156
124
  end
157
125
 
@@ -160,18 +128,14 @@ describe Canard::Adapters::Mongoid do
160
128
  subject.wont_include @author_viewer
161
129
  subject.wont_include @viewer
162
130
  end
163
-
164
131
  end
165
-
166
132
  end
167
133
 
168
- describe "with_any_role" do
169
-
170
- describe "specifying admin only" do
171
-
134
+ describe 'with_any_role' do
135
+ describe 'specifying admin only' do
172
136
  subject { MongoidUser.with_any_role(:admin).sort_by(&:id) }
173
137
 
174
- it "returns only admins" do
138
+ it 'returns only admins' do
175
139
  subject.must_equal [@admin_author_viewer, @admin_only].sort_by(&:id)
176
140
  end
177
141
 
@@ -181,14 +145,12 @@ describe Canard::Adapters::Mongoid do
181
145
  subject.wont_include @author_only
182
146
  subject.wont_include @viewer
183
147
  end
184
-
185
148
  end
186
149
 
187
- describe "specifying author only" do
188
-
150
+ describe 'specifying author only' do
189
151
  subject { MongoidUser.with_any_role(:author).sort_by(&:id) }
190
152
 
191
- it "returns only authors" do
153
+ it 'returns only authors' do
192
154
  subject.must_equal [@admin_author_viewer, @author_viewer, @author_only].sort_by(&:id)
193
155
  end
194
156
 
@@ -197,14 +159,12 @@ describe Canard::Adapters::Mongoid do
197
159
  subject.wont_include @admin_only
198
160
  subject.wont_include @viewer
199
161
  end
200
-
201
162
  end
202
163
 
203
- describe "specifying viewer only" do
204
-
164
+ describe 'specifying viewer only' do
205
165
  subject { MongoidUser.with_any_role(:viewer).sort_by(&:id) }
206
166
 
207
- it "returns only viewers" do
167
+ it 'returns only viewers' do
208
168
  subject.must_equal [@admin_author_viewer, @author_viewer, @viewer].sort_by(&:id)
209
169
  end
210
170
 
@@ -213,14 +173,12 @@ describe Canard::Adapters::Mongoid do
213
173
  subject.wont_include @admin_only
214
174
  subject.wont_include @author_only
215
175
  end
216
-
217
176
  end
218
177
 
219
- describe "specifying admin and author" do
220
-
178
+ describe 'specifying admin and author' do
221
179
  subject { MongoidUser.with_any_role(:admin, :author).sort_by(&:id) }
222
180
 
223
- it "returns only admins and authors" do
181
+ it 'returns only admins and authors' do
224
182
  subject.must_equal [@admin_author_viewer, @author_viewer, @admin_only, @author_only].sort_by(&:id)
225
183
  end
226
184
 
@@ -228,14 +186,12 @@ describe Canard::Adapters::Mongoid do
228
186
  subject.wont_include @no_role
229
187
  subject.wont_include @viewer
230
188
  end
231
-
232
189
  end
233
190
 
234
- describe "specifying admin and viewer" do
235
-
191
+ describe 'specifying admin and viewer' do
236
192
  subject { MongoidUser.with_any_role(:admin, :viewer).sort_by(&:id) }
237
193
 
238
- it "returns only admins and viewers" do
194
+ it 'returns only admins and viewers' do
239
195
  subject.must_equal [@admin_author_viewer, @author_viewer, @admin_only, @viewer].sort_by(&:id)
240
196
  end
241
197
 
@@ -243,14 +199,12 @@ describe Canard::Adapters::Mongoid do
243
199
  subject.wont_include @no_role
244
200
  subject.wont_include @author_only
245
201
  end
246
-
247
202
  end
248
203
 
249
- describe "specifying author and viewer" do
250
-
204
+ describe 'specifying author and viewer' do
251
205
  subject { MongoidUser.with_any_role(:author, :viewer).sort_by(&:id) }
252
206
 
253
- it "returns only authors and viewers" do
207
+ it 'returns only authors and viewers' do
254
208
  subject.must_equal [@admin_author_viewer, @author_viewer, @author_only, @viewer].sort_by(&:id)
255
209
  end
256
210
 
@@ -258,32 +212,26 @@ describe Canard::Adapters::Mongoid do
258
212
  subject.wont_include @no_role
259
213
  subject.wont_include @admin_only
260
214
  end
261
-
262
215
  end
263
216
 
264
- describe "specifying admin, author and viewer" do
265
-
217
+ describe 'specifying admin, author and viewer' do
266
218
  subject { MongoidUser.with_any_role(:admin, :author, :viewer).sort_by(&:id) }
267
219
 
268
- it "returns only admins, authors and viewers" do
220
+ it 'returns only admins, authors and viewers' do
269
221
  subject.must_equal [@admin_author_viewer, @author_viewer, @admin_only, @author_only, @viewer].sort_by(&:id)
270
222
  end
271
223
 
272
224
  it "doesn't return non admins, authors or viewers" do
273
225
  subject.wont_include @no_role
274
226
  end
275
-
276
227
  end
277
-
278
228
  end
279
229
 
280
- describe "with_all_roles" do
281
-
282
- describe "specifying admin only" do
283
-
230
+ describe 'with_all_roles' do
231
+ describe 'specifying admin only' do
284
232
  subject { MongoidUser.with_all_roles(:admin).sort_by(&:id) }
285
233
 
286
- it "returns only admins" do
234
+ it 'returns only admins' do
287
235
  subject.must_equal [@admin_author_viewer, @admin_only].sort_by(&:id)
288
236
  end
289
237
 
@@ -293,14 +241,12 @@ describe Canard::Adapters::Mongoid do
293
241
  subject.wont_include @author_only
294
242
  subject.wont_include @viewer
295
243
  end
296
-
297
244
  end
298
245
 
299
- describe "specifying author only" do
300
-
246
+ describe 'specifying author only' do
301
247
  subject { MongoidUser.with_all_roles(:author).sort_by(&:id) }
302
248
 
303
- it "returns only authors" do
249
+ it 'returns only authors' do
304
250
  subject.must_equal [@admin_author_viewer, @author_viewer, @author_only].sort_by(&:id)
305
251
  end
306
252
 
@@ -309,14 +255,12 @@ describe Canard::Adapters::Mongoid do
309
255
  subject.wont_include @admin_only
310
256
  subject.wont_include @viewer
311
257
  end
312
-
313
258
  end
314
259
 
315
- describe "specifying viewer only" do
316
-
260
+ describe 'specifying viewer only' do
317
261
  subject { MongoidUser.with_all_roles(:viewer).sort_by(&:id) }
318
262
 
319
- it "returns only viewers" do
263
+ it 'returns only viewers' do
320
264
  subject.must_equal [@admin_author_viewer, @author_viewer, @viewer].sort_by(&:id)
321
265
  end
322
266
 
@@ -325,14 +269,12 @@ describe Canard::Adapters::Mongoid do
325
269
  subject.wont_include @admin_only
326
270
  subject.wont_include @author_only
327
271
  end
328
-
329
272
  end
330
273
 
331
- describe "specifying admin and author" do
332
-
274
+ describe 'specifying admin and author' do
333
275
  subject { MongoidUser.with_all_roles(:admin, :author).sort_by(&:id) }
334
276
 
335
- it "returns only admins and authors" do
277
+ it 'returns only admins and authors' do
336
278
  subject.must_equal [@admin_author_viewer].sort_by(&:id)
337
279
  end
338
280
 
@@ -343,14 +285,12 @@ describe Canard::Adapters::Mongoid do
343
285
  subject.wont_include @admin_only
344
286
  subject.wont_include @viewer
345
287
  end
346
-
347
288
  end
348
289
 
349
- describe "specifying admin and viewer" do
350
-
290
+ describe 'specifying admin and viewer' do
351
291
  subject { MongoidUser.with_all_roles(:admin, :viewer).sort_by(&:id) }
352
292
 
353
- it "returns only admins and viewers" do
293
+ it 'returns only admins and viewers' do
354
294
  subject.must_equal [@admin_author_viewer].sort_by(&:id)
355
295
  end
356
296
 
@@ -361,14 +301,12 @@ describe Canard::Adapters::Mongoid do
361
301
  subject.wont_include @admin_only
362
302
  subject.wont_include @viewer
363
303
  end
364
-
365
304
  end
366
305
 
367
- describe "specifying author and viewer" do
368
-
306
+ describe 'specifying author and viewer' do
369
307
  subject { MongoidUser.with_all_roles(:author, :viewer).sort_by(&:id) }
370
308
 
371
- it "returns only authors and viewers" do
309
+ it 'returns only authors and viewers' do
372
310
  subject.must_equal [@admin_author_viewer, @author_viewer].sort_by(&:id)
373
311
  end
374
312
 
@@ -378,14 +316,12 @@ describe Canard::Adapters::Mongoid do
378
316
  subject.wont_include @author_only
379
317
  subject.wont_include @viewer
380
318
  end
381
-
382
319
  end
383
320
 
384
- describe "specifying admin, author and viewer" do
385
-
321
+ describe 'specifying admin, author and viewer' do
386
322
  subject { MongoidUser.with_all_roles(:admin, :author, :viewer).sort_by(&:id) }
387
323
 
388
- it "returns only admins, authors and viewers" do
324
+ it 'returns only admins, authors and viewers' do
389
325
  subject.must_equal [@admin_author_viewer].sort_by(&:id)
390
326
  end
391
327
 
@@ -396,18 +332,14 @@ describe Canard::Adapters::Mongoid do
396
332
  subject.wont_include @admin_only
397
333
  subject.wont_include @viewer
398
334
  end
399
-
400
335
  end
401
-
402
336
  end
403
337
 
404
- describe "with_only_roles" do
405
-
406
- describe "specifying one role" do
407
-
338
+ describe 'with_only_roles' do
339
+ describe 'specifying one role' do
408
340
  subject { MongoidUser.with_only_roles(:admin).sort_by(&:id) }
409
341
 
410
- it "returns users with just that role" do
342
+ it 'returns users with just that role' do
411
343
  subject.must_equal [@admin_only].sort_by(&:id)
412
344
  end
413
345
 
@@ -418,14 +350,12 @@ describe Canard::Adapters::Mongoid do
418
350
  subject.wont_include @author_only
419
351
  subject.wont_include @viewer
420
352
  end
421
-
422
353
  end
423
354
 
424
- describe "specifying multiple roles" do
425
-
355
+ describe 'specifying multiple roles' do
426
356
  subject { MongoidUser.with_only_roles(:author, :viewer).sort_by(&:id) }
427
357
 
428
- it "returns only users with no more or less roles" do
358
+ it 'returns only users with no more or less roles' do
429
359
  subject.must_equal [@author_viewer].sort_by(&:id)
430
360
  end
431
361
 
@@ -440,5 +370,4 @@ describe Canard::Adapters::Mongoid do
440
370
  end
441
371
  end
442
372
  end
443
-
444
373
  end