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 CHANGED
@@ -1 +1 @@
1
- 0.4.6.3
1
+ 0.4.6.4
@@ -45,6 +45,7 @@ module RoleStrategy::ActiveRecord
45
45
  def set_empty_roles
46
46
  self.send("#{role_attribute}=", [])
47
47
  end
48
+ alias_method :set_empty_role, :set_empty_roles
48
49
  end
49
50
 
50
51
  extend Roles::Generic::User::Configuration
@@ -2,7 +2,9 @@ require 'roles_active_record/strategy/shared'
2
2
 
3
3
  module Roles::ActiveRecord
4
4
  module Strategy
5
- module Multi
5
+ module Multi
6
+ include Shared
7
+
6
8
  # assign multiple roles
7
9
  def roles=(*role_names)
8
10
  extracted_roles = extract_roles(role_names)
@@ -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
- role.users << self
31
+ self.send("#{role_attribute}=", role)
32
+ # role.users << self
31
33
  end
32
34
  alias_method :set_roles, :set_role
33
35
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{roles_active_record}
8
- s.version = "0.4.6.3"
8
+ s.version = "0.4.6.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
@@ -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
- end
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
@@ -1,4 +1,7 @@
1
1
  def default_user_setup
2
+ @empty_user = User.create(:name => 'Empty user')
3
+ @empty_user.save
4
+
2
5
  @guest_user = User.create(:name => 'Guest user')
3
6
  @guest_user.add_roles :guest
4
7
  @guest_user.save
metadata CHANGED
@@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version
6
6
  - 0
7
7
  - 4
8
8
  - 6
9
- - 3
10
- version: 0.4.6.3
9
+ - 4
10
+ version: 0.4.6.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kristian Mandrup