roles_active_record 0.4.6.3 → 0.4.6.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/roles_active_record/strategy/multi/many_roles.rb +1 -0
- data/lib/roles_active_record/strategy/multi.rb +3 -1
- data/lib/roles_active_record/strategy/shared.rb +12 -0
- data/lib/roles_active_record/strategy/single/one_role.rb +4 -2
- data/roles_active_record.gemspec +1 -1
- data/spec/roles_active_record/strategy/api_examples.rb +74 -5
- data/spec/roles_active_record/strategy/user_setup.rb +3 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.6.
|
1
|
+
0.4.6.4
|
@@ -6,6 +6,18 @@ module Roles::ActiveRecord
|
|
6
6
|
end
|
7
7
|
alias_method :set_roles, :set_role
|
8
8
|
|
9
|
+
def remove_all_roles!
|
10
|
+
set_empty_role
|
11
|
+
end
|
12
|
+
|
13
|
+
def has_no_roles?
|
14
|
+
get_role.empty?
|
15
|
+
end
|
16
|
+
|
17
|
+
def has_any_roles?
|
18
|
+
!has_no_roles?
|
19
|
+
end
|
20
|
+
|
9
21
|
def get_role
|
10
22
|
r = self.send(role_attribute)
|
11
23
|
end
|
@@ -25,9 +25,11 @@ module RoleStrategy::ActiveRecord
|
|
25
25
|
module Implementation
|
26
26
|
include Roles::ActiveRecord::Strategy::Single
|
27
27
|
|
28
|
-
def set_role role
|
28
|
+
def set_role role
|
29
|
+
# set_empty_role
|
29
30
|
role = role.first if role.kind_of? Array
|
30
|
-
|
31
|
+
self.send("#{role_attribute}=", role)
|
32
|
+
# role.users << self
|
31
33
|
end
|
32
34
|
alias_method :set_roles, :set_role
|
33
35
|
|
data/roles_active_record.gemspec
CHANGED
@@ -21,7 +21,7 @@ describe "Roles for Active Record: #{api_name}" do
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
describe '#in_any_role' do
|
26
26
|
it "should return first user matching role" do
|
27
27
|
if User.respond_to? :in_roles
|
@@ -175,6 +175,13 @@ describe "Roles for Active Record: #{api_name}" do
|
|
175
175
|
@guest_user.has_role?(:admin).should be_true
|
176
176
|
@guest_user.roles = :guest
|
177
177
|
end
|
178
|
+
|
179
|
+
context 'the guest user' do
|
180
|
+
it "should be valid after setting the roles" do
|
181
|
+
@guest_user.should be_valid
|
182
|
+
lambda {@guest_user.save!}.should_not raise_error
|
183
|
+
end
|
184
|
+
end
|
178
185
|
end
|
179
186
|
|
180
187
|
describe '#role=' do
|
@@ -182,16 +189,22 @@ describe "Roles for Active Record: #{api_name}" do
|
|
182
189
|
@guest_user.role = :admin
|
183
190
|
@guest_user.has_role?(:admin).should be_true
|
184
191
|
@guest_user.has_role?(:guest).should be_false
|
185
|
-
@guest_user.role = :guest
|
192
|
+
@guest_user.role = :guest
|
186
193
|
@guest_user.has_role?(:guest).should be_true
|
187
194
|
@guest_user.has_role?(:admin).should be_false
|
188
195
|
|
189
|
-
u = User.new
|
196
|
+
u = User.new :name => 'kris'
|
190
197
|
u.role = :admin
|
191
198
|
u.has_role?(:admin).should be_true
|
192
199
|
end
|
193
|
-
|
194
|
-
|
200
|
+
|
201
|
+
context 'the guest user' do
|
202
|
+
it "should be valid after setting the role" do
|
203
|
+
@guest_user.should be_valid
|
204
|
+
lambda {@guest_user.save!}.should_not raise_error
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
195
208
|
|
196
209
|
describe '#exchange_roles' do
|
197
210
|
it "should exchange user role :user with role :admin" do
|
@@ -211,6 +224,52 @@ describe "Roles for Active Record: #{api_name}" do
|
|
211
224
|
@admin_user.has?(:admin).should be_false
|
212
225
|
end
|
213
226
|
end
|
227
|
+
|
228
|
+
context 'the admin user' do
|
229
|
+
it "should be valid after setting the role" do
|
230
|
+
@admin_user.should be_valid
|
231
|
+
lambda {@admin_user.save!}.should_not raise_error
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
describe '#add_role' do
|
237
|
+
it "should add user role :admin and :guest using #add_roles" do
|
238
|
+
@empty_user.add_role :admin
|
239
|
+
@admin_user.has_roles?(:admin).should be_true
|
240
|
+
end
|
241
|
+
|
242
|
+
context 'the empty user' do
|
243
|
+
it "should be valid after setting the role" do
|
244
|
+
@empty_user.should be_valid
|
245
|
+
end
|
246
|
+
|
247
|
+
it "should be able to save the valid user and not raise an error" do
|
248
|
+
lambda {@empty_user.save!}.should_not raise_error
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
describe '#add_roles' do
|
254
|
+
it "should add user role :admin and :guest using #add_roles" do
|
255
|
+
if @empty_user.class.role_strategy.multiplicity == :multi
|
256
|
+
@empty_user.remove_all_roles!
|
257
|
+
@empty_user.has_no_roles?.should be_true
|
258
|
+
@empty_user.has_any_roles?.should be_false
|
259
|
+
@empty_user.add_roles :admin, :guest
|
260
|
+
@empty_user.has_roles?(:admin, :guest).should be_true
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
context 'the empty user' do
|
265
|
+
it "should be valid after setting the role" do
|
266
|
+
@empty_user.should be_valid
|
267
|
+
end
|
268
|
+
|
269
|
+
it "should be able to save the valid user and not raise an error" do
|
270
|
+
lambda {@empty_user.save!}.should_not raise_error
|
271
|
+
end
|
272
|
+
end
|
214
273
|
end
|
215
274
|
|
216
275
|
describe '#remove_roles' do
|
@@ -225,5 +284,15 @@ describe "Roles for Active Record: #{api_name}" do
|
|
225
284
|
@guest_user.remove_role :admin
|
226
285
|
@guest_user.has_role?(:admin).should_not be_true
|
227
286
|
end
|
287
|
+
|
288
|
+
context 'the guest user' do
|
289
|
+
it "should be valid after setting the role" do
|
290
|
+
@guest_user.should be_valid
|
291
|
+
end
|
292
|
+
|
293
|
+
it "should be able to save the valid user and not raise an error" do
|
294
|
+
lambda {@guest_user.save!}.should_not raise_error
|
295
|
+
end
|
296
|
+
end
|
228
297
|
end
|
229
298
|
end
|