role_model 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +12 -1
- data/VERSION +1 -1
- data/lib/role_model/implementation.rb +31 -5
- data/role_model.gemspec +2 -2
- data/spec/role_model_spec.rb +84 -15
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -25,7 +25,7 @@ It works like this:
|
|
25
25
|
end
|
26
26
|
|
27
27
|
#
|
28
|
-
# Test drive
|
28
|
+
# Test drive (check the RDoc or source for additional finesse)
|
29
29
|
#
|
30
30
|
|
31
31
|
>> u = User.new
|
@@ -41,6 +41,10 @@ It works like this:
|
|
41
41
|
|
42
42
|
# querying roles...
|
43
43
|
|
44
|
+
# get all valid roles that have been declared
|
45
|
+
>> User.valid_roles
|
46
|
+
=> [:admin, :manager, :author]
|
47
|
+
|
44
48
|
# ... retrieve all assigned roles
|
45
49
|
>> u.roles # aliased to role_symbols for DeclarativeAuthorization
|
46
50
|
=> [:admin, :manager]
|
@@ -49,6 +53,13 @@ It works like this:
|
|
49
53
|
>> u.has_role? :author # has_role? is also aliased to is?
|
50
54
|
=> false
|
51
55
|
|
56
|
+
# ... check for multiple roles
|
57
|
+
>> u.has_any_role? :author, :manager # has_any_role? is also aliased to is_any_of?
|
58
|
+
=> true
|
59
|
+
|
60
|
+
>> u.has_all_roles? :author, :manager # has_all_roles? is also aliased to is?
|
61
|
+
=> false
|
62
|
+
|
52
63
|
# see the internal bitmask representation (3 = 0b0011)
|
53
64
|
>> u.roles_mask
|
54
65
|
=> 3
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.0
|
@@ -10,13 +10,39 @@ module RoleModel
|
|
10
10
|
def roles
|
11
11
|
Roles.new(self, self.class.valid_roles.reject { |r| ((self.send(self.class.roles_attribute_name) || 0) & 2**self.class.valid_roles.index(r)).zero? })
|
12
12
|
end
|
13
|
-
|
13
|
+
alias_method :role_symbols, :roles
|
14
14
|
|
15
|
-
#
|
16
|
-
|
17
|
-
|
15
|
+
# :call-seq:
|
16
|
+
# has_all_roles?(:role)
|
17
|
+
# has_all_roles?('role')
|
18
|
+
# has_all_roles?(:role_1, ..., :role_n)
|
19
|
+
# has_all_roles?('role_1', ..., 'role_n')
|
20
|
+
# has_all_roles?([:role_1, ..., :role_n])
|
21
|
+
# has_all_roles?(['role_1', ..., 'role_n'])
|
22
|
+
#
|
23
|
+
# check if ALL of the given roles have been assigned
|
24
|
+
# this method is aliased as #is? and #has_roles?
|
25
|
+
def has_all_roles?(*roles)
|
26
|
+
roles.flatten.map { |r| r.to_sym }.all? { |r| self.roles.include?(r) }
|
18
27
|
end
|
19
|
-
|
28
|
+
alias_method :is?, :has_all_roles?
|
29
|
+
alias_method :has_roles?, :has_all_roles?
|
30
|
+
|
31
|
+
# :call-seq:
|
32
|
+
# has_any_role?(:role)
|
33
|
+
# has_any_role?('role')
|
34
|
+
# has_any_role?(:role_1, ..., :role_n)
|
35
|
+
# has_any_role?('role_1', ..., 'role_n')
|
36
|
+
# has_any_role?([:role_1, ..., :role_n])
|
37
|
+
# has_any_role?(['role_1', ..., 'role_n'])
|
38
|
+
#
|
39
|
+
# check if any (at least ONE) of the given roles have been assigned
|
40
|
+
# this method is aliased as #is_any_of? and #has_role?
|
41
|
+
def has_any_role?(*roles)
|
42
|
+
roles.flatten.map { |r| r.to_sym }.any? { |r| self.roles.include?(r) }
|
43
|
+
end
|
44
|
+
alias_method :is_any_of?, :has_any_role?
|
45
|
+
alias_method :has_role?, :has_any_role?
|
20
46
|
|
21
47
|
end
|
22
48
|
end
|
data/role_model.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{role_model}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.5.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 = %q{2010-08-
|
12
|
+
s.date = %q{2010-08-17}
|
13
13
|
s.description = %q{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 = %q{martin.rehfeld@glnetworks.de}
|
15
15
|
s.extra_rdoc_files = [
|
data/spec/role_model_spec.rb
CHANGED
@@ -180,27 +180,96 @@ describe RoleModel do
|
|
180
180
|
end
|
181
181
|
end
|
182
182
|
|
183
|
-
|
184
|
-
|
185
|
-
|
183
|
+
context "query for an individual role" do
|
184
|
+
[:has_any_role?, :is_any_of?, :has_role?, :has_all_roles?, :is?, :has_roles?].each do |check_role_assignment_method|
|
185
|
+
describe "##{check_role_assignment_method}" do
|
186
|
+
subject { model_class.new }
|
187
|
+
|
188
|
+
it "should return true when the given role was assigned" do
|
189
|
+
subject.roles = :foo
|
190
|
+
subject.send(check_role_assignment_method, :foo).should be_true
|
191
|
+
end
|
186
192
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
193
|
+
it "should return false when the given role was not assigned" do
|
194
|
+
subject.roles = :bar
|
195
|
+
subject.send(check_role_assignment_method, :foo).should be_false
|
196
|
+
end
|
191
197
|
|
192
|
-
|
193
|
-
|
194
|
-
|
198
|
+
it "should return false when no role was assigned" do
|
199
|
+
subject.send(check_role_assignment_method, :foo).should be_false
|
200
|
+
subject.send(check_role_assignment_method, :bar).should be_false
|
201
|
+
end
|
202
|
+
|
203
|
+
it "should return false when asked for an undefined role" do
|
204
|
+
subject.send(check_role_assignment_method, :baz).should be_false
|
205
|
+
end
|
195
206
|
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
context "query for multiple roles" do
|
211
|
+
[:has_any_role?, :is_any_of?, :has_role?].each do |check_role_assignment_method|
|
212
|
+
describe "##{check_role_assignment_method}" do
|
213
|
+
subject { model_class.new }
|
214
|
+
|
215
|
+
it "should return true when the given role was assigned" do
|
216
|
+
subject.roles = :foo
|
217
|
+
subject.send(check_role_assignment_method, :foo).should be_true
|
218
|
+
end
|
219
|
+
|
220
|
+
it "should return true when any of the given roles was assigned" do
|
221
|
+
subject.roles = :foo
|
222
|
+
subject.send(check_role_assignment_method, :foo, :bar).should be_true
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should return false when the given role was not assigned" do
|
226
|
+
subject.roles = :bar
|
227
|
+
subject.send(check_role_assignment_method, :foo).should be_false
|
228
|
+
end
|
229
|
+
|
230
|
+
it "should return false when no role was assigned" do
|
231
|
+
subject.send(check_role_assignment_method, :foo).should be_false
|
232
|
+
subject.send(check_role_assignment_method, :bar).should be_false
|
233
|
+
end
|
196
234
|
|
197
|
-
|
198
|
-
|
199
|
-
|
235
|
+
it "should return false when asked for an undefined role" do
|
236
|
+
subject.send(check_role_assignment_method, :baz).should be_false
|
237
|
+
end
|
200
238
|
end
|
239
|
+
end
|
201
240
|
|
202
|
-
|
203
|
-
|
241
|
+
[:has_all_roles?, :is?, :has_roles?].each do |check_role_assignment_method|
|
242
|
+
describe "##{check_role_assignment_method}" do
|
243
|
+
subject { model_class.new }
|
244
|
+
|
245
|
+
it "should return true when the given role was assigned" do
|
246
|
+
subject.roles = :foo
|
247
|
+
subject.send(check_role_assignment_method, :foo).should be_true
|
248
|
+
end
|
249
|
+
|
250
|
+
it "should return true when all of the given roles was assigned" do
|
251
|
+
subject.roles = [:foo, :bar]
|
252
|
+
subject.send(check_role_assignment_method, :foo, :bar).should be_true
|
253
|
+
end
|
254
|
+
|
255
|
+
it "should return false when not all of the given roles were assigned" do
|
256
|
+
subject.roles = [:foo, :bar]
|
257
|
+
subject.send(check_role_assignment_method, :bar, :baz).should be_false
|
258
|
+
end
|
259
|
+
|
260
|
+
it "should return false when the given role was not assigned" do
|
261
|
+
subject.roles = :bar
|
262
|
+
subject.send(check_role_assignment_method, :foo).should be_false
|
263
|
+
end
|
264
|
+
|
265
|
+
it "should return false when no role was assigned" do
|
266
|
+
subject.send(check_role_assignment_method, :foo).should be_false
|
267
|
+
subject.send(check_role_assignment_method, :bar).should be_false
|
268
|
+
end
|
269
|
+
|
270
|
+
it "should return false when asked for an undefined role" do
|
271
|
+
subject.send(check_role_assignment_method, :baz).should be_false
|
272
|
+
end
|
204
273
|
end
|
205
274
|
end
|
206
275
|
end
|
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: 11
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 5
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.5.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: 2010-08-
|
18
|
+
date: 2010-08-17 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|