role_model 0.7.1 → 0.8.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.
- data/.travis.yml +1 -0
- data/README.rdoc +10 -4
- data/VERSION +1 -1
- data/lib/role_model/class_methods.rb +20 -0
- data/lib/role_model/implementation.rb +15 -0
- data/role_model.gemspec +2 -2
- data/spec/role_model_spec.rb +100 -0
- metadata +5 -5
data/.travis.yml
CHANGED
data/README.rdoc
CHANGED
@@ -11,12 +11,18 @@ It works like this:
|
|
11
11
|
require 'role_model'
|
12
12
|
|
13
13
|
class User
|
14
|
-
attr_accessor :roles_mask
|
15
|
-
|
14
|
+
attr_accessor :roles_mask # just for demo purposes
|
15
|
+
# in real life this would usuallt be a persistent attribute,
|
16
|
+
# e.g. if your User model is persisted in a SQL-DB add an integer
|
17
|
+
# column named roles_mask to your users table -- just remove/replace
|
18
|
+
# above attr_accessor line with whatever is needed for your
|
19
|
+
# persistence solution
|
20
|
+
|
16
21
|
include RoleModel
|
17
22
|
|
18
|
-
#
|
19
|
-
#
|
23
|
+
# if you want to use a different integer attribute to store the
|
24
|
+
# roles in, set it with roles_attribute :my_roles_attribute,
|
25
|
+
# :roles_mask is the default name
|
20
26
|
roles_attribute :roles_mask
|
21
27
|
|
22
28
|
# declare the valid roles -- do not change the order if you add more
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.8.0
|
@@ -34,7 +34,27 @@ module RoleModel
|
|
34
34
|
#
|
35
35
|
# declare valid roles
|
36
36
|
def roles(*roles)
|
37
|
+
opts = roles.last.is_a?(Hash) ? roles.pop : {}
|
37
38
|
self.valid_roles = roles.flatten.map(&:to_sym)
|
39
|
+
unless (opts[:dynamic] == false)
|
40
|
+
self.define_dynamic_queries(self.valid_roles)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Defines dynamic queries for :role
|
45
|
+
# #is_<:role>?
|
46
|
+
# #<:role>?
|
47
|
+
#
|
48
|
+
# Defines new methods which call #is?(:role)
|
49
|
+
def define_dynamic_queries(roles)
|
50
|
+
dynamic_module = Module.new do
|
51
|
+
roles.each do |role|
|
52
|
+
["#{role}?".to_sym, "is_#{role}?".to_sym].each do |method|
|
53
|
+
define_method(method) { is? role }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
include dynamic_module
|
38
58
|
end
|
39
59
|
end
|
40
60
|
end
|
@@ -44,5 +44,20 @@ module RoleModel
|
|
44
44
|
alias_method :is_any_of?, :has_any_role?
|
45
45
|
alias_method :has_role?, :has_any_role?
|
46
46
|
|
47
|
+
# :call-seq:
|
48
|
+
# has_only_roles?(:role)
|
49
|
+
# has_only_roles?('role')
|
50
|
+
# has_only_roles?(:role_1, ..., :role_n)
|
51
|
+
# has_only_roles?('role_1', ..., 'role_n')
|
52
|
+
# has_only_roles?([:role_1, ..., :role_n])
|
53
|
+
# has_only_roles?(['role_1', ..., 'role_n'])
|
54
|
+
#
|
55
|
+
# check if ONLY of the given roles have been assigned
|
56
|
+
# this method is aliased as #is_exactly?
|
57
|
+
def has_only_roles?(*roles)
|
58
|
+
self.send("#{self.class.roles_attribute_name}") == self.class.mask_for(*roles)
|
59
|
+
end
|
60
|
+
alias_method :is_exactly?, :has_only_roles?
|
61
|
+
|
47
62
|
end
|
48
63
|
end
|
data/role_model.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "role_model"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.8.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Martin Rehfeld"]
|
12
|
-
s.date = "2012-
|
12
|
+
s.date = "2012-10-29"
|
13
13
|
s.description = "Ever needed to assign roles to a model, say a User, and build conditional behaviour on top of that? Enter RoleModel -- roles have never been easier! Just declare your roles and you are done. Assigned roles will be stored as a bitmask."
|
14
14
|
s.email = "martin.rehfeld@glnetworks.de"
|
15
15
|
s.extra_rdoc_files = [
|
data/spec/role_model_spec.rb
CHANGED
@@ -296,6 +296,78 @@ describe RoleModel do
|
|
296
296
|
end
|
297
297
|
end
|
298
298
|
end
|
299
|
+
|
300
|
+
describe "dynamically query for an individual role" do
|
301
|
+
subject { model_class.new }
|
302
|
+
|
303
|
+
it "should return true when the given role was assigned" do
|
304
|
+
subject.roles = :foo
|
305
|
+
subject.is_foo?.should be_true
|
306
|
+
subject.foo?.should be_true
|
307
|
+
end
|
308
|
+
|
309
|
+
it "should return false when the given role was not assigned" do
|
310
|
+
subject.roles = :bar
|
311
|
+
subject.is_foo?.should be_false
|
312
|
+
subject.foo?.should be_false
|
313
|
+
end
|
314
|
+
|
315
|
+
it "should return false when no role was assigned" do
|
316
|
+
subject.is_foo?.should be_false
|
317
|
+
subject.bar?.should be_false
|
318
|
+
end
|
319
|
+
|
320
|
+
it "should throw NoMethodError when asked for an undefined role" do
|
321
|
+
lambda { subject.baz? }.should raise_error(NoMethodError)
|
322
|
+
lambda { subject.is_baz? }.should raise_error(NoMethodError)
|
323
|
+
end
|
324
|
+
|
325
|
+
it "should not define dynamic finders when opting out" do
|
326
|
+
non_dynamic_klass = Class.new do
|
327
|
+
attr_accessor :roles_mask
|
328
|
+
attr_accessor :custom_roles_mask
|
329
|
+
include RoleModel
|
330
|
+
roles :foo, :bar, :third, :dynamic => false
|
331
|
+
end
|
332
|
+
|
333
|
+
model = non_dynamic_klass.new
|
334
|
+
lambda { model.is_foo? }.should raise_error(NoMethodError)
|
335
|
+
lambda { model.bar? }.should raise_error(NoMethodError)
|
336
|
+
end
|
337
|
+
|
338
|
+
it "should be able to override the default dynamic query methods and call super" do
|
339
|
+
klass = Class.new do
|
340
|
+
def bar?
|
341
|
+
return false
|
342
|
+
end
|
343
|
+
|
344
|
+
attr_accessor :roles_mask
|
345
|
+
attr_accessor :custom_roles_mask
|
346
|
+
include RoleModel
|
347
|
+
roles :foo, :bar, :baz
|
348
|
+
|
349
|
+
def is_baz?
|
350
|
+
return false
|
351
|
+
end
|
352
|
+
|
353
|
+
def foo?
|
354
|
+
ret = super
|
355
|
+
!ret
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
model = klass.new
|
360
|
+
model.roles = [:foo, :bar, :baz]
|
361
|
+
|
362
|
+
model.foo?.should be_false
|
363
|
+
|
364
|
+
model.bar?.should be_false
|
365
|
+
model.is_bar?.should be_true
|
366
|
+
|
367
|
+
model.is_baz?.should be_false
|
368
|
+
model.baz?.should be_true
|
369
|
+
end
|
370
|
+
end
|
299
371
|
|
300
372
|
context "query for multiple roles" do
|
301
373
|
[:has_any_role?, :is_any_of?, :has_role?].each do |check_role_assignment_method|
|
@@ -318,6 +390,11 @@ describe RoleModel do
|
|
318
390
|
describe "##{check_role_assignment_method}" do
|
319
391
|
subject { model_class.new }
|
320
392
|
|
393
|
+
it "returns true when the assigned roles include the given roles" do
|
394
|
+
subject.roles = [:foo, :bar, :third]
|
395
|
+
subject.send(check_role_assignment_method, :foo, :bar).should be_true
|
396
|
+
end
|
397
|
+
|
321
398
|
it "should return true when all of the given roles were assigned" do
|
322
399
|
subject.roles = [:foo, :bar]
|
323
400
|
subject.send(check_role_assignment_method, :foo, :bar).should be_true
|
@@ -336,6 +413,29 @@ describe RoleModel do
|
|
336
413
|
end
|
337
414
|
end
|
338
415
|
|
416
|
+
context "query for exact roles" do
|
417
|
+
[:has_only_roles?, :is_exactly?].each do |check_role_assignment_method|
|
418
|
+
describe "##{check_role_assignment_method}" do
|
419
|
+
subject { model_class.new }
|
420
|
+
|
421
|
+
it "returns false when the given roles are a subset of those assigned" do
|
422
|
+
subject.roles = [:foo, :bar, :third]
|
423
|
+
subject.send(check_role_assignment_method, :foo, :bar).should be_false
|
424
|
+
end
|
425
|
+
|
426
|
+
it "returns true when the given roles exactly match those assigned" do
|
427
|
+
subject.roles = [:foo, :bar]
|
428
|
+
subject.send(check_role_assignment_method, :foo, :bar).should be_true
|
429
|
+
end
|
430
|
+
|
431
|
+
it "should return false when only some of the given roles were assigned" do
|
432
|
+
subject.roles = [:foo, :bar]
|
433
|
+
subject.send(check_role_assignment_method, :bar, :baz).should be_false
|
434
|
+
end
|
435
|
+
end
|
436
|
+
end
|
437
|
+
end
|
438
|
+
|
339
439
|
context "query for roles when none defined in model" do
|
340
440
|
[:has_any_role?, :is_any_of?, :has_role?, :has_all_roles?, :is?, :has_roles?].each do |check_role_assignment_method|
|
341
441
|
describe "##{check_role_assignment_method}" do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: role_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 63
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 8
|
9
|
+
- 0
|
10
|
+
version: 0.8.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Martin Rehfeld
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-10-29 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
requirement: &id001 !ruby/object:Gem::Requirement
|