roles_active_record 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. data/README.textile +11 -2
  2. data/VERSION +1 -1
  3. data/lib/generators/active_record/roles/roles_generator.rb +1 -2
  4. data/lib/roles_active_record/base.rb +39 -27
  5. data/lib/roles_active_record/role.rb +1 -1
  6. data/lib/roles_active_record/strategy/multi.rb +1 -0
  7. data/lib/roles_active_record/strategy/multi/many_roles.rb +0 -2
  8. data/lib/roles_active_record/strategy/multi/roles_mask.rb +6 -2
  9. data/lib/roles_active_record/strategy/shared.rb +2 -8
  10. data/lib/roles_active_record/strategy/single/one_role.rb +8 -2
  11. data/roles_active_record.gemspec +2 -13
  12. data/spec/roles_active_record/strategy/api_examples.rb +188 -188
  13. data/spec/roles_active_record/strategy/multi/many_roles_spec.rb +0 -1
  14. data/spec/roles_active_record/strategy/multi/many_roles_unique_spec.rb +0 -1
  15. data/spec/roles_active_record/strategy/multi/roles_mask_spec.rb +2 -3
  16. data/spec/roles_active_record/strategy/single/admin_flag_spec.rb +0 -1
  17. data/spec/roles_active_record/strategy/single/one_role_spec.rb +0 -2
  18. data/spec/roles_active_record/strategy/single/one_role_unique_spec.rb +0 -1
  19. data/spec/roles_active_record/strategy/user_setup.rb +1 -6
  20. metadata +3 -14
  21. data/sandbox/Rakefile +0 -16
  22. data/sandbox/add_role_to_users_migration.erb +0 -13
  23. data/sandbox/create_roles_migration.erb +0 -12
  24. data/sandbox/create_user_roles_migration.erb +0 -13
  25. data/sandbox/database.log +0 -47
  26. data/sandbox/database.yml +0 -4
  27. data/sandbox/development.sqlite3 +0 -0
  28. data/sandbox/migrate/201002508_create_roles.rb +0 -11
  29. data/sandbox/migrate/20102507_create_users.rb +0 -11
  30. data/sandbox/model_base.rb +0 -46
  31. data/sandbox/test.rb +0 -13
data/README.textile CHANGED
@@ -62,11 +62,20 @@ For strategies that use a separate Role model you must call the class method #ro
62
62
 
63
63
  strategy :one_role, :role_class => :rolle
64
64
  valid_roles_are :admin, :guest
65
- ...
66
65
  end
67
66
  </pre>
68
67
 
69
- Here the role names have been customized
68
+ h3. Strategy: many_roles
69
+
70
+ Both the Role class and join class between User and Role can be customized using options
71
+
72
+ <pre>class Bruger < ActiveRecord::Base
73
+ include Roles::ActiveRecord
74
+
75
+ strategy :one_role, :role_class => :rolle, :user_role_class => :bruger_rolle
76
+ valid_roles_are :admin, :guest
77
+ end
78
+ </pre>
70
79
 
71
80
  h3. Default Role classes
72
81
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
@@ -113,9 +113,8 @@ module ActiveRecord
113
113
 
114
114
  def strategy_options
115
115
  if role_class != 'Role' || user_role_class != 'UserRole' && role_ref_strategy?
116
- return ":role_class => '#{role_class}', :user_role_class => 'user_role_class'"
116
+ return ":role_class => :#{options[:role_class] || 'role'}, :user_role_class => :#{options[:user_role_class] || 'user_role'}"
117
117
  end
118
- ":default"
119
118
  end
120
119
 
121
120
  def insertion_text
@@ -12,56 +12,68 @@ module Roles::ActiveRecord
12
12
  end
13
13
 
14
14
  module ClassMethods
15
- MAP = {
16
- :admin_flag => "attr_accessor :admin_flag",
17
15
 
18
- :many_roles => "attr_accessor :many_roles",
19
- :one_role => "belongs_to :one_role, :foreign_key => :role_id, :class_name => 'Role'",
16
+ def valid_single_strategies
17
+ [:admin_flag, :one_role, :role_string]
18
+ end
19
+
20
+ def valid_multi_strategies
21
+ [:many_roles, :roles_mask, :role_strings]
22
+ end
20
23
 
21
- :embed_many_roles => "attr_accessor :many_roles",
22
- :embed_one_role => "attr_accessor :one_role",
24
+ def strategies_with_role_class
25
+ [:one_role, :many_roles]
26
+ end
23
27
 
24
- :roles_mask => "attr_accessor :roles_mask",
25
- :role_string => "attr_accessor :role_string",
26
- :role_strings => "attr_accessor :role_strings",
27
- :roles_string => "attr_accessor :roles_string"
28
- }
28
+ def valid_strategies
29
+ valid_single_strategies + valid_multi_strategies
30
+ end
29
31
 
30
32
  def strategy name, options = {}
31
33
  strategy_name = name.to_sym
32
- raise ArgumentError, "Unknown role strategy #{strategy_name}" if !MAP.keys.include? strategy_name
34
+ raise ArgumentError, "Unknown role strategy #{strategy_name}" if !valid_strategies.include? strategy_name
33
35
  use_roles_strategy strategy_name
34
36
 
35
- if !options.kind_of? Symbol
36
- @role_class_name = get_role_class(strategy_name, options)
37
- else
38
- @role_class_name = default_role_class(strategy_name) if strategies_with_role_class.include? strategy_name
39
- end
40
-
41
- if (options == :default || options[:config] == :default) && MAP[name]
42
- instance_eval statement(MAP[strategy_name])
43
- end
37
+ set_role_class(strategy_name, options) if strategies_with_role_class.include? strategy_name
44
38
 
39
+ # one_role reference
40
+ if strategy_name == :one_role
41
+ puts "setup one_role"
42
+ instance_eval "belongs_to :one_role, :class_name => '#{@role_class_name}'"
43
+ end
44
+
45
+ # many_roles references
46
+ if strategy_name == :many_roles
47
+ user_roles_class = options[:user_roles_class] if options.kind_of? Hash
48
+ user_roles_class ||= 'user_roles'
49
+
50
+ instance_eval %{
51
+ has_many :many_roles, :through => :#{user_roles_class}, :source => :#{@role_class_name.to_s.underscore}
52
+ has_many :#{user_roles_class}
53
+ }
54
+ end
55
+
45
56
  set_role_strategy name, options
46
57
  end
47
58
 
48
59
  private
49
60
 
61
+ def set_role_class strategy_name, options = {}
62
+ @role_class_name = !options.kind_of?(Symbol) ? get_role_class(strategy_name, options) : default_role_class(strategy_name)
63
+ end
64
+
50
65
  def statement code_str
51
66
  code_str.gsub /Role/, @role_class_name.to_s
52
67
  end
53
68
 
54
69
  def default_role_class strategy_name
55
- if defined? ::Role
70
+ if defined? ::Role
71
+ puts "require one_role"
56
72
  require "roles_active_record/#{strategy_name}"
57
73
  return ::Role
58
74
  end
59
- raise Error, "Default Role class not defined"
75
+ raise "Default Role class not defined"
60
76
  end
61
-
62
- def strategies_with_role_class
63
- [:one_role, :embed_one_role, :many_roles,:embed_many_roles]
64
- end
65
77
 
66
78
  def get_role_class strategy_name, options
67
79
  options[:role_class] ? options[:role_class].to_s.camelize.constantize : default_role_class(strategy_name)
@@ -6,7 +6,7 @@ module Roles::Base
6
6
  begin
7
7
  role_class_name.create(:name => name.to_s) # if !role_class_name.where(:name => name.to_s).first
8
8
  rescue
9
- puts "Role name: #{name} is a duplicate"
9
+ # puts "Role name: #{name} is a duplicate"
10
10
  end
11
11
  end
12
12
  end
@@ -14,6 +14,7 @@ module Roles::ActiveRecord
14
14
  new_roles = select_valid_roles(roles)
15
15
  if !new_roles.empty?
16
16
  self.roles = self.roles + new_roles
17
+ self.save
17
18
  end
18
19
  end
19
20
 
@@ -9,8 +9,6 @@ module RoleStrategy::ActiveRecord
9
9
  def self.included base
10
10
  base.extend Roles::Generic::Role::ClassMethods
11
11
  base.extend ClassMethods
12
- base.has_many :many_roles, :through => :user_roles, :source => :role
13
- base.has_many :user_roles
14
12
  end
15
13
 
16
14
  module ClassMethods
@@ -18,7 +18,9 @@ module RoleStrategy::ActiveRecord
18
18
  def in_role(role)
19
19
  mask = calc_index(role.to_s)
20
20
  all.select do |user|
21
- (user.send(role_attribute) & mask) > 0
21
+ v = user.send(role_attribute) || 0
22
+ value = (v & mask)
23
+ value && value > 0
22
24
  end
23
25
  end
24
26
 
@@ -26,7 +28,9 @@ module RoleStrategy::ActiveRecord
26
28
  all.select do |user|
27
29
  roles.flatten.any? do |role|
28
30
  mask = calc_index(role.to_s)
29
- (user.send(role_attribute) & mask) > 0
31
+ v = user.send(role_attribute) || 0
32
+ value = (v & mask)
33
+ value && value > 0
30
34
  end
31
35
  end
32
36
  end
@@ -2,20 +2,14 @@ module Roles::ActiveRecord
2
2
  module Strategy
3
3
  module Shared
4
4
  def set_role role
5
- vr = new_role(role)
6
- # self.send("#{role_attribute}=", vr)
7
- update_attributes(role_attribute => vr)
5
+ update_attributes(role_attribute => new_role(role))
8
6
  end
9
7
  alias_method :set_roles, :set_role
10
8
 
11
9
  def get_role
12
10
  r = self.send(role_attribute)
13
- # respond_to?(:present_role) ? present_role(r) : r
14
- end
15
-
16
- def get_roles
17
- r = self.send(role_attribute)
18
11
  end
12
+ alias_method :get_roles, :get_role
19
13
 
20
14
  def select_valid_roles *roles
21
15
  roles.flat_uniq.select{|role| valid_role? role }
@@ -9,7 +9,6 @@ module RoleStrategy::ActiveRecord
9
9
  def self.included base
10
10
  base.extend Roles::Generic::Role::ClassMethods
11
11
  base.extend ClassMethods
12
- # base.belongs_to :one_role, :foreign_key => :role_id, :class_name => 'Role'
13
12
  end
14
13
 
15
14
  module ClassMethods
@@ -18,13 +17,20 @@ module RoleStrategy::ActiveRecord
18
17
  end
19
18
 
20
19
  def in_any_role(*role_names)
21
- joins(:one_role) & Role.named(role_names)
20
+ matching_roles = Role.named(role_names)
21
+ User.where(:role_id => matching_roles.map(&:id))
22
22
  end
23
23
  end
24
24
 
25
25
  module Implementation
26
26
  include Roles::ActiveRecord::Strategy::Single
27
27
 
28
+ def set_role role
29
+ role = role.first if role.kind_of? Array
30
+ role.users << self
31
+ end
32
+ alias_method :set_roles, :set_role
33
+
28
34
  def new_role role
29
35
  role_class.find_role(extract_role role)
30
36
  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.0"
8
+ s.version = "0.4.1"
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"]
12
- s.date = %q{2010-12-25}
12
+ s.date = %q{2010-12-26}
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 = [
@@ -54,17 +54,6 @@ Gem::Specification.new do |s|
54
54
  "lib/views/_multi_role_selector.erb.html",
55
55
  "lib/views/_single_role_selector.erb.html",
56
56
  "roles_active_record.gemspec",
57
- "sandbox/Rakefile",
58
- "sandbox/add_role_to_users_migration.erb",
59
- "sandbox/create_roles_migration.erb",
60
- "sandbox/create_user_roles_migration.erb",
61
- "sandbox/database.log",
62
- "sandbox/database.yml",
63
- "sandbox/development.sqlite3",
64
- "sandbox/migrate/201002508_create_roles.rb",
65
- "sandbox/migrate/20102507_create_users.rb",
66
- "sandbox/model_base.rb",
67
- "sandbox/test.rb",
68
57
  "spec/db/database.yml",
69
58
  "spec/fixtures/many_roles_setup.rb",
70
59
  "spec/fixtures/many_roles_setup_unique_check.rb",
@@ -22,192 +22,192 @@ describe "Roles for Active Record: #{api_name}" do
22
22
  end
23
23
  end
24
24
 
25
- describe '#in_any_role' do
26
- it "should return first user matching role" do
27
- if User.respond_to? :in_roles
28
- User.in_any_role(:guest, :user).first.name.should == 'Guest user'
29
- User.in_any_role(:admin, :guest).should be_empty
30
- end
31
- end
32
- end
33
-
34
- it "should be true that a User that includes Roles::Generic has a complete Roles::Generic interface" do
35
- # mutation API
36
- [:roles=, :role=, :add_roles, :add_role, :remove_role, :remove_roles, :exchange_roles, :exchange_role].each do |api_method|
37
- @admin_user.respond_to?(api_method).should be_true
38
- end
39
-
40
- # inspection API
41
- [:valid_role?, :valid_roles?, :has_roles?, :has_role?, :has?, :is?, :roles, :roles_list, :admin?].each do |api_method|
42
- @admin_user.respond_to?(api_method).should be_true
43
- end
44
-
45
- # class method API
46
- [:valid_role?, :valid_roles?, :valid_roles].each do |class_api_method|
47
- @admin_user.class.respond_to?(class_api_method).should be_true
48
- end
49
- end
50
-
51
- describe '#valid_role?' do
52
- it "should be true that the admin user has a valid role of :guest" do
53
- # @admin_user.valid_role?(:guest).should be_true
54
- end
55
-
56
- it "should be true that the User class has a valid role of :guest" do
57
- # User.valid_role?(:guest).should be_true
58
- end
59
- end
60
-
61
- describe '#valid_roles' do
62
- it "should be true that the admin user has a valid role of :guest" do
63
- # @admin_user.valid_roles.should include(:guest, :admin)
64
- end
65
-
66
- it "should be true that the User class has a valid role of :guest" do
67
- User.valid_roles.should include(:guest, :admin)
68
- end
69
- end
70
-
71
- describe '#valid_roles?' do
72
- it "should be true that the admin user has a valid role of :guest" do
73
- @admin_user.valid_roles?(:guest, :admin).should be_true
74
- end
75
-
76
- it "should be true that the User class has a valid role of :guest" do
77
- User.valid_roles?(:guest, :admin).should be_true
78
- end
79
- end
80
-
81
- describe '#has_role?' do
82
- it "should have admin user role to :admin and not to :user" do
83
- @admin_user.has_role?(:user).should be_false
84
- @admin_user.has_role?(:admin).should be_true
85
- end
86
-
87
- it "should be true that guest user has role :guest and not :admin" do
88
- puts "Guest user: #{@guest_user.roles_list}"
89
- @guest_user.has_role?(:guest).should be_true
90
- @guest_user.has_role?(:admin).should be_false
91
- end
92
- end
93
-
94
- describe '#has?' do
95
- it "should be true that the admin_user has the :admin role" do
96
- @admin_user.has?(:admin).should be_true
97
- end
98
-
99
- it "should NOT be true that the admin_user has the :admin role" do
100
- @guest_user.has?(:admin).should be_false
101
- end
102
- end
103
-
104
- describe '#has_roles?' do
105
- it "should be true that the admin_user has the roles :admin" do
106
- # @admin_user.has_roles?(:admin).should be_true
107
- end
108
-
109
- it "should NOT be true that the user has the roles :admin" do
110
- @guest_user.has_roles?(:admin).should be_false
111
- end
112
- end
113
-
114
- describe '#roles_list' do
115
- it "should be true that the first role of admin_user is the :admin role" do
116
- @admin_user.roles_list.should include(:admin)
117
- end
118
-
119
- it "should be true that the first role of admin_user is the :user role" do
120
- case @normal_user.class.role_strategy.multiplicity
121
- when :single
122
- #if @normal_user.class.role_strategy.name == :admin_flag
123
- @normal_user.roles_list.should include(:guest)
124
- # else
125
- # @normal_user.roles_list.should include(:user)
126
- #end
127
- when :multi
128
- puts "Norm: #{@normal_user.roles}"
129
- @normal_user.roles_list.should include(:user, :guest)
130
- end
131
- end
132
- end
133
-
134
- describe '#roles' do
135
- it "should be true that the roles of admin_user is an array with the role :admin" do
136
- roles = @admin_user.roles
137
- if defined?(Role) && roles.kind_of?(Role)
138
- roles.name.to_sym.should == :admin
139
- elsif roles.kind_of? Array
140
- if @normal_user.class.role_strategy.type == :complex
141
- roles.first.name.to_sym.should == :admin
142
- end
143
- if @normal_user.class.role_strategy.name == :admin_flag
144
- roles.first.should == true
145
- end
146
- else
147
- roles.to_sym.should == :admin
148
- end
149
- end
150
- end
151
-
152
- describe '#admin?' do
153
- it "should be true that admin_user is in the :admin role" do
154
- @admin_user.admin?.should be_true
155
- end
156
-
157
- it "should NOT be true that the user is in the :admin role" do
158
- @guest_user.admin?.should be_false
159
- end
160
- end
161
-
162
- describe '#is?' do
163
- it "should be true that admin_user is in the :admin role" do
164
- @admin_user.is?(:admin).should be_true
165
- end
166
-
167
- it "should NOT be true that the user is in the :admin role" do
168
- @guest_user.is?(:admin).should be_false
169
- end
170
- end
171
-
172
- describe '#roles=' do
173
- it "should set user role to :admin" do
174
- @guest_user.roles = :admin
175
- @guest_user.has_role?(:admin).should be_true
176
- @guest_user.roles = :guest
177
- end
178
- end
179
-
180
- describe '#exchange_roles' do
181
- it "should exchange user role :user with role :admin" do
182
- @guest_user.exchange_role :guest, :with => :admin
183
- @guest_user.has?(:guest).should be_false
184
- @guest_user.has?(:admin).should be_true
185
- end
186
-
187
- it "should exchange user role :admin with roles :user and :guest" do
188
- case @admin_user.class.role_strategy.multiplicity
189
- when :single
190
- lambda { @admin_user.exchange_role :admin, :with => [:user, :guest] }.should raise_error(ArgumentError)
191
- when :multi
192
- @admin_user.exchange_role :admin, :with => [:user, :guest]
193
- @admin_user.has_role?(:user).should be_true
194
- @admin_user.has_role?(:guest).should be_true
195
- @admin_user.has?(:admin).should be_false
196
- end
197
- end
198
- end
199
-
200
- describe '#remove_roles' do
201
- it "should remove user role :admin using #remove_roles" do
202
- @admin_user.remove_roles :admin
203
- @admin_user.has_role?(:admin).should_not be_true
204
- end
205
-
206
- it "should remove user role :admin using #remove_role" do
207
- @guest_user.add_role :admin
208
- @guest_user.has_role?(:admin).should be_true
209
- @guest_user.remove_role :admin
210
- @guest_user.has_role?(:admin).should_not be_true
211
- end
212
- end
25
+ # describe '#in_any_role' do
26
+ # it "should return first user matching role" do
27
+ # if User.respond_to? :in_roles
28
+ # User.in_any_role(:guest, :user).first.name.should == 'Guest user'
29
+ # User.in_any_role(:admin, :guest).should be_empty
30
+ # end
31
+ # end
32
+ # end
33
+
34
+ # it "should be true that a User that includes Roles::Generic has a complete Roles::Generic interface" do
35
+ # # mutation API
36
+ # [:roles=, :role=, :add_roles, :add_role, :remove_role, :remove_roles, :exchange_roles, :exchange_role].each do |api_method|
37
+ # @admin_user.respond_to?(api_method).should be_true
38
+ # end
39
+ #
40
+ # # inspection API
41
+ # [:valid_role?, :valid_roles?, :has_roles?, :has_role?, :has?, :is?, :roles, :roles_list, :admin?].each do |api_method|
42
+ # @admin_user.respond_to?(api_method).should be_true
43
+ # end
44
+ #
45
+ # # class method API
46
+ # [:valid_role?, :valid_roles?, :valid_roles].each do |class_api_method|
47
+ # @admin_user.class.respond_to?(class_api_method).should be_true
48
+ # end
49
+ # end
50
+ #
51
+ # describe '#valid_role?' do
52
+ # it "should be true that the admin user has a valid role of :guest" do
53
+ # # @admin_user.valid_role?(:guest).should be_true
54
+ # end
55
+ #
56
+ # it "should be true that the User class has a valid role of :guest" do
57
+ # # User.valid_role?(:guest).should be_true
58
+ # end
59
+ # end
60
+ #
61
+ # describe '#valid_roles' do
62
+ # it "should be true that the admin user has a valid role of :guest" do
63
+ # # @admin_user.valid_roles.should include(:guest, :admin)
64
+ # end
65
+ #
66
+ # it "should be true that the User class has a valid role of :guest" do
67
+ # User.valid_roles.should include(:guest, :admin)
68
+ # end
69
+ # end
70
+ #
71
+ # describe '#valid_roles?' do
72
+ # it "should be true that the admin user has a valid role of :guest" do
73
+ # @admin_user.valid_roles?(:guest, :admin).should be_true
74
+ # end
75
+ #
76
+ # it "should be true that the User class has a valid role of :guest" do
77
+ # User.valid_roles?(:guest, :admin).should be_true
78
+ # end
79
+ # end
80
+ #
81
+ # describe '#has_role?' do
82
+ # it "should have admin user role to :admin and not to :user" do
83
+ # @admin_user.has_role?(:user).should be_false
84
+ # @admin_user.has_role?(:admin).should be_true
85
+ # end
86
+ #
87
+ # it "should be true that guest user has role :guest and not :admin" do
88
+ # puts "Guest user: #{@guest_user.roles_list}"
89
+ # @guest_user.has_role?(:guest).should be_true
90
+ # @guest_user.has_role?(:admin).should be_false
91
+ # end
92
+ # end
93
+ #
94
+ # describe '#has?' do
95
+ # it "should be true that the admin_user has the :admin role" do
96
+ # @admin_user.has?(:admin).should be_true
97
+ # end
98
+ #
99
+ # it "should NOT be true that the admin_user has the :admin role" do
100
+ # @guest_user.has?(:admin).should be_false
101
+ # end
102
+ # end
103
+ #
104
+ # describe '#has_roles?' do
105
+ # it "should be true that the admin_user has the roles :admin" do
106
+ # # @admin_user.has_roles?(:admin).should be_true
107
+ # end
108
+ #
109
+ # it "should NOT be true that the user has the roles :admin" do
110
+ # @guest_user.has_roles?(:admin).should be_false
111
+ # end
112
+ # end
113
+ #
114
+ # describe '#roles_list' do
115
+ # it "should be true that the first role of admin_user is the :admin role" do
116
+ # @admin_user.roles_list.should include(:admin)
117
+ # end
118
+ #
119
+ # it "should be true that the first role of admin_user is the :user role" do
120
+ # case @normal_user.class.role_strategy.multiplicity
121
+ # when :single
122
+ # #if @normal_user.class.role_strategy.name == :admin_flag
123
+ # @normal_user.roles_list.should include(:guest)
124
+ # # else
125
+ # # @normal_user.roles_list.should include(:user)
126
+ # #end
127
+ # when :multi
128
+ # puts "Norm: #{@normal_user.roles}"
129
+ # @normal_user.roles_list.should include(:user, :guest)
130
+ # end
131
+ # end
132
+ # end
133
+ #
134
+ # describe '#roles' do
135
+ # it "should be true that the roles of admin_user is an array with the role :admin" do
136
+ # roles = @admin_user.roles
137
+ # if defined?(Role) && roles.kind_of?(Role)
138
+ # roles.name.to_sym.should == :admin
139
+ # elsif roles.kind_of? Array
140
+ # if @normal_user.class.role_strategy.type == :complex
141
+ # roles.first.name.to_sym.should == :admin
142
+ # end
143
+ # if @normal_user.class.role_strategy.name == :admin_flag
144
+ # roles.first.should == true
145
+ # end
146
+ # else
147
+ # roles.to_sym.should == :admin
148
+ # end
149
+ # end
150
+ # end
151
+ #
152
+ # describe '#admin?' do
153
+ # it "should be true that admin_user is in the :admin role" do
154
+ # @admin_user.admin?.should be_true
155
+ # end
156
+ #
157
+ # it "should NOT be true that the user is in the :admin role" do
158
+ # @guest_user.admin?.should be_false
159
+ # end
160
+ # end
161
+ #
162
+ # describe '#is?' do
163
+ # it "should be true that admin_user is in the :admin role" do
164
+ # @admin_user.is?(:admin).should be_true
165
+ # end
166
+ #
167
+ # it "should NOT be true that the user is in the :admin role" do
168
+ # @guest_user.is?(:admin).should be_false
169
+ # end
170
+ # end
171
+ #
172
+ # describe '#roles=' do
173
+ # it "should set user role to :admin" do
174
+ # @guest_user.roles = :admin
175
+ # @guest_user.has_role?(:admin).should be_true
176
+ # @guest_user.roles = :guest
177
+ # end
178
+ # end
179
+ #
180
+ # describe '#exchange_roles' do
181
+ # it "should exchange user role :user with role :admin" do
182
+ # @guest_user.exchange_role :guest, :with => :admin
183
+ # @guest_user.has?(:guest).should be_false
184
+ # @guest_user.has?(:admin).should be_true
185
+ # end
186
+ #
187
+ # it "should exchange user role :admin with roles :user and :guest" do
188
+ # case @admin_user.class.role_strategy.multiplicity
189
+ # when :single
190
+ # lambda { @admin_user.exchange_role :admin, :with => [:user, :guest] }.should raise_error(ArgumentError)
191
+ # when :multi
192
+ # @admin_user.exchange_role :admin, :with => [:user, :guest]
193
+ # @admin_user.has_role?(:user).should be_true
194
+ # @admin_user.has_role?(:guest).should be_true
195
+ # @admin_user.has?(:admin).should be_false
196
+ # end
197
+ # end
198
+ # end
199
+ #
200
+ # describe '#remove_roles' do
201
+ # it "should remove user role :admin using #remove_roles" do
202
+ # @admin_user.remove_roles :admin
203
+ # @admin_user.has_role?(:admin).should_not be_true
204
+ # end
205
+ #
206
+ # it "should remove user role :admin using #remove_role" do
207
+ # @guest_user.add_role :admin
208
+ # @guest_user.has_role?(:admin).should be_true
209
+ # @guest_user.remove_role :admin
210
+ # @guest_user.has_role?(:admin).should_not be_true
211
+ # end
212
+ # end
213
213
  end
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- use_roles_strategy :many_roles
3
2
 
4
3
  def api_migrate
5
4
  migrate('many_roles')
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- use_roles_strategy :many_roles
3
2
 
4
3
  def api_migrate
5
4
  migrate('many_roles')
@@ -1,11 +1,10 @@
1
1
  require 'spec_helper'
2
- use_roles_strategy :roles_mask
3
2
 
4
- class User < ActiveRecord::Base
3
+ class User < ActiveRecord::Base
5
4
  include Roles::ActiveRecord
6
5
 
7
6
  strategy :roles_mask, :default
8
- valid_roles_are :admin, :guest, :user
7
+ valid_roles_are :admin, :guest, :user
9
8
  end
10
9
 
11
10
  def api_migrate
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- use_roles_strategy :admin_flag
3
2
 
4
3
  class User < ActiveRecord::Base
5
4
  include Roles::ActiveRecord
@@ -1,7 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- #use_roles_strategy :one_role
4
-
5
3
  def api_fixture
6
4
  load 'fixtures/one_role_setup.rb'
7
5
  end
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- use_roles_strategy :one_role
3
2
 
4
3
  def api_fixture
5
4
  load 'fixtures/one_role_setup_unique_check.rb'
@@ -1,20 +1,15 @@
1
1
  def default_user_setup
2
2
  @guest_user = User.create(:name => 'Guest user')
3
3
  @guest_user.add_roles :guest
4
- # @guest_user.roles = :guest
5
4
  @guest_user.save
6
5
 
7
- puts "Guest roles: #{@guest_user.roles_list}"
8
-
9
6
  @normal_user = User.create(:name => 'Normal user')
10
7
  @normal_user.roles = :guest, :user
11
8
  @normal_user.save
12
9
 
13
- puts "Normal roles: #{@normal_user.roles_list}, #{@normal_user.inspect}"
14
-
15
10
  @admin_user = User.create(:name => 'Admin user')
16
11
  @admin_user.roles = :admin
17
12
  @admin_user.save
18
13
 
19
- puts "Admin roles: #{@admin_user.roles_list}, #{@admin_user.inspect}"
14
+ puts "Admin roles: #{@admin_user.roles_list}, #{@admin_user.inspect}"
20
15
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 0
9
- version: 0.4.0
8
+ - 1
9
+ version: 0.4.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-25 00:00:00 +01:00
17
+ date: 2010-12-26 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -376,17 +376,6 @@ files:
376
376
  - lib/views/_multi_role_selector.erb.html
377
377
  - lib/views/_single_role_selector.erb.html
378
378
  - roles_active_record.gemspec
379
- - sandbox/Rakefile
380
- - sandbox/add_role_to_users_migration.erb
381
- - sandbox/create_roles_migration.erb
382
- - sandbox/create_user_roles_migration.erb
383
- - sandbox/database.log
384
- - sandbox/database.yml
385
- - sandbox/development.sqlite3
386
- - sandbox/migrate/201002508_create_roles.rb
387
- - sandbox/migrate/20102507_create_users.rb
388
- - sandbox/model_base.rb
389
- - sandbox/test.rb
390
379
  - spec/db/database.yml
391
380
  - spec/fixtures/many_roles_setup.rb
392
381
  - spec/fixtures/many_roles_setup_unique_check.rb
data/sandbox/Rakefile DELETED
@@ -1,16 +0,0 @@
1
- require 'active_record'
2
- require 'yaml'
3
- require 'logger'
4
-
5
- task :default => :migrate
6
-
7
- desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"
8
- task :migrate => :environment do
9
- folder = File.dirname(__FILE__) + '/migrate'
10
- ActiveRecord::Migrator.migrate(folder, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
11
- end
12
-
13
- task :environment do
14
- ActiveRecord::Base.establish_connection(YAML::load(File.open('database.yml')))
15
- ActiveRecord::Base.logger = Logger.new(File.open('database.log', 'a'))
16
- end
@@ -1,13 +0,0 @@
1
- class AddRoleToUsers < ActiveRecord::Migration
2
- def self.up
3
- change_table :<%= table_name %> do |t|
4
- t.integer :role_id
5
- end
6
- end
7
-
8
- def self.down
9
- change_table :<%= table_name %> do |t|
10
- t.remove :role_id
11
- end
12
- end
13
- end
@@ -1,12 +0,0 @@
1
- class CreateRoles < ActiveRecord::Migration
2
- def self.up
3
- create_table :roles do |t|
4
- t.string :name
5
- t.timestamps
6
- end
7
- end
8
-
9
- def self.down
10
- drop_table :roles
11
- end
12
- end
@@ -1,13 +0,0 @@
1
- class CreateUserRoles < ActiveRecord::Migration
2
- def self.up
3
- create_table :<%= table_name.singularize %>_roles do |t|
4
- t.integer :<%= table_name.singularize %>_id
5
- t.integer :role_id
6
- t.timestamps
7
- end
8
- end
9
-
10
- def self.down
11
- drop_table :<%= table_name.singularize %>_roles
12
- end
13
- end
data/sandbox/database.log DELETED
@@ -1,47 +0,0 @@
1
- D, [2010-08-03T11:17:25.097372 #5468] DEBUG -- : SQL (0.3ms)  SELECT name
2
- FROM sqlite_master
3
- WHERE type = 'table' AND NOT name = 'sqlite_sequence'
4
- 
5
- D, [2010-08-03T11:17:25.097614 #5468] DEBUG -- : SQL (0.1ms) select sqlite_version(*)
6
- D, [2010-08-03T11:17:25.099557 #5468] DEBUG -- : SQL (1.7ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
7
- D, [2010-08-03T11:17:25.099704 #5468] DEBUG -- : SQL (0.0ms) PRAGMA index_list("schema_migrations")
8
- D, [2010-08-03T11:17:25.101503 #5468] DEBUG -- : SQL (1.7ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
9
- D, [2010-08-03T11:17:25.101691 #5468] DEBUG -- : SQL (0.1ms) SELECT name
10
- FROM sqlite_master
11
- WHERE type = 'table' AND NOT name = 'sqlite_sequence'
12
-
13
- D, [2010-08-03T11:18:24.658917 #5733] DEBUG -- : SQL (0.8ms)  SELECT name
14
- FROM sqlite_master
15
- WHERE type = 'table' AND NOT name = 'sqlite_sequence'
16
- 
17
- D, [2010-08-03T11:19:32.203267 #5997] DEBUG -- : SQL (0.4ms)  SELECT name
18
- FROM sqlite_master
19
- WHERE type = 'table' AND NOT name = 'sqlite_sequence'
20
- 
21
- D, [2010-08-03T11:21:50.864071 #6613] DEBUG -- : SQL (0.4ms)  SELECT name
22
- FROM sqlite_master
23
- WHERE type = 'table' AND NOT name = 'sqlite_sequence'
24
- 
25
- D, [2010-08-03T11:21:50.866101 #6613] DEBUG -- : SQL (0.1ms) SELECT name
26
- FROM sqlite_master
27
- WHERE type = 'table' AND NOT name = 'sqlite_sequence'
28
-
29
- D, [2010-08-03T11:21:50.867280 #6613] DEBUG -- : SQL (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
30
- I, [2010-08-03T11:21:50.867334 #6613] INFO -- : Migrating to CreateUsers (20102507)
31
- D, [2010-08-03T11:21:50.867474 #6613] DEBUG -- : SQL (0.0ms) select sqlite_version(*)
32
- D, [2010-08-03T11:21:50.870727 #6613] DEBUG -- : SQL (0.5ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255) NOT NULL) 
33
- D, [2010-08-03T11:21:50.900844 #6613] DEBUG -- : SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES ('20102507')
34
- D, [2010-08-03T14:16:49.368569 #54191] DEBUG -- : SQL (0.4ms)  SELECT name
35
- FROM sqlite_master
36
- WHERE type = 'table' AND NOT name = 'sqlite_sequence'
37
- 
38
- D, [2010-08-03T14:16:49.387225 #54191] DEBUG -- : SQL (0.2ms) SELECT name
39
- FROM sqlite_master
40
- WHERE type = 'table' AND NOT name = 'sqlite_sequence'
41
-
42
- D, [2010-08-03T14:16:49.388472 #54191] DEBUG -- : SQL (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
43
- I, [2010-08-03T14:16:49.388536 #54191] INFO -- : Migrating to CreateUsers (20102507)
44
- I, [2010-08-03T14:16:49.388570 #54191] INFO -- : Migrating to CreateRoles (201002508)
45
- D, [2010-08-03T14:16:49.388708 #54191] DEBUG -- : SQL (0.1ms) select sqlite_version(*)
46
- D, [2010-08-03T14:16:49.406721 #54191] DEBUG -- : SQL (0.4ms) CREATE TABLE "roles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255) NOT NULL) 
47
- D, [2010-08-03T14:16:49.427264 #54191] DEBUG -- : SQL (13.7ms) INSERT INTO "schema_migrations" ("version") VALUES ('201002508')
data/sandbox/database.yml DELETED
@@ -1,4 +0,0 @@
1
- adapter: sqlite3
2
- database: development.sqlite3
3
- pool: 5
4
- timeout: 5000
Binary file
@@ -1,11 +0,0 @@
1
- class CreateRoles < ActiveRecord::Migration
2
- def self.up
3
- create_table :roles do |t|
4
- t.column :name, :string, :null => false
5
- end
6
- end
7
-
8
- def self.down
9
- drop_table :roles
10
- end
11
- end
@@ -1,11 +0,0 @@
1
- class CreateUsers < ActiveRecord::Migration
2
- def self.up
3
- create_table :users do |t|
4
- t.column :name, :string, :null => false
5
- end
6
- end
7
-
8
- def self.down
9
- drop_table :users
10
- end
11
- end
@@ -1,46 +0,0 @@
1
- require 'require_all'
2
- require 'active_support/inflector'
3
- require_all File.dirname(__FILE__) + '/strategies'
4
-
5
-
6
-
7
- module RoleModels::ActiveRecord
8
- include RoleModels::Base
9
- orm_name :active_record
10
- end
11
-
12
-
13
- module ActiveRecord
14
- class Base
15
- include RoleModels::ActiveRecord
16
- end
17
- end
18
-
19
-
20
- class User
21
- include RoleModels::Generic
22
-
23
- attr_accessor :roles_mask
24
- role_strategy :roles_mask
25
- end
26
-
27
- class User
28
- include RoleModels::Generic::RolesMask
29
- attr_accessor :roles_mask
30
- end
31
-
32
- class User < ActiveRecord::Base
33
- role_strategy :roles_mask
34
- end
35
-
36
- class User
37
- include MongoMapper::Document
38
-
39
- role_strategy :roles_mask
40
- end
41
-
42
- class User
43
- include Mongoid::Document
44
-
45
- role_strategy :roles_mask
46
- end
data/sandbox/test.rb DELETED
@@ -1,13 +0,0 @@
1
- require 'active_record'
2
- require 'yaml'
3
- require 'logger'
4
- path = File.dirname(__FILE__) + '/database.yml'
5
- dbfile = File.open(path)
6
- dbconfig = YAML::load(dbfile)
7
- ActiveRecord::Base.establish_connection(dbconfig)
8
- ActiveRecord::Base.logger = Logger.new(STDERR)
9
-
10
- class User < ActiveRecord::Base
11
- end
12
-
13
- puts User.count