roles_mongoid 0.4.0 → 0.4.1

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.0
1
+ 0.4.1
@@ -13,6 +13,7 @@ module Mongoid
13
13
 
14
14
  class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
15
15
  class_option :roles, :type => :array, :aliases => "-r", :default => [], :desc => "Valid roles"
16
+ class_option :role_class, :type => :string, :aliases => "-rc", :default => 'Role', :desc => "Role class"
16
17
 
17
18
  def apply_role_strategy
18
19
  logger.add_logfile :logfile => logfile if logfile
@@ -31,13 +32,11 @@ module Mongoid
31
32
  insert_into_model user_model_name, :after => /include Mongoid::\w+/ do
32
33
  insertion_text
33
34
  end
34
-
35
- unless read_model(:user) =~ /use_roles_strategy/
36
- inject_into_file model_file(:user), "use_roles_strategy :#{strategy}\n\n", :before => "class"
37
- end
38
35
  rescue Exception => e
39
36
  logger.debug"Error: #{e.message}"
40
37
  end
38
+
39
+ copy_role_class if role_class_strategy?
41
40
  end
42
41
 
43
42
  protected
@@ -71,6 +70,11 @@ module Mongoid
71
70
  [:admin, :guest]
72
71
  end
73
72
 
73
+ def copy_role_class
74
+ logger.debug "copy_role_class: #{role_class.underscore}"
75
+ template 'role.rb', "app/models/#{role_class.underscore}.rb"
76
+ end
77
+
74
78
  def roles_to_add
75
79
  @roles_to_add ||= default_roles.concat(options[:roles]).to_symbols.uniq
76
80
  end
@@ -80,23 +84,19 @@ module Mongoid
80
84
  end
81
85
 
82
86
  def role_strategy_statement
83
- "strategy :#{strategy}, #{strategy_option_arg}"
87
+ "strategy :#{strategy} #{strategy_option_arg}"
84
88
  end
85
89
 
86
90
  def strategy_option_arg
87
- case strategy
88
- when 'embed_one_role', 'embed_many_roles'
89
- ":role_class => :role, :config => :default"
90
- else
91
- ":default\n#{role_class_stmt}"
92
- end
91
+ return ", :role_class => :#{role_class.to_s.underscore}" if role_class_strategy? && role_class.to_s != 'Role'
92
+ ''
93
93
  end
94
94
 
95
- def role_class_stmt
96
- " role_class :role" if [:one_role, :many_roles].include? (strategy.to_sym)
95
+ def role_class_strategy?
96
+ [:one_role, :many_roles, :embed_one_role, :embed_many_roles].include? strategy.to_sym
97
97
  end
98
98
 
99
- def roles_statement
99
+ def valid_roles_statement
100
100
  return '' if has_valid_roles_statement?
101
101
  roles ? "valid_roles_are #{roles.join(', ')}" : ''
102
102
  end
@@ -108,7 +108,11 @@ module Mongoid
108
108
  def insertion_text
109
109
  %Q{include Roles::#{orm.to_s.camelize}
110
110
  #{role_strategy_statement}
111
- #{roles_statement}}
111
+ #{valid_roles_statement}}
112
+ end
113
+
114
+ def role_class
115
+ options[:role_class].classify || 'Role'
112
116
  end
113
117
 
114
118
  def strategy
@@ -0,0 +1,8 @@
1
+ class <%= role_class %>
2
+ include Mongoid::Document
3
+ field :name, :type => String
4
+
5
+ validates_uniqueness_of :name
6
+
7
+ extend RoleClass::ClassMethods
8
+ end
@@ -34,13 +34,7 @@ module Roles
34
34
  raise ArgumentError, "Unknown role strategy #{strategy_name}" if !MAP.keys.include? strategy_name
35
35
  use_roles_strategy strategy_name
36
36
 
37
- if strategies_with_role_class.include? strategy_name
38
- if !options.kind_of? Symbol
39
- @role_class_name = get_role_class(strategy_name, options)
40
- else
41
- @role_class_name = default_role_class(strategy_name)
42
- end
43
- end
37
+ set_role_class(strategy_name, options) if strategies_with_role_class.include? strategy_name
44
38
 
45
39
  if default_options?(options) && MAP[strategy_name]
46
40
  instance_eval statement(MAP[strategy_name])
@@ -60,6 +54,10 @@ module Roles
60
54
 
61
55
  private
62
56
 
57
+ def set_role_class strategy_name, options = {}
58
+ @role_class_name = !options.kind_of?(Symbol) ? get_role_class(strategy_name, options) : default_role_class(strategy_name)
59
+ end
60
+
63
61
  def default_options? options = {}
64
62
  return true if options == :default
65
63
  if options.kind_of? Hash
@@ -10,4 +10,18 @@ module Roles::Base
10
10
  end
11
11
  end
12
12
  end
13
+ end
14
+
15
+ module RoleClass
16
+ module ClassMethods
17
+ def find_roles(*role_names)
18
+ where(:name.in => role_names.flatten).to_a
19
+ end
20
+
21
+ def find_role role_name
22
+ raise ArgumentError, "#find_role takes a single role name as argument, not: #{role_name.inspect}" if !role_name.kind_of_label?
23
+ res = find_roles(role_name)
24
+ res ? res.first : res
25
+ end
26
+ end
13
27
  end
@@ -6,15 +6,5 @@ class Role
6
6
 
7
7
  validates_uniqueness_of :name
8
8
 
9
- class << self
10
- def find_roles(*role_names)
11
- where(:name.in => role_names.flatten).to_a
12
- end
13
-
14
- def find_role role_name
15
- raise ArgumentError, "#find_role takes a single role name as argument, not: #{role_name.inspect}" if !role_name.kind_of_label?
16
- res = find_roles(role_name)
17
- res ? res.first : res
18
- end
19
- end
9
+ extend RoleClass::ClassMethods
20
10
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{roles_mongoid}
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-26}
12
+ s.date = %q{2011-01-01}
13
13
  s.description = %q{Makes it easy to set a role strategy on your User model in Mongoid}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
26
26
  "Rakefile",
27
27
  "VERSION",
28
28
  "lib/generators/mongoid/roles/roles_generator.rb",
29
+ "lib/generators/mongoid/roles/templates/role.rb",
29
30
  "lib/roles_mongoid.rb",
30
31
  "lib/roles_mongoid/base.rb",
31
32
  "lib/roles_mongoid/base_role.rb",
@@ -23,178 +23,178 @@ describe "Roles for Mongoid: #{api_name}" do
23
23
  end
24
24
  end
25
25
 
26
- # it "should be true that a User that includes Roles::Generic has a complete Roles::Generic interface" do
27
- # # mutation API
28
- # [:roles=, :role=, :add_roles, :add_role, :remove_role, :remove_roles, :exchange_roles, :exchange_role].each do |api_method|
29
- # @admin_user.respond_to?(api_method).should be_true
30
- # end
31
- #
32
- # # inspection API
33
- # [:valid_role?, :valid_roles?, :has_roles?, :has_role?, :has?, :is?, :roles, :roles_list, :admin?].each do |api_method|
34
- # @admin_user.respond_to?(api_method).should be_true
35
- # end
36
- #
37
- # # class method API
38
- # [:valid_role?, :valid_roles?, :valid_roles].each do |class_api_method|
39
- # @admin_user.class.respond_to?(class_api_method).should be_true
40
- # end
41
- # end
42
- # #
43
- # describe '#valid_role?' do
44
- # it "should be true that the admin user has a valid role of :guest" do
45
- # # @admin_user.valid_role?(:guest).should be_true
46
- # end
47
- #
48
- # it "should be true that the User class has a valid role of :guest" do
49
- # # User.valid_role?(:guest).should be_true
50
- # end
51
- # end
52
- #
53
- # describe '#valid_roles' do
54
- # it "should be true that the admin user has a valid role of :guest" do
55
- # # @admin_user.valid_roles.should include(:guest, :admin)
56
- # end
57
- #
58
- # it "should be true that the User class has a valid role of :guest" do
59
- # User.valid_roles.should include(:guest, :admin)
60
- # end
61
- # end
62
- #
63
- # describe '#valid_roles?' do
64
- # it "should be true that the admin user has a valid role of :guest" do
65
- # @admin_user.valid_roles?(:guest, :admin).should be_true
66
- # end
67
- #
68
- # it "should be true that the User class has a valid role of :guest" do
69
- # User.valid_roles?(:guest, :admin).should be_true
70
- # end
71
- # end
72
- #
73
- # describe '#has_role?' do
74
- # it "should have admin user role to :admin and not to :user" do
75
- # @admin_user.has_role?(:user).should be_false
76
- # @admin_user.has_role?(:admin).should be_true
77
- # end
78
- #
79
- # it "should be true that guest user has role :guest and not :admin" do
80
- # @guest_user.has_role?(:guest).should be_true
81
- # @guest_user.has_role?(:admin).should be_false
82
- # end
83
- # end
84
- #
85
- # describe '#has?' do
86
- # it "should be true that the admin_user has the :admin role" do
87
- # @admin_user.has?(:admin).should be_true
88
- # end
89
- #
90
- # it "should NOT be true that the admin_user has the :admin role" do
91
- # @guest_user.has?(:admin).should be_false
92
- # end
93
- # end
94
- #
95
- # describe '#has_roles?' do
96
- # it "should be true that the admin_user has the roles :admin" do
97
- # @admin_user.has_roles?(:admin).should be_true
98
- # end
99
- #
100
- # it "should NOT be true that the user has the roles :admin" do
101
- # @guest_user.has_roles?(:admin).should be_false
102
- # end
103
- # end
104
- #
105
- # describe '#roles_list' do
106
- # it "should be true that the first role of admin_user is the :admin role" do
107
- # @admin_user.roles_list.should include(:admin)
108
- # end
109
- #
110
- # it "should be true that the first role of admin_user is the :user role" do
111
- # case @normal_user.class.role_strategy.multiplicity
112
- # when :single
113
- # @normal_user.roles_list.should include(:guest)
114
- # # @normal_user.roles_list.should include(:user)
115
- # when :multi
116
- # @normal_user.roles_list.should include(:user, :guest)
117
- # end
118
- # end
119
- # end
120
- #
121
- # describe '#roles' do
122
- # it "should be true that the roles of admin_user is an array with the role :admin" do
123
- # roles = @admin_user.roles
124
- # if defined?(Role) && roles.kind_of?(Role)
125
- # roles.name.to_sym.should == :admin
126
- # elsif roles.kind_of? Array
127
- # if @normal_user.class.role_strategy.type == :complex
128
- # roles.first.name.to_sym.should == :admin
129
- # end
130
- # if @normal_user.class.role_strategy.name == :admin_flag
131
- # roles.first.should == true
132
- # end
133
- # else
134
- # roles.to_sym.should == :admin
135
- # end
136
- # end
137
- # end
138
- #
139
- # describe '#admin?' do
140
- # it "should be true that admin_user is in the :admin role" do
141
- # @admin_user.admin?.should be_true
142
- # end
143
- #
144
- # it "should NOT be true that the user is in the :admin role" do
145
- # @guest_user.admin?.should be_false
146
- # end
147
- # end
148
- #
149
- # describe '#is?' do
150
- # it "should be true that admin_user is in the :admin role" do
151
- # @admin_user.is?(:admin).should be_true
152
- # end
153
- #
154
- # it "should NOT be true that the user is in the :admin role" do
155
- # @guest_user.is?(:admin).should be_false
156
- # end
157
- # end
158
- #
159
- # describe '#roles=' do
160
- # it "should set user role to :admin" do
161
- # @guest_user.roles = :admin
162
- # @guest_user.has_role?(:admin).should be_true
163
- # @guest_user.roles = :guest
164
- # end
165
- # end
166
- #
167
- # describe '#exchange_roles' do
168
- # it "should exchange user role :user with role :admin" do
169
- # @guest_user.exchange_role :guest, :with => :admin
170
- # @guest_user.has?(:guest).should be_false
171
- # @guest_user.has?(:admin).should be_true
172
- # end
173
- #
174
- # it "should exchange user role :admin with roles :user and :guest" do
175
- # case @admin_user.class.role_strategy.multiplicity
176
- # when :single
177
- # lambda { @admin_user.exchange_role :admin, :with => [:user, :guest] }.should raise_error(ArgumentError)
178
- # when :multi
179
- # @admin_user.exchange_role :admin, :with => [:user, :guest]
180
- # @admin_user.has_role?(:user).should be_true
181
- # @admin_user.has_role?(:guest).should be_true
182
- # @admin_user.has?(:admin).should be_false
183
- # end
184
- # end
185
- # end
186
- #
187
- # describe '#remove_roles' do
188
- # it "should remove user role :admin using #remove_roles" do
189
- # @admin_user.remove_roles :admin
190
- # @admin_user.has_role?(:admin).should_not be_true
191
- # end
26
+ it "should be true that a User that includes Roles::Generic has a complete Roles::Generic interface" do
27
+ # mutation API
28
+ [:roles=, :role=, :add_roles, :add_role, :remove_role, :remove_roles, :exchange_roles, :exchange_role].each do |api_method|
29
+ @admin_user.respond_to?(api_method).should be_true
30
+ end
31
+
32
+ # inspection API
33
+ [:valid_role?, :valid_roles?, :has_roles?, :has_role?, :has?, :is?, :roles, :roles_list, :admin?].each do |api_method|
34
+ @admin_user.respond_to?(api_method).should be_true
35
+ end
36
+
37
+ # class method API
38
+ [:valid_role?, :valid_roles?, :valid_roles].each do |class_api_method|
39
+ @admin_user.class.respond_to?(class_api_method).should be_true
40
+ end
41
+ end
192
42
  #
193
- # it "should remove user role :admin using #remove_role" do
194
- # @guest_user.add_role :admin
195
- # @guest_user.has_role?(:admin).should be_true
196
- # @guest_user.remove_role :admin
197
- # @guest_user.has_role?(:admin).should_not be_true
198
- # end
199
- # end
43
+ describe '#valid_role?' do
44
+ it "should be true that the admin user has a valid role of :guest" do
45
+ # @admin_user.valid_role?(:guest).should be_true
46
+ end
47
+
48
+ it "should be true that the User class has a valid role of :guest" do
49
+ # User.valid_role?(:guest).should be_true
50
+ end
51
+ end
52
+
53
+ describe '#valid_roles' do
54
+ it "should be true that the admin user has a valid role of :guest" do
55
+ # @admin_user.valid_roles.should include(:guest, :admin)
56
+ end
57
+
58
+ it "should be true that the User class has a valid role of :guest" do
59
+ User.valid_roles.should include(:guest, :admin)
60
+ end
61
+ end
62
+
63
+ describe '#valid_roles?' do
64
+ it "should be true that the admin user has a valid role of :guest" do
65
+ @admin_user.valid_roles?(:guest, :admin).should be_true
66
+ end
67
+
68
+ it "should be true that the User class has a valid role of :guest" do
69
+ User.valid_roles?(:guest, :admin).should be_true
70
+ end
71
+ end
72
+
73
+ describe '#has_role?' do
74
+ it "should have admin user role to :admin and not to :user" do
75
+ @admin_user.has_role?(:user).should be_false
76
+ @admin_user.has_role?(:admin).should be_true
77
+ end
78
+
79
+ it "should be true that guest user has role :guest and not :admin" do
80
+ @guest_user.has_role?(:guest).should be_true
81
+ @guest_user.has_role?(:admin).should be_false
82
+ end
83
+ end
84
+
85
+ describe '#has?' do
86
+ it "should be true that the admin_user has the :admin role" do
87
+ @admin_user.has?(:admin).should be_true
88
+ end
89
+
90
+ it "should NOT be true that the admin_user has the :admin role" do
91
+ @guest_user.has?(:admin).should be_false
92
+ end
93
+ end
94
+
95
+ describe '#has_roles?' do
96
+ it "should be true that the admin_user has the roles :admin" do
97
+ @admin_user.has_roles?(:admin).should be_true
98
+ end
99
+
100
+ it "should NOT be true that the user has the roles :admin" do
101
+ @guest_user.has_roles?(:admin).should be_false
102
+ end
103
+ end
104
+
105
+ describe '#roles_list' do
106
+ it "should be true that the first role of admin_user is the :admin role" do
107
+ @admin_user.roles_list.should include(:admin)
108
+ end
109
+
110
+ it "should be true that the first role of admin_user is the :user role" do
111
+ case @normal_user.class.role_strategy.multiplicity
112
+ when :single
113
+ @normal_user.roles_list.should include(:guest)
114
+ # @normal_user.roles_list.should include(:user)
115
+ when :multi
116
+ @normal_user.roles_list.should include(:user, :guest)
117
+ end
118
+ end
119
+ end
120
+
121
+ describe '#roles' do
122
+ it "should be true that the roles of admin_user is an array with the role :admin" do
123
+ roles = @admin_user.roles
124
+ if defined?(Role) && roles.kind_of?(Role)
125
+ roles.name.to_sym.should == :admin
126
+ elsif roles.kind_of? Array
127
+ if @normal_user.class.role_strategy.type == :complex
128
+ roles.first.name.to_sym.should == :admin
129
+ end
130
+ if @normal_user.class.role_strategy.name == :admin_flag
131
+ roles.first.should == true
132
+ end
133
+ else
134
+ roles.to_sym.should == :admin
135
+ end
136
+ end
137
+ end
138
+
139
+ describe '#admin?' do
140
+ it "should be true that admin_user is in the :admin role" do
141
+ @admin_user.admin?.should be_true
142
+ end
143
+
144
+ it "should NOT be true that the user is in the :admin role" do
145
+ @guest_user.admin?.should be_false
146
+ end
147
+ end
148
+
149
+ describe '#is?' do
150
+ it "should be true that admin_user is in the :admin role" do
151
+ @admin_user.is?(:admin).should be_true
152
+ end
153
+
154
+ it "should NOT be true that the user is in the :admin role" do
155
+ @guest_user.is?(:admin).should be_false
156
+ end
157
+ end
158
+
159
+ describe '#roles=' do
160
+ it "should set user role to :admin" do
161
+ @guest_user.roles = :admin
162
+ @guest_user.has_role?(:admin).should be_true
163
+ @guest_user.roles = :guest
164
+ end
165
+ end
166
+
167
+ describe '#exchange_roles' do
168
+ it "should exchange user role :user with role :admin" do
169
+ @guest_user.exchange_role :guest, :with => :admin
170
+ @guest_user.has?(:guest).should be_false
171
+ @guest_user.has?(:admin).should be_true
172
+ end
173
+
174
+ it "should exchange user role :admin with roles :user and :guest" do
175
+ case @admin_user.class.role_strategy.multiplicity
176
+ when :single
177
+ lambda { @admin_user.exchange_role :admin, :with => [:user, :guest] }.should raise_error(ArgumentError)
178
+ when :multi
179
+ @admin_user.exchange_role :admin, :with => [:user, :guest]
180
+ @admin_user.has_role?(:user).should be_true
181
+ @admin_user.has_role?(:guest).should be_true
182
+ @admin_user.has?(:admin).should be_false
183
+ end
184
+ end
185
+ end
186
+
187
+ describe '#remove_roles' do
188
+ it "should remove user role :admin using #remove_roles" do
189
+ @admin_user.remove_roles :admin
190
+ @admin_user.has_role?(:admin).should_not be_true
191
+ end
192
+
193
+ it "should remove user role :admin using #remove_role" do
194
+ @guest_user.add_role :admin
195
+ @guest_user.has_role?(:admin).should be_true
196
+ @guest_user.remove_role :admin
197
+ @guest_user.has_role?(:admin).should_not be_true
198
+ end
199
+ end
200
200
  end
@@ -1,5 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class Role
4
+ end
5
+
3
6
  class User
4
7
  include Mongoid::Document
5
8
  include Roles::Mongoid
@@ -1,5 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class Role
4
+ end
5
+
3
6
  class User
4
7
  include Mongoid::Document
5
8
  include Roles::Mongoid
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-26 00:00:00 +01:00
17
+ date: 2011-01-01 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -250,6 +250,7 @@ files:
250
250
  - Rakefile
251
251
  - VERSION
252
252
  - lib/generators/mongoid/roles/roles_generator.rb
253
+ - lib/generators/mongoid/roles/templates/role.rb
253
254
  - lib/roles_mongoid.rb
254
255
  - lib/roles_mongoid/base.rb
255
256
  - lib/roles_mongoid/base_role.rb