roles_active_record 0.4.8 → 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.textile CHANGED
@@ -1,6 +1,12 @@
1
1
  h1. Changelog
2
2
 
3
- h2. V. 0.4.6.5
3
+ h2. May 9, 2011
4
+
5
+ *version 0.4.9*
6
+
7
+ - Added #default_role and #default_role instance methods and #default_role and #default_role= class methods.
8
+ - The #default_role class method attempts to set the default role of the class to an available role that matches the class name, or the class name with User at the end removed (fx GuestUser becomes :guest).
9
+
10
+
4
11
 
5
- * Feb 9/2011 - merged johanlunds/master to support Rubygems 1.5 and resolve dependencies
6
12
 
data/README.textile CHANGED
@@ -50,9 +50,16 @@ h3. Strategy: admin_flag
50
50
 
51
51
  strategy :admin_flag, :default
52
52
  valid_roles_are :admin, :guest
53
+
54
+ def initialize attributes = {}
55
+ super
56
+ add_role default_role
57
+ end
53
58
  end
54
59
  </pre>
55
60
 
61
+ Here the initializer initially sets the role of the user tothe default_role, which is the available role matching the class name (or nil in case no match)
62
+
56
63
  h3. Strategy: one_role
57
64
 
58
65
  For strategies that use a separate Role model you must call the class method #role_class with the name of the role class
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.8
1
+ 0.4.9
@@ -10,10 +10,28 @@ module Roles::ActiveRecord
10
10
  def self.included(base)
11
11
  base.extend Roles::Base
12
12
  base.extend ClassMethods
13
+ base.send :include, InstanceMethods
13
14
  base.orm_name = :active_record
14
15
  end
15
16
 
17
+ module InstanceMethods
18
+ def default_role?
19
+ has_role?(default_role)
20
+ end
21
+
22
+ def default_role
23
+ self.class.default_role
24
+ end
25
+ end
26
+
16
27
  module ClassMethods
28
+ def default_role
29
+ self.to_s.gsub(/(.+)User$/, '\1').underscore.to_sym
30
+ end
31
+
32
+ def default_role= drole
33
+ @default_role = drole
34
+ end
17
35
 
18
36
  def valid_single_strategies
19
37
  [:admin_flag, :one_role, :role_string]
@@ -10,7 +10,7 @@ module Roles::Base
10
10
  end
11
11
  end
12
12
  end
13
- end
13
+ end
14
14
  end
15
15
 
16
16
  module RoleClass
@@ -1,6 +1,6 @@
1
1
  module Roles::ActiveRecord
2
2
  module Strategy
3
- module Shared
3
+ module Shared
4
4
  def set_role role
5
5
  update_attributes(role_attribute => new_role(role))
6
6
  end
@@ -9,7 +9,8 @@ end
9
9
  module Roles::ActiveRecord
10
10
  module Strategy
11
11
  module Single
12
- include Shared
12
+ include Shared
13
+
13
14
  # assigns first valid role from list of roles
14
15
  def add_roles *role_names
15
16
  new_roles = select_valid_roles(role_names)
@@ -28,6 +28,7 @@ module RoleStrategy::ActiveRecord
28
28
  def set_role role
29
29
  # set_empty_role
30
30
  role = role.first if role.kind_of? Array
31
+ # puts "set_role: #{role.inspect}"
31
32
  self.send("#{role_attribute}=", role)
32
33
  # role.users << self
33
34
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{roles_active_record}
8
- s.version = "0.4.8"
8
+ s.version = "0.4.9"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Kristian Mandrup}]
12
- s.date = %q{2011-05-08}
12
+ s.date = %q{2011-05-09}
13
13
  s.description = %q{Makes it easy to set a role strategy on your User model in Active Record}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -3,4 +3,10 @@ class User < ActiveRecord::Base
3
3
 
4
4
  strategy :many_roles
5
5
  valid_roles_are :admin, :guest, :user
6
+
7
+ def initialize attributes = {}
8
+ super
9
+ # role = default_role
10
+ add_role default_role
11
+ end
6
12
  end
@@ -3,4 +3,10 @@ class User < ActiveRecord::Base
3
3
 
4
4
  strategy :one_role
5
5
  valid_roles_are :admin, :guest, :user
6
+
7
+ def initialize attributes = {}
8
+ super
9
+ # role = default_role
10
+ add_role default_role
11
+ end
6
12
  end
@@ -24,7 +24,6 @@ describe 'Roles Generic API' do
24
24
  end
25
25
  end
26
26
 
27
-
28
27
  # it "should be true that a User that includes Roles::Generic has a complete Roles::Generic interface" do
29
28
  # # mutation API
30
29
  # [:roles=, :role=, :add_roles, :add_role, :remove_role, :remove_roles, :exchange_roles, :exchange_role].each do |api_method|
@@ -47,6 +47,29 @@ describe "Roles for Active Record: #{api_name}" do
47
47
  @admin_user.class.respond_to?(class_api_method).should be_true
48
48
  end
49
49
  end
50
+
51
+ describe '#default_role' do
52
+ it "should be true that the default user role is empty when no valid role matches class name in lowercase (no :user)" do
53
+ @default_user.class.default_role.should == :user
54
+ @default_user.default_role.should == :user
55
+ @default_user.has_role?(:user).should be_true
56
+ end
57
+
58
+ it "should be true that the User class has a valid role of :guest" do
59
+ @default_user.valid_role?(:user).should be_true
60
+ end
61
+ end
62
+
63
+ describe '#default_role?' do
64
+ it "should be true that the default User has a role that is the default role" do
65
+ @default_user.default_role?.should be_true
66
+ end
67
+
68
+ it "should not be that after changing the role it is still the default role" do
69
+ @default_user.role = :admin
70
+ @default_user.default_role?.should be_false
71
+ end
72
+ end
50
73
 
51
74
  describe '#valid_role?' do
52
75
  it "should be true that the admin user has a valid role of :guest" do
@@ -197,7 +220,7 @@ describe "Roles for Active Record: #{api_name}" do
197
220
  u.role = :admin
198
221
  u.has_role?(:admin).should be_true
199
222
  end
200
-
223
+
201
224
  context 'the guest user' do
202
225
  it "should be valid after setting the role" do
203
226
  @guest_user.should be_valid
@@ -232,7 +255,7 @@ describe "Roles for Active Record: #{api_name}" do
232
255
  end
233
256
  end
234
257
  end
235
-
258
+
236
259
  describe '#add_role' do
237
260
  it "should add user role :admin and :guest using #add_roles" do
238
261
  @empty_user.add_role :admin
@@ -249,7 +272,7 @@ describe "Roles for Active Record: #{api_name}" do
249
272
  end
250
273
  end
251
274
  end
252
-
275
+
253
276
  describe '#add_roles' do
254
277
  it "should add user role :admin and :guest using #add_roles" do
255
278
  if @empty_user.class.role_strategy.multiplicity == :multi
@@ -13,6 +13,8 @@ def default_user_setup
13
13
  @admin_user = User.create(:name => 'Admin user')
14
14
  @admin_user.roles = :admin
15
15
  @admin_user.save
16
-
16
+
17
+ @default_user = User.create(:name => 'def User')
18
+ @default_user.save
17
19
  # puts "Admin roles: #{@admin_user.roles_list}, #{@admin_user.inspect}, #{Role.all}"
18
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roles_active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.8
4
+ version: 0.4.9
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-05-08 00:00:00.000000000Z
12
+ date: 2011-05-09 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: database_cleaner
16
- requirement: &2156655440 !ruby/object:Gem::Requirement
16
+ requirement: &2157347720 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0.6'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2156655440
24
+ version_requirements: *2157347720
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &2156654800 !ruby/object:Gem::Requirement
27
+ requirement: &2157346800 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.4.1
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2156654800
35
+ version_requirements: *2157346800
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: generator-spec
38
- requirement: &2156654120 !ruby/object:Gem::Requirement
38
+ requirement: &2157346080 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.7.3
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2156654120
46
+ version_requirements: *2157346080
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: migration_assist
49
- requirement: &2156653540 !ruby/object:Gem::Requirement
49
+ requirement: &2157345380 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 0.2.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2156653540
57
+ version_requirements: *2157345380
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: roles_generic
60
- requirement: &2156652700 !ruby/object:Gem::Requirement
60
+ requirement: &2157344520 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 0.3.8
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *2156652700
68
+ version_requirements: *2157344520
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: require_all
71
- requirement: &2156652100 !ruby/object:Gem::Requirement
71
+ requirement: &2157343880 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 1.2.0
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *2156652100
79
+ version_requirements: *2157343880
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: sugar-high
82
- requirement: &2156651460 !ruby/object:Gem::Requirement
82
+ requirement: &2157343260 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 0.4.0
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *2156651460
90
+ version_requirements: *2157343260
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: meta_where
93
- requirement: &2156650660 !ruby/object:Gem::Requirement
93
+ requirement: &2157342640 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: 1.0.1
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *2156650660
101
+ version_requirements: *2157342640
102
102
  description: Makes it easy to set a role strategy on your User model in Active Record
103
103
  email: kmandrup@gmail.com
104
104
  executables: []