canard 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  *.gem
2
2
  .bundle
3
3
  pkg/*
4
- .rvmrc
4
+ .rvmrc
5
+ Gemfile.lock
data/README.rdoc CHANGED
@@ -95,6 +95,21 @@ Obviously the generators are just a starting point and should not be used only t
95
95
  suggest that every new model you create you add to the abilities as the specs are so easy to write and CanCan
96
96
  definitions are so clear and simple.
97
97
 
98
+ == Scopes
99
+
100
+ The :acts_as_user method with automatically define some named scopes for each role. For the example User model
101
+ above it will define the following scopes;
102
+
103
+ User.admins:: return all the users with the admin role
104
+ User.non_admins:: return all the users without the admin role
105
+ User.managers:: return all the users with the manager role
106
+ User.non_namagers:: return all the users without the manager role
107
+
108
+ In addition to the role specific scopes it also adds some general scopes;
109
+
110
+ User.with_any_role(roles):: return all the users with any of the specified roles
111
+ User.with_all_roles(roles):: return only the users with all the specified roles
112
+
98
113
  == Installation
99
114
 
100
115
  === Rails 3.x
data/TODO CHANGED
@@ -2,5 +2,4 @@
2
2
  * Test the ability class.
3
3
  * Test the generators.
4
4
  * Test the railtie
5
- * Add scopes by default and :scopes => false to acts_as_user
6
5
  * Expand the tests to produce all the standard abilities: index,show,read,new,create,edit,update,destroy.
@@ -8,6 +8,43 @@ module Canard
8
8
  options = args.extract_options!.symbolize_keys
9
9
 
10
10
  roles options[:roles] if options.has_key?(:roles) && column_names.include?(roles_attribute_name.to_s)
11
+
12
+ valid_roles.each do |role|
13
+ define_scopes_for_role role
14
+ end
15
+
16
+ define_scope_method(:with_any_role) do |*roles|
17
+ where("#{role_mask_column} & :role_mask > 0", { :role_mask => mask_for(*roles) })
18
+ end
19
+
20
+ define_scope_method(:with_all_roles) do |*roles|
21
+ where("#{role_mask_column} & :role_mask = :role_mask", { :role_mask => mask_for(*roles) })
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def define_scopes_for_role(role)
28
+ include_scope = role.to_s.pluralize
29
+ exclude_scope = "non_#{include_scope}"
30
+
31
+ define_scope_method(include_scope) do
32
+ where("#{role_mask_column} & :role_mask > 0", { :role_mask => mask_for(role) })
33
+ end
34
+
35
+ define_scope_method(exclude_scope) do
36
+ where("#{role_mask_column} & :role_mask = 0 or #{role_mask_column} is null", { :role_mask => mask_for(role) })
37
+ end
38
+ end
39
+
40
+ def define_scope_method(method, &block)
41
+ (class << self; self end).class_eval do
42
+ define_method(method, block)
43
+ end
44
+ end
45
+
46
+ def role_mask_column
47
+ %{"#{table_name}"."#{roles_attribute_name}"}
11
48
  end
12
49
 
13
50
  end
@@ -1,3 +1,3 @@
1
1
  module Canard
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -55,4 +55,379 @@ describe Canard::UserModel do
55
55
 
56
56
  end
57
57
 
58
- end
58
+ describe "scopes" do
59
+
60
+ before do
61
+ @no_role = User.create
62
+ @admin_author_viewer = User.create(:roles => [:admin, :author, :viewer])
63
+ @author_viewer = User.create(:roles => [:author, :viewer])
64
+ @viewer = User.create(:roles => [:viewer])
65
+ @admin_only = User.create(:roles => [:admin])
66
+ @author_only = User.create(:roles => [:author])
67
+ end
68
+
69
+ after do
70
+ User.delete_all
71
+ end
72
+
73
+ describe "on models with roles" do
74
+
75
+ subject { User }
76
+
77
+ it "should add a scope to return instances with each role" do
78
+ subject.must_respond_to :admins
79
+ subject.must_respond_to :authors
80
+ subject.must_respond_to :viewers
81
+ end
82
+
83
+ it "should add a scope to return instances without each role" do
84
+ subject.must_respond_to :non_admins
85
+ subject.must_respond_to :non_authors
86
+ subject.must_respond_to :non_viewers
87
+ end
88
+
89
+ describe "finding instances with a role" do
90
+
91
+ describe "admins scope" do
92
+
93
+ subject { User.admins.sort_by(&:id) }
94
+
95
+ it "should return only admins" do
96
+ subject.must_equal [@admin_author_viewer, @admin_only].sort_by(&:id)
97
+ end
98
+
99
+ it "should not return non admins" do
100
+ subject.wont_include @no_role
101
+ subject.wont_include @author_viewer
102
+ subject.wont_include @author_only
103
+ subject.wont_include @viewer
104
+ end
105
+
106
+ end
107
+
108
+ describe "authors scope" do
109
+
110
+ subject { User.authors.sort_by(&:id) }
111
+
112
+ it "should return only authors" do
113
+ subject.must_equal [@admin_author_viewer, @author_viewer, @author_only].sort_by(&:id)
114
+ end
115
+
116
+ it "should not return non authors" do
117
+ subject.wont_include @no_role
118
+ subject.wont_include @admin_only
119
+ subject.wont_include @viewer
120
+ end
121
+
122
+ end
123
+
124
+ describe "viewers scope" do
125
+
126
+ subject { User.viewers.sort_by(&:id) }
127
+
128
+ it "should return only viewers" do
129
+ subject.must_equal [@admin_author_viewer, @author_viewer, @viewer].sort_by(&:id)
130
+ end
131
+
132
+ it "should not return non authors" do
133
+ subject.wont_include @no_role
134
+ subject.wont_include @admin_only
135
+ subject.wont_include @author_only
136
+ end
137
+
138
+ end
139
+
140
+ end
141
+
142
+ describe "finding instances without a role" do
143
+
144
+ describe "non_admins scope" do
145
+
146
+ subject { User.non_admins.sort_by(&:id) }
147
+
148
+ it "should return only non_admins" do
149
+ subject.must_equal [@no_role, @author_viewer, @viewer, @author_only].sort_by(&:id)
150
+ end
151
+
152
+ it "should not return admins" do
153
+ subject.wont_include @admin_author_viewer
154
+ subject.wont_include @admin_only
155
+ end
156
+
157
+ end
158
+
159
+ describe "non_authors scope" do
160
+
161
+ subject { User.non_authors.sort_by(&:id) }
162
+
163
+ it "should return only non_authors" do
164
+ subject.must_equal [@no_role, @viewer, @admin_only].sort_by(&:id)
165
+ end
166
+
167
+ it "should not return authors" do
168
+ subject.wont_include @admin_author_viewer
169
+ subject.wont_include @author_viewer
170
+ subject.wont_include @author_only
171
+ end
172
+
173
+ end
174
+
175
+ describe "non_viewers scope" do
176
+
177
+ subject { User.non_viewers.sort_by(&:id) }
178
+
179
+ it "should return only non_viewers" do
180
+ subject.must_equal [@no_role, @admin_only, @author_only].sort_by(&:id)
181
+ end
182
+
183
+ it "should not return viewers" do
184
+ subject.wont_include @admin_author_viewer
185
+ subject.wont_include @author_viewer
186
+ subject.wont_include @viewer
187
+ end
188
+
189
+ end
190
+
191
+ end
192
+
193
+ describe "with_any_role" do
194
+
195
+ describe "specifying admin only" do
196
+
197
+ subject { User.with_any_role(:admin).sort_by(&:id) }
198
+
199
+ it "should return only admins" do
200
+ subject.must_equal [@admin_author_viewer, @admin_only].sort_by(&:id)
201
+ end
202
+
203
+ it "should not return non admins" do
204
+ subject.wont_include @no_role
205
+ subject.wont_include @author_viewer
206
+ subject.wont_include @author_only
207
+ subject.wont_include @viewer
208
+ end
209
+
210
+ end
211
+
212
+ describe "specifying author only" do
213
+
214
+ subject { User.with_any_role(:author).sort_by(&:id) }
215
+
216
+ it "should return only authors" do
217
+ subject.must_equal [@admin_author_viewer, @author_viewer, @author_only].sort_by(&:id)
218
+ end
219
+
220
+ it "should not return non authors" do
221
+ subject.wont_include @no_role
222
+ subject.wont_include @admin_only
223
+ subject.wont_include @viewer
224
+ end
225
+
226
+ end
227
+
228
+ describe "specifying viewer only" do
229
+
230
+ subject { User.with_any_role(:viewer).sort_by(&:id) }
231
+
232
+ it "should return only viewers" do
233
+ subject.must_equal [@admin_author_viewer, @author_viewer, @viewer].sort_by(&:id)
234
+ end
235
+
236
+ it "should not return non authors" do
237
+ subject.wont_include @no_role
238
+ subject.wont_include @admin_only
239
+ subject.wont_include @author_only
240
+ end
241
+
242
+ end
243
+
244
+ describe "specifying admin and author" do
245
+
246
+ subject { User.with_any_role(:admin, :author).sort_by(&:id) }
247
+
248
+ it "should return only admins and authors" do
249
+ subject.must_equal [@admin_author_viewer, @author_viewer, @admin_only, @author_only].sort_by(&:id)
250
+ end
251
+
252
+ it "should not return non admins or authors" do
253
+ subject.wont_include @no_role
254
+ subject.wont_include @viewer
255
+ end
256
+
257
+ end
258
+
259
+ describe "specifying admin and viewer" do
260
+
261
+ subject { User.with_any_role(:admin, :viewer).sort_by(&:id) }
262
+
263
+ it "should return only admins and viewers" do
264
+ subject.must_equal [@admin_author_viewer, @author_viewer, @admin_only, @viewer].sort_by(&:id)
265
+ end
266
+
267
+ it "should not return non admins or viewers" do
268
+ subject.wont_include @no_role
269
+ subject.wont_include @author_only
270
+ end
271
+
272
+ end
273
+
274
+ describe "specifying author and viewer" do
275
+
276
+ subject { User.with_any_role(:author, :viewer).sort_by(&:id) }
277
+
278
+ it "should return only authors and viewers" do
279
+ subject.must_equal [@admin_author_viewer, @author_viewer, @author_only, @viewer].sort_by(&:id)
280
+ end
281
+
282
+ it "should not return non authors or viewers" do
283
+ subject.wont_include @no_role
284
+ subject.wont_include @admin_only
285
+ end
286
+
287
+ end
288
+
289
+ describe "specifying admin, author and viewer" do
290
+
291
+ subject { User.with_any_role(:admin, :author, :viewer).sort_by(&:id) }
292
+
293
+ it "should return only admins, authors and viewers" do
294
+ subject.must_equal [@admin_author_viewer, @author_viewer, @admin_only, @author_only, @viewer].sort_by(&:id)
295
+ end
296
+
297
+ it "should not return non admins, authors or viewers" do
298
+ subject.wont_include @no_role
299
+ end
300
+
301
+ end
302
+
303
+ end
304
+
305
+ describe "with_all_roles" do
306
+
307
+ describe "specifying admin only" do
308
+
309
+ subject { User.with_all_roles(:admin).sort_by(&:id) }
310
+
311
+ it "should return only admins" do
312
+ subject.must_equal [@admin_author_viewer, @admin_only].sort_by(&:id)
313
+ end
314
+
315
+ it "should not return non admins" do
316
+ subject.wont_include @no_role
317
+ subject.wont_include @author_viewer
318
+ subject.wont_include @author_only
319
+ subject.wont_include @viewer
320
+ end
321
+
322
+ end
323
+
324
+ describe "specifying author only" do
325
+
326
+ subject { User.with_all_roles(:author).sort_by(&:id) }
327
+
328
+ it "should return only authors" do
329
+ subject.must_equal [@admin_author_viewer, @author_viewer, @author_only].sort_by(&:id)
330
+ end
331
+
332
+ it "should not return non authors" do
333
+ subject.wont_include @no_role
334
+ subject.wont_include @admin_only
335
+ subject.wont_include @viewer
336
+ end
337
+
338
+ end
339
+
340
+ describe "specifying viewer only" do
341
+
342
+ subject { User.with_all_roles(:viewer).sort_by(&:id) }
343
+
344
+ it "should return only viewers" do
345
+ subject.must_equal [@admin_author_viewer, @author_viewer, @viewer].sort_by(&:id)
346
+ end
347
+
348
+ it "should not return non authors" do
349
+ subject.wont_include @no_role
350
+ subject.wont_include @admin_only
351
+ subject.wont_include @author_only
352
+ end
353
+
354
+ end
355
+
356
+ describe "specifying admin and author" do
357
+
358
+ subject { User.with_all_roles(:admin, :author).sort_by(&:id) }
359
+
360
+ it "should return only admins and authors" do
361
+ subject.must_equal [@admin_author_viewer].sort_by(&:id)
362
+ end
363
+
364
+ it "should not return non admin and authors" do
365
+ subject.wont_include @no_role
366
+ subject.wont_include @author_viewer
367
+ subject.wont_include @author_only
368
+ subject.wont_include @admin_only
369
+ subject.wont_include @viewer
370
+ end
371
+
372
+ end
373
+
374
+ describe "specifying admin and viewer" do
375
+
376
+ subject { User.with_all_roles(:admin, :viewer).sort_by(&:id) }
377
+
378
+ it "should return only admins and viewers" do
379
+ subject.must_equal [@admin_author_viewer].sort_by(&:id)
380
+ end
381
+
382
+ it "should not return non admins or viewers" do
383
+ subject.wont_include @no_role
384
+ subject.wont_include @author_viewer
385
+ subject.wont_include @author_only
386
+ subject.wont_include @admin_only
387
+ subject.wont_include @viewer
388
+ end
389
+
390
+ end
391
+
392
+ describe "specifying author and viewer" do
393
+
394
+ subject { User.with_all_roles(:author, :viewer).sort_by(&:id) }
395
+
396
+ it "should return only authors and viewers" do
397
+ subject.must_equal [@admin_author_viewer, @author_viewer].sort_by(&:id)
398
+ end
399
+
400
+ it "should not return non authors or viewers" do
401
+ subject.wont_include @no_role
402
+ subject.wont_include @admin_only
403
+ subject.wont_include @author_only
404
+ subject.wont_include @viewer
405
+ end
406
+
407
+ end
408
+
409
+ describe "specifying admin, author and viewer" do
410
+
411
+ subject { User.with_all_roles(:admin, :author, :viewer).sort_by(&:id) }
412
+
413
+ it "should return only admins, authors and viewers" do
414
+ subject.must_equal [@admin_author_viewer].sort_by(&:id)
415
+ end
416
+
417
+ it "should not return non admins, authors or viewers" do
418
+ subject.wont_include @no_role
419
+ subject.wont_include @author_viewer
420
+ subject.wont_include @author_only
421
+ subject.wont_include @admin_only
422
+ subject.wont_include @viewer
423
+ end
424
+
425
+ end
426
+
427
+ end
428
+
429
+ end
430
+
431
+ end
432
+
433
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: canard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-10 00:00:00.000000000Z
12
+ date: 2011-12-05 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &2153302940 !ruby/object:Gem::Requirement
16
+ requirement: &2160840240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '2'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2153302940
24
+ version_requirements: *2160840240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sqlite3
27
- requirement: &2153302520 !ruby/object:Gem::Requirement
27
+ requirement: &2160839820 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2153302520
35
+ version_requirements: *2160839820
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activerecord
38
- requirement: &2153302060 !ruby/object:Gem::Requirement
38
+ requirement: &2160839360 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2153302060
46
+ version_requirements: *2160839360
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: cancan
49
- requirement: &2153301640 !ruby/object:Gem::Requirement
49
+ requirement: &2160838940 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *2153301640
57
+ version_requirements: *2160838940
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: role_model
60
- requirement: &2153301220 !ruby/object:Gem::Requirement
60
+ requirement: &2160838520 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *2153301220
68
+ version_requirements: *2160838520
69
69
  description: Wraps CanCan and RoleModel up with a generator and makes role based authorisation
70
70
  really easy in Rails 3.x.
71
71
  email:
@@ -76,7 +76,6 @@ extra_rdoc_files: []
76
76
  files:
77
77
  - .gitignore
78
78
  - Gemfile
79
- - Gemfile.lock
80
79
  - MIT-LICENSE
81
80
  - README.rdoc
82
81
  - Rakefile
data/Gemfile.lock DELETED
@@ -1,41 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- canard (0.0.1)
5
- cancan
6
- role_model
7
-
8
- GEM
9
- remote: http://rubygems.org/
10
- specs:
11
- activemodel (3.1.0)
12
- activesupport (= 3.1.0)
13
- bcrypt-ruby (~> 3.0.0)
14
- builder (~> 3.0.0)
15
- i18n (~> 0.6)
16
- activerecord (3.1.0)
17
- activemodel (= 3.1.0)
18
- activesupport (= 3.1.0)
19
- arel (~> 2.2.1)
20
- tzinfo (~> 0.3.29)
21
- activesupport (3.1.0)
22
- multi_json (~> 1.0)
23
- arel (2.2.1)
24
- bcrypt-ruby (3.0.0)
25
- builder (3.0.0)
26
- cancan (1.6.5)
27
- i18n (0.6.0)
28
- minitest (2.5.1)
29
- multi_json (1.0.3)
30
- role_model (0.7.0)
31
- sqlite3 (1.3.4)
32
- tzinfo (0.3.29)
33
-
34
- PLATFORMS
35
- ruby
36
-
37
- DEPENDENCIES
38
- activerecord
39
- canard!
40
- minitest (~> 2)
41
- sqlite3