mongoid 3.0.23 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. data/CHANGELOG.md +253 -9
  2. data/LICENSE +1 -1
  3. data/README.md +4 -1
  4. data/lib/config/locales/en.yml +7 -6
  5. data/lib/mongoid.rb +18 -1
  6. data/lib/mongoid/atomic.rb +22 -20
  7. data/lib/mongoid/atomic/paths/embedded.rb +19 -5
  8. data/lib/mongoid/atomic/paths/root.rb +1 -1
  9. data/lib/mongoid/atomic/positionable.rb +73 -0
  10. data/lib/mongoid/attributes.rb +63 -1
  11. data/lib/mongoid/callbacks.rb +58 -4
  12. data/lib/mongoid/components.rb +8 -3
  13. data/lib/mongoid/config.rb +71 -23
  14. data/lib/mongoid/contextual.rb +2 -1
  15. data/lib/mongoid/contextual/aggregable/mongo.rb +27 -63
  16. data/lib/mongoid/contextual/atomic.rb +4 -3
  17. data/lib/mongoid/contextual/find_and_modify.rb +1 -1
  18. data/lib/mongoid/contextual/geo_near.rb +238 -0
  19. data/lib/mongoid/contextual/map_reduce.rb +12 -1
  20. data/lib/mongoid/contextual/memory.rb +36 -31
  21. data/lib/mongoid/contextual/mongo.rb +147 -91
  22. data/lib/mongoid/contextual/queryable.rb +25 -0
  23. data/lib/mongoid/copyable.rb +4 -1
  24. data/lib/mongoid/criteria.rb +23 -275
  25. data/lib/mongoid/criterion/findable.rb +179 -0
  26. data/lib/mongoid/criterion/modifiable.rb +191 -0
  27. data/lib/mongoid/criterion/scoping.rb +11 -6
  28. data/lib/mongoid/document.rb +7 -56
  29. data/lib/mongoid/equality.rb +66 -0
  30. data/lib/mongoid/errors/mongoid_error.rb +7 -3
  31. data/lib/mongoid/extensions/array.rb +13 -1
  32. data/lib/mongoid/extensions/date.rb +9 -2
  33. data/lib/mongoid/extensions/hash.rb +38 -2
  34. data/lib/mongoid/extensions/nil_class.rb +12 -0
  35. data/lib/mongoid/extensions/object.rb +24 -0
  36. data/lib/mongoid/extensions/string.rb +14 -2
  37. data/lib/mongoid/extensions/time.rb +4 -1
  38. data/lib/mongoid/fields.rb +49 -5
  39. data/lib/mongoid/fields/foreign_key.rb +12 -0
  40. data/lib/mongoid/fields/standard.rb +12 -0
  41. data/lib/mongoid/finders.rb +8 -0
  42. data/lib/mongoid/hierarchy.rb +19 -1
  43. data/lib/mongoid/indexes.rb +30 -4
  44. data/lib/mongoid/indexes/validators/options.rb +12 -2
  45. data/lib/mongoid/inspection.rb +2 -1
  46. data/lib/mongoid/matchers/strategies.rb +5 -5
  47. data/lib/mongoid/observer.rb +27 -36
  48. data/lib/mongoid/persistence.rb +42 -17
  49. data/lib/mongoid/persistence/atomic.rb +10 -5
  50. data/lib/mongoid/persistence/atomic/operation.rb +26 -9
  51. data/lib/mongoid/persistence/atomic/unset.rb +1 -1
  52. data/lib/mongoid/persistence/operations/embedded/insert.rb +5 -2
  53. data/lib/mongoid/persistence/operations/embedded/remove.rb +5 -2
  54. data/lib/mongoid/persistence/operations/update.rb +7 -3
  55. data/lib/mongoid/railties/database.rake +12 -19
  56. data/lib/mongoid/relations.rb +2 -0
  57. data/lib/mongoid/relations/accessors.rb +30 -8
  58. data/lib/mongoid/relations/binding.rb +5 -1
  59. data/lib/mongoid/relations/bindings/referenced/in.rb +1 -1
  60. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +3 -3
  61. data/lib/mongoid/relations/counter_cache.rb +107 -0
  62. data/lib/mongoid/relations/embedded/batchable.rb +13 -4
  63. data/lib/mongoid/relations/embedded/many.rb +30 -1
  64. data/lib/mongoid/relations/macros.rb +2 -0
  65. data/lib/mongoid/relations/marshalable.rb +0 -1
  66. data/lib/mongoid/relations/metadata.rb +63 -11
  67. data/lib/mongoid/relations/options.rb +1 -0
  68. data/lib/mongoid/relations/proxy.rb +45 -2
  69. data/lib/mongoid/relations/referenced/in.rb +11 -2
  70. data/lib/mongoid/relations/referenced/many.rb +31 -3
  71. data/lib/mongoid/relations/referenced/many_to_many.rb +31 -3
  72. data/lib/mongoid/relations/referenced/one.rb +1 -1
  73. data/lib/mongoid/relations/targets/enumerable.rb +5 -1
  74. data/lib/mongoid/relations/touchable.rb +35 -6
  75. data/lib/mongoid/reloading.rb +5 -3
  76. data/lib/mongoid/scoping.rb +2 -2
  77. data/lib/mongoid/sessions.rb +57 -7
  78. data/lib/mongoid/sessions/factory.rb +22 -1
  79. data/lib/mongoid/threaded.rb +4 -30
  80. data/lib/mongoid/threaded/lifecycle.rb +12 -12
  81. data/lib/mongoid/timestamps.rb +1 -0
  82. data/lib/mongoid/timestamps/created.rb +2 -0
  83. data/lib/mongoid/timestamps/created/short.rb +19 -0
  84. data/lib/mongoid/timestamps/short.rb +10 -0
  85. data/lib/mongoid/timestamps/updated.rb +2 -0
  86. data/lib/mongoid/timestamps/updated/short.rb +19 -0
  87. data/lib/mongoid/validations.rb +2 -0
  88. data/lib/mongoid/validations/queryable.rb +2 -2
  89. data/lib/mongoid/validations/uniqueness.rb +1 -18
  90. data/lib/mongoid/version.rb +1 -1
  91. data/lib/rails/generators/mongoid/model/model_generator.rb +1 -0
  92. data/lib/rails/generators/mongoid/model/templates/model.rb.tt +3 -0
  93. data/lib/rails/mongoid.rb +53 -29
  94. data/lib/support/ruby_version.rb +26 -0
  95. metadata +18 -7
@@ -62,6 +62,8 @@ module Mongoid
62
62
  relation.do_or_do_not(:in_memory) || relation
63
63
  elsif fields[attribute].try(:localized?)
64
64
  attributes[attribute]
65
+ elsif respond_to?("#{attr}_before_type_cast")
66
+ send("#{attr}_before_type_cast")
65
67
  else
66
68
  send(attr)
67
69
  end
@@ -19,11 +19,11 @@ module Mongoid
19
19
  def with_query(document)
20
20
  klass = document.class
21
21
  begin
22
- Threaded.begin("#{klass.name}-validate-with-query")
22
+ Threaded.begin_execution("#{klass.name}-validate-with-query")
23
23
  yield
24
24
  ensure
25
25
  klass.clear_persistence_options unless document.errors.empty?
26
- Threaded.exit("#{klass.name}-validate-with-query")
26
+ Threaded.exit_execution("#{klass.name}-validate-with-query")
27
27
  end
28
28
  end
29
29
  end
@@ -267,7 +267,7 @@ module Mongoid
267
267
  # @since 2.4.10
268
268
  def validate_root(document, attribute, value)
269
269
  criteria = create_criteria(klass, document, attribute, value)
270
- if criteria.with(persistence_options(criteria)).exists?
270
+ if criteria.with(consistency: :strong).exists?
271
271
  add_error(document, attribute, value)
272
272
  end
273
273
  end
@@ -288,23 +288,6 @@ module Mongoid
288
288
  document.send("attribute_changed?", attribute.to_s) ||
289
289
  scope_value_changed?(document)
290
290
  end
291
-
292
- # Get the persistence options to perform to check, merging with any
293
- # existing.
294
- #
295
- # @api private
296
- #
297
- # @example Get the persistence options.
298
- # validator.persistence_options(criteria)
299
- #
300
- # @param [ Criteria ] criteria The criteria.
301
- #
302
- # @return [ Hash ] The persistence options.
303
- #
304
- # @since 3.0.23
305
- def persistence_options(criteria)
306
- (criteria.klass.persistence_options || {}).merge!(consistency: :strong)
307
- end
308
291
  end
309
292
  end
310
293
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- VERSION = "3.0.23"
3
+ VERSION = "3.1.0"
4
4
  end
@@ -13,6 +13,7 @@ module Mongoid
13
13
  class_option :timestamps, type: :boolean
14
14
  class_option :parent, type: :string, desc: "The parent class for the generated model"
15
15
  class_option :versioning, type: :boolean, default: false, desc: "Enable mongoid versioning"
16
+ class_option :collection, type: :string, desc: "The collection for storing model's documents"
16
17
 
17
18
  def create_model_file
18
19
  template "model.rb.tt", File.join("app/models", class_path, "#{file_name}.rb")
@@ -9,6 +9,9 @@ class <%= class_name %><%= " < #{options[:parent].classify}" if options[:parent]
9
9
  <% if options[:versioning] -%>
10
10
  include Mongoid::Versioning
11
11
  <% end -%>
12
+ <% if options[:collection] -%>
13
+ store_in collection: "<%= options[:collection] %>"
14
+ <% end -%>
12
15
  <% attributes.reject{|attr| attr.reference?}.each do |attribute| -%>
13
16
  field :<%= attribute.name %>, type: <%= attribute.type_class %>
14
17
  <% end -%>
@@ -3,20 +3,19 @@ module Rails
3
3
  module Mongoid
4
4
  extend self
5
5
 
6
- # Create indexes for each model given the provided pattern and the class is
6
+ # Create indexes for each model given the provided globs and the class is
7
7
  # not embedded.
8
8
  #
9
9
  # @example Create all the indexes.
10
10
  # Rails::Mongoid.create_indexes("app/models/**/*.rb")
11
11
  #
12
- # @param [ String ] pattern The file matching pattern.
12
+ # @param [ Array<String> ] globs The file matching globs.
13
13
  #
14
- # @return [ Array<String> ] The file names.
14
+ # @return [ Array<Class> ] The indexed models.
15
15
  #
16
16
  # @since 2.1.0
17
- def create_indexes(pattern)
18
- logger = Logger.new($stdout)
19
- models(pattern).each do |model|
17
+ def create_indexes(*globs)
18
+ models(*globs).each do |model|
20
19
  next if model.index_options.empty?
21
20
  unless model.embedded?
22
21
  model.create_indexes
@@ -24,55 +23,76 @@ module Rails
24
23
  model.index_options.each_pair do |index, options|
25
24
  logger.info("MONGOID: Index: #{index}, Options: #{options}")
26
25
  end
26
+ model
27
27
  else
28
28
  logger.info("MONGOID: Index ignored on: #{model}, please define in the root model.")
29
+ nil
29
30
  end
30
- end
31
+ end.compact
31
32
  end
32
33
 
33
- # Remove indexes for each model given the provided pattern and the class is
34
+ # Remove indexes for each model given the provided globs and the class is
34
35
  # not embedded.
35
36
  #
36
37
  # @example Remove all the indexes.
37
38
  # Rails::Mongoid.create_indexes("app/models/**/*.rb")
38
39
  #
39
- # @param [ String ] pattern The file matching pattern.
40
+ # @param [ Array<String> ] globs The file matching globs.
40
41
  #
41
- # @return [ Array<String> ] The file names.
42
+ # @return [ Array<Class> ] The un-indexed models.
42
43
  #
43
- def remove_indexes(pattern)
44
- logger = Logger.new($stdout)
45
- models(pattern).each do |model|
44
+ def remove_indexes(*globs)
45
+ models(*globs).each do |model|
46
46
  next if model.embedded?
47
47
  indexes = model.collection.indexes.map{ |doc| doc["name"] }
48
48
  indexes.delete_one("_id_")
49
49
  model.remove_indexes
50
50
  logger.info("MONGOID: Removing indexes on: #{model} for: #{indexes.join(', ')}.")
51
- end
51
+ model
52
+ end.compact
52
53
  end
53
54
 
54
- # Return all models matching the pattern.
55
+ # Return all models matching the globs or, if no globs are specified, all
56
+ # possible models known from engines, the app, any gems, etc.
55
57
  #
56
- # @example Return all models.
58
+ # @example Return *all* models. Return all models under app/models/
59
+ # Rails::Mongoid.models
57
60
  # Rails::Mongoid.models("app/models/**/*.rb")
58
61
  #
59
- # @param [ String ] pattern The file matching pattern.
62
+ # @param [ String ] glob The file matching glob.
60
63
  #
61
64
  # @return [ Array<Class> ] The models.
62
65
  #
63
- def models(pattern)
64
- Dir.glob(pattern).map do |file|
65
- logger = Logger.new($stdout)
66
- begin
67
- determine_model(file, logger)
68
- rescue => e
69
- logger.error(%Q{MONGOID: Failed to determine model from #{file}:
70
- #{e.class}:#{e.message}
71
- #{e.backtrace.join("\n")}
72
- })
73
- nil
66
+ def models(*globs)
67
+ all_possible_models = globs.empty?
68
+
69
+ if globs.empty?
70
+ engines_models_paths = Rails.application.railties.engines.map{|engine| engine.paths["app/models"].expanded}
71
+ root_models_paths = Rails.application.paths["app/models"]
72
+ models_paths = engines_models_paths.push(root_models_paths).flatten
73
+ globs.replace(models_paths.map{|path| "#{path}/**/*.rb"})
74
+ end
75
+
76
+ models = []
77
+
78
+ globs.flatten.compact.each do |glob|
79
+ Dir.glob(glob).map do |file|
80
+ begin
81
+ model = determine_model(file, logger)
82
+ models.push(model)
83
+ rescue => e
84
+ logger.error(%Q{MONGOID: Failed to determine model from #{file}:
85
+ #{e.class}:#{e.message}
86
+ #{e.backtrace.join("\n")}
87
+ })
88
+ nil
89
+ end
74
90
  end
75
- end.flatten.compact
91
+ end
92
+
93
+ models = (::Mongoid.models | models) if all_possible_models
94
+
95
+ models.compact.sort_by { |model| model.name || '' }
76
96
  end
77
97
 
78
98
  # Use the application configuration to get every model and require it, so
@@ -150,5 +170,9 @@ module Rails
150
170
  end
151
171
  klass if klass && klass.ancestors.include?(::Mongoid::Document)
152
172
  end
173
+
174
+ def logger
175
+ @logger ||= Logger.new($stdout)
176
+ end
153
177
  end
154
178
  end
@@ -0,0 +1,26 @@
1
+ # Go ahead and fail if not using Ruby 1.9.3, no since in letting people
2
+ # squarm for answers
3
+ def raise_version_error(message)
4
+ puts message
5
+ Rails.logger.info(message) if defined?(Rails)
6
+ raise
7
+ end
8
+
9
+ def invalid_version?
10
+ RUBY_VERSION =~ /([\d]+)\.([\d]+)\.([\d]+)/
11
+ major, minor, revision = $1.to_i, $2.to_i, $3.to_i
12
+ if defined?(JRUBY_VERSION)
13
+ major <= 1 && minor <= 9 && revision <= 1
14
+ else
15
+ major <= 1 && minor <= 9 && revision <= 2
16
+ end
17
+ end
18
+
19
+ if invalid_version?
20
+ message = %{
21
+ Mongoid requires MRI version 1.9.3+ or JRuby 1.6.0+ running in 1.9 mode.
22
+ You're current Ruby version is defined as #{RUBY_VERSION}. Please see:
23
+ http://mongoid.org/en/mongoid/docs/tips.html#ruby for details.
24
+ }
25
+ raise_version_error(message)
26
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.23
4
+ version: 3.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-24 00:00:00.000000000 Z
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '3.1'
21
+ version: '3.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '3.1'
29
+ version: '3.2'
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: tzinfo
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: '1.2'
53
+ version: 1.4.2
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: '1.2'
61
+ version: 1.4.2
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: origin
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -90,6 +90,7 @@ files:
90
90
  - lib/mongoid/atomic/paths/embedded.rb
91
91
  - lib/mongoid/atomic/paths/root.rb
92
92
  - lib/mongoid/atomic/paths.rb
93
+ - lib/mongoid/atomic/positionable.rb
93
94
  - lib/mongoid/atomic.rb
94
95
  - lib/mongoid/attributes/processing.rb
95
96
  - lib/mongoid/attributes/readonly.rb
@@ -109,17 +110,22 @@ files:
109
110
  - lib/mongoid/contextual/command.rb
110
111
  - lib/mongoid/contextual/eager.rb
111
112
  - lib/mongoid/contextual/find_and_modify.rb
113
+ - lib/mongoid/contextual/geo_near.rb
112
114
  - lib/mongoid/contextual/map_reduce.rb
113
115
  - lib/mongoid/contextual/memory.rb
114
116
  - lib/mongoid/contextual/mongo.rb
117
+ - lib/mongoid/contextual/queryable.rb
115
118
  - lib/mongoid/contextual.rb
116
119
  - lib/mongoid/copyable.rb
117
120
  - lib/mongoid/criteria.rb
121
+ - lib/mongoid/criterion/findable.rb
118
122
  - lib/mongoid/criterion/inspection.rb
119
123
  - lib/mongoid/criterion/marshalable.rb
124
+ - lib/mongoid/criterion/modifiable.rb
120
125
  - lib/mongoid/criterion/scoping.rb
121
126
  - lib/mongoid/dirty.rb
122
127
  - lib/mongoid/document.rb
128
+ - lib/mongoid/equality.rb
123
129
  - lib/mongoid/errors/ambiguous_relationship.rb
124
130
  - lib/mongoid/errors/callback.rb
125
131
  - lib/mongoid/errors/delete_restriction.rb
@@ -276,6 +282,7 @@ files:
276
282
  - lib/mongoid/relations/cascading.rb
277
283
  - lib/mongoid/relations/constraint.rb
278
284
  - lib/mongoid/relations/conversions.rb
285
+ - lib/mongoid/relations/counter_cache.rb
279
286
  - lib/mongoid/relations/cyclic.rb
280
287
  - lib/mongoid/relations/embedded/batchable.rb
281
288
  - lib/mongoid/relations/embedded/in.rb
@@ -312,8 +319,11 @@ files:
312
319
  - lib/mongoid/state.rb
313
320
  - lib/mongoid/threaded/lifecycle.rb
314
321
  - lib/mongoid/threaded.rb
322
+ - lib/mongoid/timestamps/created/short.rb
315
323
  - lib/mongoid/timestamps/created.rb
324
+ - lib/mongoid/timestamps/short.rb
316
325
  - lib/mongoid/timestamps/timeless.rb
326
+ - lib/mongoid/timestamps/updated/short.rb
317
327
  - lib/mongoid/timestamps/updated.rb
318
328
  - lib/mongoid/timestamps.rb
319
329
  - lib/mongoid/unit_of_work.rb
@@ -339,6 +349,7 @@ files:
339
349
  - lib/rails/generators/mongoid/observer/templates/observer.rb.tt
340
350
  - lib/rails/generators/mongoid_generator.rb
341
351
  - lib/rails/mongoid.rb
352
+ - lib/support/ruby_version.rb
342
353
  - CHANGELOG.md
343
354
  - LICENSE
344
355
  - README.md
@@ -364,7 +375,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
364
375
  version: 1.3.6
365
376
  requirements: []
366
377
  rubyforge_project: mongoid
367
- rubygems_version: 1.8.25
378
+ rubygems_version: 1.8.24
368
379
  signing_key:
369
380
  specification_version: 3
370
381
  summary: Elegant Persistance in Ruby for MongoDB.