roles_mongoid 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -8,6 +8,7 @@ group :default do
8
8
  gem "require_all", '~> 1.2.0'
9
9
  gem "sugar-high", '>= 0.3.0'
10
10
  gem "roles_generic", '>= 0.3.2'
11
+ gem 'logging_assist', '>= 0.2.0'
11
12
  end
12
13
 
13
14
  group :test do
data/Rakefile CHANGED
@@ -17,7 +17,7 @@ begin
17
17
  gem.add_dependency "roles_generic", '>= 0.3.3'
18
18
 
19
19
  gem.add_dependency 'rails3_artifactor', '>= 0.3.1'
20
- gem.add_dependency 'logging_assist', '~> 0.1.6'
20
+ gem.add_dependency 'logging_assist', '>= 0.2.0'
21
21
 
22
22
 
23
23
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.4.2
@@ -3,33 +3,43 @@ require 'logging_assist'
3
3
 
4
4
  module Mongoid
5
5
  module Generators
6
- class RolesGenerator < Rails::Generators::NamedBase
7
- desc "Add role strategy to a model"
6
+ class RolesGenerator < Rails::Generators::Base
7
+ desc "Add role strategy to a Mongoid User model"
8
8
 
9
- # argument name
9
+ argument :user_class, :type => :string, :default => 'User', :desc => "User class name"
10
10
 
11
- class_option :strategy, :type => :string, :aliases => "-s", :default => 'role_string',
11
+ class_option :strategy, :type => :string, :aliases => "-s", :default => 'role_string',
12
12
  :desc => "Role strategy to use (admin_flag, role_string, roles_string, role_strings, roles_mask, one_role, many_roles, embed_one_role, embed_many_roles)"
13
13
 
14
- class_option :logfile, :type => :string, :default => nil, :desc => "Logfile location"
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"
14
+ class_option :roles, :type => :array, :aliases => "-r", :default => [], :desc => "Valid roles"
15
+ class_option :role_class, :type => :string, :aliases => "-rc", :default => 'Role', :desc => "Role class name"
16
+
17
+ class_option :logfile, :type => :string, :aliases => "-l", :default => nil, :desc => "Logfile location"
18
+
19
+ source_root File.dirname(__FILE__) + '/templates'
17
20
 
18
21
  def apply_role_strategy
19
22
  logger.add_logfile :logfile => logfile if logfile
20
- logger.debug "apply_role_strategy for : #{strategy} in model #{name}"
23
+ logger.debug "apply_role_strategy for : #{strategy} in model #{user_class}"
21
24
 
22
25
  if !valid_strategy?
23
26
  say "Strategy #{strategy} is not currently supported, please try one of #{valid_strategies.join(', ')}", :red
24
27
  end
25
28
 
26
- if !has_model_file?(user_model_name)
27
- say "User model #{user_model_name} not found", :red
29
+ if !has_model_file?(user_class_file)
30
+ say "User model #{user_class_file} not found", :red
28
31
  return
29
32
  end
30
33
 
34
+ if !is_mongoid_model?(user_class_file)
35
+ say "User model #{user_class_file} is not a Mongoid Document", :red
36
+ return
37
+ end
38
+
31
39
  begin
32
- insert_into_model user_model_name, :after => /include Mongoid::\w+/ do
40
+ logger.debug "Trying to insert roles code into #{user_class_file}"
41
+
42
+ insert_into_model user_class_file, :after => /include Mongoid::\w+/ do
33
43
  insertion_text
34
44
  end
35
45
  rescue Exception => e
@@ -46,6 +56,14 @@ module Mongoid
46
56
 
47
57
  use_orm :mongoid
48
58
 
59
+ def user_class_file
60
+ user_class.to_s.underscore
61
+ end
62
+
63
+ def is_mongoid_model? name
64
+ read_model(name) =~ /include Mongoid::\w+/
65
+ end
66
+
49
67
  def valid_strategy?
50
68
  valid_strategies.include? strategy.to_sym
51
69
  end
@@ -58,10 +76,6 @@ module Mongoid
58
76
  options[:logfile]
59
77
  end
60
78
 
61
- def user_model_name
62
- name || 'User'
63
- end
64
-
65
79
  def orm
66
80
  :mongoid
67
81
  end
@@ -88,7 +102,7 @@ module Mongoid
88
102
  end
89
103
 
90
104
  def strategy_option_arg
91
- return ", :role_class => :#{role_class.to_s.underscore}" if role_class_strategy? && role_class.to_s != 'Role'
105
+ return ", :role_class => :#{role_class.underscore}" if role_class_strategy? && role_class.to_s != 'Role'
92
106
  ''
93
107
  end
94
108
 
@@ -102,7 +116,7 @@ module Mongoid
102
116
  end
103
117
 
104
118
  def has_valid_roles_statement?
105
- !(read_model(user_model_name) =~ /valid_roles_are/).nil?
119
+ !(read_model(user_class) =~ /valid_roles_are/).nil?
106
120
  end
107
121
 
108
122
  def insertion_text
@@ -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.1"
8
+ s.version = "0.4.2"
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{2011-01-01}
12
+ s.date = %q{2011-01-10}
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 = [
@@ -49,6 +49,7 @@ Gem::Specification.new do |s|
49
49
  "sandbox/roles_assign.rb",
50
50
  "sandbox/single_role.rb",
51
51
  "sandbox/test.rb",
52
+ "sandbox/test_query.rb",
52
53
  "spec/generator_spec_helper.rb",
53
54
  "spec/roles_mongoid/generators/roles_generator_spec.rb",
54
55
  "spec/roles_mongoid/strategy/api_examples.rb",
@@ -96,6 +97,7 @@ Gem::Specification.new do |s|
96
97
  s.add_runtime_dependency(%q<require_all>, ["~> 1.2.0"])
97
98
  s.add_runtime_dependency(%q<sugar-high>, [">= 0.3.0"])
98
99
  s.add_runtime_dependency(%q<roles_generic>, [">= 0.3.2"])
100
+ s.add_runtime_dependency(%q<logging_assist>, [">= 0.2.0"])
99
101
  s.add_development_dependency(%q<rspec>, [">= 2.0.1"])
100
102
  s.add_runtime_dependency(%q<mongoid>, [">= 2.0.0.beta.20"])
101
103
  s.add_runtime_dependency(%q<bson>, [">= 1.1.4"])
@@ -103,7 +105,7 @@ Gem::Specification.new do |s|
103
105
  s.add_runtime_dependency(%q<require_all>, ["~> 1.2.0"])
104
106
  s.add_runtime_dependency(%q<roles_generic>, [">= 0.3.3"])
105
107
  s.add_runtime_dependency(%q<rails3_artifactor>, [">= 0.3.1"])
106
- s.add_runtime_dependency(%q<logging_assist>, ["~> 0.1.6"])
108
+ s.add_runtime_dependency(%q<logging_assist>, [">= 0.2.0"])
107
109
  else
108
110
  s.add_dependency(%q<mongoid>, ["~> 2.0.0.beta.19"])
109
111
  s.add_dependency(%q<bson>, [">= 1.1.1"])
@@ -111,6 +113,7 @@ Gem::Specification.new do |s|
111
113
  s.add_dependency(%q<require_all>, ["~> 1.2.0"])
112
114
  s.add_dependency(%q<sugar-high>, [">= 0.3.0"])
113
115
  s.add_dependency(%q<roles_generic>, [">= 0.3.2"])
116
+ s.add_dependency(%q<logging_assist>, [">= 0.2.0"])
114
117
  s.add_dependency(%q<rspec>, [">= 2.0.1"])
115
118
  s.add_dependency(%q<mongoid>, [">= 2.0.0.beta.20"])
116
119
  s.add_dependency(%q<bson>, [">= 1.1.4"])
@@ -118,7 +121,7 @@ Gem::Specification.new do |s|
118
121
  s.add_dependency(%q<require_all>, ["~> 1.2.0"])
119
122
  s.add_dependency(%q<roles_generic>, [">= 0.3.3"])
120
123
  s.add_dependency(%q<rails3_artifactor>, [">= 0.3.1"])
121
- s.add_dependency(%q<logging_assist>, ["~> 0.1.6"])
124
+ s.add_dependency(%q<logging_assist>, [">= 0.2.0"])
122
125
  end
123
126
  else
124
127
  s.add_dependency(%q<mongoid>, ["~> 2.0.0.beta.19"])
@@ -127,6 +130,7 @@ Gem::Specification.new do |s|
127
130
  s.add_dependency(%q<require_all>, ["~> 1.2.0"])
128
131
  s.add_dependency(%q<sugar-high>, [">= 0.3.0"])
129
132
  s.add_dependency(%q<roles_generic>, [">= 0.3.2"])
133
+ s.add_dependency(%q<logging_assist>, [">= 0.2.0"])
130
134
  s.add_dependency(%q<rspec>, [">= 2.0.1"])
131
135
  s.add_dependency(%q<mongoid>, [">= 2.0.0.beta.20"])
132
136
  s.add_dependency(%q<bson>, [">= 1.1.4"])
@@ -134,7 +138,7 @@ Gem::Specification.new do |s|
134
138
  s.add_dependency(%q<require_all>, ["~> 1.2.0"])
135
139
  s.add_dependency(%q<roles_generic>, [">= 0.3.3"])
136
140
  s.add_dependency(%q<rails3_artifactor>, [">= 0.3.1"])
137
- s.add_dependency(%q<logging_assist>, ["~> 0.1.6"])
141
+ s.add_dependency(%q<logging_assist>, [">= 0.2.0"])
138
142
  end
139
143
  end
140
144
 
@@ -0,0 +1,49 @@
1
+ require 'mongoid'
2
+
3
+ Mongoid.configure do |config|
4
+ config.master = Mongo::Connection.new.db('testing')
5
+ end
6
+
7
+ Mongoid.database.collections.each do |coll|
8
+ coll.remove
9
+ end
10
+
11
+
12
+ class User
13
+ include Mongoid::Document
14
+
15
+ field :username, :type => String
16
+ field :email, :type => String
17
+
18
+ def self.find_record(login)
19
+ found = where(:username => login).to_a
20
+ found = where(:email => login).to_a if found.empty?
21
+ found
22
+ end
23
+
24
+ def self.find_record_generic(attributes)
25
+ where(attributes).first
26
+ end
27
+
28
+ def self.find_record_alt(login)
29
+ where("function() {return this.username == '#{login}' || this.email == '#{login}'}")
30
+ end
31
+ end
32
+
33
+ User.create :username => 'guest', :email => 'guest@email.com'
34
+ User.create :username => 'admin', :email => 'admin@email.com'
35
+ User.create :username => 'blip', :email => 'guest@email.com'
36
+ User.create :username => 'blip', :email => 'guest@cool.com'
37
+
38
+ puts User.all
39
+
40
+ puts "Found match 'blip'"
41
+
42
+ # puts User.find_record('blip').first.inspect
43
+ puts User.find_record_alt('blip').first.inspect
44
+
45
+ # puts User.find_record_generic(:username => 'blip')
46
+
47
+ puts "Found match 'guest@email.com'"
48
+
49
+ puts User.find_record('guest@email.com').first.inspect
@@ -1,5 +1,7 @@
1
1
  require 'generator_spec_helper'
2
- require_generator :mongoid => :roles
2
+ # require_generator :mongoid => :roles
3
+
4
+ require 'generators/mongoid/roles/roles_generator'
3
5
 
4
6
  # root_dir = Rails3::Assist::Directory.rails_root
5
7
  root_dir = File.join(Rails.application.config.root_dir, 'rails')
@@ -28,7 +30,7 @@ describe 'role strategy generator: admin_flag' do
28
30
  arguments = "User --strategy admin_flag --roles admin user"
29
31
  puts "arguments: #{arguments}"
30
32
  g.run_generator arguments.args
31
- root_dir.should have_model :user do |clazz|
33
+ root_dir.should have_model_file :user do |clazz|
32
34
  clazz.should include_module 'Mongoid::Document'
33
35
  clazz.should include_module 'Roles::Mongoid'
34
36
  puts "clazz: #{clazz}"
@@ -42,11 +44,16 @@ describe 'role strategy generator: admin_flag' do
42
44
  create_model :user do
43
45
  '# content'
44
46
  end
47
+ # puts read_model :user
48
+
45
49
  with_generator do |g|
46
50
  arguments = "User --strategy one_role --roles admin user"
47
51
  puts "arguments: #{arguments}"
48
52
  g.run_generator arguments.args
49
- root_dir.should have_model :user do |clazz|
53
+
54
+ # puts read_model :user
55
+
56
+ root_dir.should have_model_file :user do |clazz|
50
57
  clazz.should include_module 'Mongoid::Document'
51
58
  clazz.should include_module 'Roles::Mongoid'
52
59
  puts "clazz: #{clazz}"
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 1
9
- version: 0.4.1
8
+ - 2
9
+ version: 0.4.2
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: 2011-01-01 00:00:00 +01:00
17
+ date: 2011-01-10 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -110,8 +110,23 @@ dependencies:
110
110
  prerelease: false
111
111
  version_requirements: *id006
112
112
  - !ruby/object:Gem::Dependency
113
- name: rspec
113
+ name: logging_assist
114
114
  requirement: &id007 !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ segments:
120
+ - 0
121
+ - 2
122
+ - 0
123
+ version: 0.2.0
124
+ type: :runtime
125
+ prerelease: false
126
+ version_requirements: *id007
127
+ - !ruby/object:Gem::Dependency
128
+ name: rspec
129
+ requirement: &id008 !ruby/object:Gem::Requirement
115
130
  none: false
116
131
  requirements:
117
132
  - - ">="
@@ -123,10 +138,10 @@ dependencies:
123
138
  version: 2.0.1
124
139
  type: :development
125
140
  prerelease: false
126
- version_requirements: *id007
141
+ version_requirements: *id008
127
142
  - !ruby/object:Gem::Dependency
128
143
  name: mongoid
129
- requirement: &id008 !ruby/object:Gem::Requirement
144
+ requirement: &id009 !ruby/object:Gem::Requirement
130
145
  none: false
131
146
  requirements:
132
147
  - - ">="
@@ -140,10 +155,10 @@ dependencies:
140
155
  version: 2.0.0.beta.20
141
156
  type: :runtime
142
157
  prerelease: false
143
- version_requirements: *id008
158
+ version_requirements: *id009
144
159
  - !ruby/object:Gem::Dependency
145
160
  name: bson
146
- requirement: &id009 !ruby/object:Gem::Requirement
161
+ requirement: &id010 !ruby/object:Gem::Requirement
147
162
  none: false
148
163
  requirements:
149
164
  - - ">="
@@ -155,10 +170,10 @@ dependencies:
155
170
  version: 1.1.4
156
171
  type: :runtime
157
172
  prerelease: false
158
- version_requirements: *id009
173
+ version_requirements: *id010
159
174
  - !ruby/object:Gem::Dependency
160
175
  name: sugar-high
161
- requirement: &id010 !ruby/object:Gem::Requirement
176
+ requirement: &id011 !ruby/object:Gem::Requirement
162
177
  none: false
163
178
  requirements:
164
179
  - - ~>
@@ -170,10 +185,10 @@ dependencies:
170
185
  version: 0.3.1
171
186
  type: :runtime
172
187
  prerelease: false
173
- version_requirements: *id010
188
+ version_requirements: *id011
174
189
  - !ruby/object:Gem::Dependency
175
190
  name: require_all
176
- requirement: &id011 !ruby/object:Gem::Requirement
191
+ requirement: &id012 !ruby/object:Gem::Requirement
177
192
  none: false
178
193
  requirements:
179
194
  - - ~>
@@ -185,10 +200,10 @@ dependencies:
185
200
  version: 1.2.0
186
201
  type: :runtime
187
202
  prerelease: false
188
- version_requirements: *id011
203
+ version_requirements: *id012
189
204
  - !ruby/object:Gem::Dependency
190
205
  name: roles_generic
191
- requirement: &id012 !ruby/object:Gem::Requirement
206
+ requirement: &id013 !ruby/object:Gem::Requirement
192
207
  none: false
193
208
  requirements:
194
209
  - - ">="
@@ -200,10 +215,10 @@ dependencies:
200
215
  version: 0.3.3
201
216
  type: :runtime
202
217
  prerelease: false
203
- version_requirements: *id012
218
+ version_requirements: *id013
204
219
  - !ruby/object:Gem::Dependency
205
220
  name: rails3_artifactor
206
- requirement: &id013 !ruby/object:Gem::Requirement
221
+ requirement: &id014 !ruby/object:Gem::Requirement
207
222
  none: false
208
223
  requirements:
209
224
  - - ">="
@@ -215,22 +230,22 @@ dependencies:
215
230
  version: 0.3.1
216
231
  type: :runtime
217
232
  prerelease: false
218
- version_requirements: *id013
233
+ version_requirements: *id014
219
234
  - !ruby/object:Gem::Dependency
220
235
  name: logging_assist
221
- requirement: &id014 !ruby/object:Gem::Requirement
236
+ requirement: &id015 !ruby/object:Gem::Requirement
222
237
  none: false
223
238
  requirements:
224
- - - ~>
239
+ - - ">="
225
240
  - !ruby/object:Gem::Version
226
241
  segments:
227
242
  - 0
228
- - 1
229
- - 6
230
- version: 0.1.6
243
+ - 2
244
+ - 0
245
+ version: 0.2.0
231
246
  type: :runtime
232
247
  prerelease: false
233
- version_requirements: *id014
248
+ version_requirements: *id015
234
249
  description: Makes it easy to set a role strategy on your User model in Mongoid
235
250
  email: kmandrup@gmail.com
236
251
  executables: []
@@ -273,6 +288,7 @@ files:
273
288
  - sandbox/roles_assign.rb
274
289
  - sandbox/single_role.rb
275
290
  - sandbox/test.rb
291
+ - sandbox/test_query.rb
276
292
  - spec/generator_spec_helper.rb
277
293
  - spec/roles_mongoid/generators/roles_generator_spec.rb
278
294
  - spec/roles_mongoid/strategy/api_examples.rb