mongoid 4.0.0.beta1 → 4.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 158b72d6477798214b724968813850224dc3657f
4
- data.tar.gz: beff6994a7f9818cb6a956a1dbe682c66d1155f1
3
+ metadata.gz: cc6ad8c410d4ae0e13b891cef3eddfddd41a236d
4
+ data.tar.gz: b31dfba7b12e23a91520409662c9e73dd8452cd0
5
5
  SHA512:
6
- metadata.gz: d01a18d141f3883470322bf3e713bd141003d85df34b97cdc45437819ae6ded25291b6c0846fc1a59e0a9d2e88a51da50684ead9ddd4747d405157f9324c7de4
7
- data.tar.gz: 872d41a758ecdf59a503bda991cb8677982e774e7cc2509020c2ffc0953bbac559636ed2cd0bd58a07e9099b78195f5688d91748d55025231ef78de210c36f66
6
+ metadata.gz: a89977a8214ab3eeb419d17976d446d38ac4279300e093e357da037512380fe0812fdc7d042fe6405d4a409388a859bd1c466175d10ad18110a2840e7211e646
7
+ data.tar.gz: e9fb4f020f0a90fb726453858926fca07941fafdfe270e9cdb40ee697fe5daa3a68937a0b148d6067e75ef9762b134b2263a52f1eefc6c992f02bb46c0c5cde6
@@ -281,6 +281,10 @@ For instructions on upgrading to newer versions, visit
281
281
  https://github.com/mongoid/mongoid/blob/master/lib/mongoid/fields.rb#L16
282
282
  for the available mappings.
283
283
 
284
+ * \#3580 Fields can now be reset to their default values, with the methods:
285
+
286
+ document.reset_name_to_default!
287
+
284
288
  * \#3513 Documents now have a `#destroy!` method that will raise a
285
289
  `Mongoid::Errors::DocumentNotDestroyed` error if a destroy callback returns
286
290
  a false value.
@@ -341,6 +345,13 @@ For instructions on upgrading to newer versions, visit
341
345
 
342
346
  ### Resolved Issues
343
347
 
348
+ * \#3619 Don't validate documents that are flagged for destruction.
349
+ (Christopher J. Bottaro)
350
+
351
+ * \#3617 Don't skip index creation on cyclic documents. (shaiker)
352
+
353
+ * \#3568 Fixed missing attributes error on present localized fields.
354
+
344
355
  * \#3514 Fixed query cache to work on first/last calls.
345
356
 
346
357
  * \#3383/\#3495 Fix has_and_belongs_to_many eager load. (Arthur Neves)
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- Mongoid [![Build Status](https://secure.travis-ci.org/mongoid/mongoid.png?branch=master)](http://travis-ci.org/mongoid/mongoid) [![Code Climate](https://codeclimate.com/github/mongoid/mongoid.png)](https://codeclimate.com/github/mongoid/mongoid) [![Coverage Status](https://coveralls.io/repos/mongoid/mongoid/badge.png?branch=master)](https://coveralls.io/r/mongoid/mongoid?branch=master) [![Gem Version](https://badge.fury.io/rb/mongoid.png)](http://badge.fury.io/rb/mongoid) [![Dependency Status](https://www.versioneye.com/ruby/mongoid/3.1.6/badge.png)](https://www.versioneye.com/ruby/mongoid/3.1.6)
1
+ Mongoid [![Build Status](https://travis-ci.org/mongoid/mongoid.svg)](https://travis-ci.org/mongoid/mongoid) [![Code Climate](https://codeclimate.com/github/mongoid/mongoid.png)](https://codeclimate.com/github/mongoid/mongoid) [![Coverage Status](https://coveralls.io/repos/mongoid/mongoid/badge.png?branch=master)](https://coveralls.io/r/mongoid/mongoid?branch=master) [![Gem Version](https://badge.fury.io/rb/mongoid.png)](http://badge.fury.io/rb/mongoid) [![Dependency Status](https://www.versioneye.com/ruby/mongoid/3.1.6/badge.png)](https://www.versioneye.com/ruby/mongoid/3.1.6)
2
2
  ========
3
3
 
4
4
  Mongoid is an ODM (Object-Document-Mapper) framework for MongoDB in Ruby.
@@ -184,6 +184,12 @@ en:
184
184
  \_\_\_\_include Mongoid::Document\n
185
185
  \_\_\_\_store_in collection: 'artists', database: 'secondary'\n
186
186
  \_\_end\n\n"
187
+ invalid_storage_parent:
188
+ message: "Invalid store_in call on class %{klass}."
189
+ summary: "The :store_in macro can only be called on a base Mongoid Document"
190
+ resolution: "Remove the store_in call on class %{klass}, as it will use its
191
+ parent store configuration. Or remove the hierarchy extension for this
192
+ class."
187
193
  invalid_time:
188
194
  message: "'%{value}' is not a valid Time."
189
195
  summary: "Mongoid tries to serialize the values for Date, DateTime, and
@@ -5,6 +5,7 @@ require "delegate"
5
5
  require "time"
6
6
  require "set"
7
7
 
8
+ require "active_support"
8
9
  require "active_support/core_ext"
9
10
  require "active_support/json"
10
11
  require "active_support/inflector"
@@ -91,7 +91,7 @@ module Mongoid
91
91
  # @since 1.0.0
92
92
  def read_attribute(name)
93
93
  normalized = database_field_name(name.to_s)
94
- if attribute_missing?(name)
94
+ if attribute_missing?(normalized)
95
95
  raise ActiveModel::MissingAttributeError, "Missing attribute: '#{name}'."
96
96
  end
97
97
  if hash_dot_syntax?(normalized)
@@ -237,12 +237,29 @@ module Mongoid
237
237
  def attribute_missing?(name)
238
238
  selection = __selected_fields
239
239
  return false unless selection
240
- (selection.values.first == 0 && selection[name.to_s] == 0) ||
241
- (selection.values.first == 1 && !selection.has_key?(name.to_s))
240
+ field = fields[name]
241
+ (selection.values.first == 0 && selection_excluded?(name, selection, field)) ||
242
+ (selection.values.first == 1 && !selection_included?(name, selection, field))
242
243
  end
243
244
 
244
245
  private
245
246
 
247
+ def selection_excluded?(name, selection, field)
248
+ if field && field.localized?
249
+ selection["#{name}.#{::I18n.locale}"] == 0
250
+ else
251
+ selection[name] == 0
252
+ end
253
+ end
254
+
255
+ def selection_included?(name, selection, field)
256
+ if field && field.localized?
257
+ selection.has_key?("#{name}.#{::I18n.locale}")
258
+ else
259
+ selection.has_key?(name)
260
+ end
261
+ end
262
+
246
263
  # Does the string contain dot syntax for accessing hashes?
247
264
  #
248
265
  # @api private
@@ -298,6 +315,7 @@ module Mongoid
298
315
  alias #{name}_change #{original}_change
299
316
  alias #{name}_changed? #{original}_changed?
300
317
  alias reset_#{name}! reset_#{original}!
318
+ alias reset_#{name}_to_default! reset_#{original}_to_default!
301
319
  alias #{name}_was #{original}_was
302
320
  alias #{name}_will_change! #{original}_will_change!
303
321
  alias #{name}_before_type_cast #{original}_before_type_cast
@@ -248,6 +248,15 @@ module Mongoid
248
248
  attributes[attr] = changed_attributes.delete(attr) if attribute_changed?(attr)
249
249
  end
250
250
 
251
+ def reset_attribute_to_default!(attr)
252
+ attr = database_field_name(attr)
253
+ if field = fields[attr]
254
+ __send__("#{attr}=", field.eval_default(self))
255
+ else
256
+ __send__("#{attr}=", nil)
257
+ end
258
+ end
259
+
251
260
  module ClassMethods
252
261
 
253
262
  private
@@ -270,6 +279,7 @@ module Mongoid
270
279
  create_dirty_default_change_check(name, meth)
271
280
  create_dirty_previous_value_accessor(name, meth)
272
281
  create_dirty_reset(name, meth)
282
+ create_dirty_reset_to_default(name, meth)
273
283
  end
274
284
 
275
285
  # Creates the dirty change accessor.
@@ -373,6 +383,23 @@ module Mongoid
373
383
  end
374
384
  end
375
385
  end
386
+
387
+ # Creates the dirty change reset to default.
388
+ #
389
+ # @example Create the reset.
390
+ # Model.create_dirty_reset_to_default("name", "alias")
391
+ #
392
+ # @param [ String ] name The attribute name.
393
+ # @param [ String ] meth The name of the accessor.
394
+ #
395
+ # @since 3.0.0
396
+ def create_dirty_reset_to_default(name, meth)
397
+ generated_methods.module_eval do
398
+ re_define_method("reset_#{meth}_to_default!") do
399
+ reset_attribute_to_default!(name)
400
+ end
401
+ end
402
+ end
376
403
  end
377
404
  end
378
405
  end
@@ -26,13 +26,11 @@ module Mongoid
26
26
  # All modules that a +Document+ is composed of are defined in this
27
27
  # module, to keep the document class from getting too cluttered.
28
28
  included do
29
- extend ActiveModel::Translation
30
29
  extend Findable
31
30
  end
32
31
 
33
- include ActiveModel::Conversion
32
+ include ActiveModel::Model
34
33
  include ActiveModel::ForbiddenAttributesProtection
35
- include ActiveModel::Naming
36
34
  include ActiveModel::Serializers::JSON
37
35
  include ActiveModel::Serializers::Xml
38
36
  include Atomic
@@ -295,7 +295,7 @@ module Mongoid
295
295
  updates["$set"].merge!(doc.atomic_updates["$set"] || {})
296
296
  doc.move_changes
297
297
  end
298
- collection.find(selector).update(updates)
298
+ collection.find(selector).update(updates) unless updates["$set"].empty?
299
299
  end
300
300
 
301
301
  # Get the limiting value.
@@ -17,6 +17,7 @@ require "mongoid/errors/invalid_path"
17
17
  require "mongoid/errors/invalid_scope"
18
18
  require "mongoid/errors/invalid_set_polymorphic_relation"
19
19
  require "mongoid/errors/invalid_storage_options"
20
+ require "mongoid/errors/invalid_storage_parent"
20
21
  require "mongoid/errors/invalid_time"
21
22
  require "mongoid/errors/invalid_value"
22
23
  require "mongoid/errors/inverse_not_found"
@@ -8,7 +8,7 @@ module Mongoid
8
8
  # Create the new error.
9
9
  #
10
10
  # @example Create the new error.
11
- # InvalidStorageOptions.new(:collection_name)
11
+ # InvalidStorageOptions.new(Person, invalid_option: 'name')
12
12
  #
13
13
  # @param [ Class ] klass The model class.
14
14
  # @param [ Hash, String, Symbol ] options The provided options.
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Errors
4
+
5
+ # Raised when calling store_in in a sub-class of Mongoid::Document
6
+ class InvalidStorageParent < MongoidError
7
+
8
+ # Create the new error.
9
+ #
10
+ # @example Create the new error.
11
+ # InvalidStorageParent.new(Person)
12
+ #
13
+ # @param [ Class ] klass The model class.
14
+ #
15
+ # @since 4.0.0
16
+ def initialize(klass)
17
+ super(
18
+ compose_message(
19
+ "invalid_storage_parent",
20
+ { klass: klass }
21
+ )
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
@@ -112,10 +112,10 @@ module Mongoid
112
112
  def alias_query_cache_clear(*method_names)
113
113
  method_names.each do |method_name|
114
114
  class_eval <<-CODE, __FILE__, __LINE__ + 1
115
- def #{method_name}_with_clear_cache(*args)
116
- QueryCache.clear_cache
117
- #{method_name}_without_clear_cache(*args)
118
- end
115
+ def #{method_name}_with_clear_cache(*args) # def upsert_with_clear_cache(*args)
116
+ QueryCache.clear_cache # QueryCache.clear_cache
117
+ #{method_name}_without_clear_cache(*args) # upsert_without_clear_cache(*args)
118
+ end # end
119
119
  CODE
120
120
 
121
121
  alias_method_chain method_name, :clear_cache
@@ -131,10 +131,11 @@ module Mongoid
131
131
 
132
132
  private
133
133
 
134
- def with_cache
134
+ def with_cache(context = :cursor, &block)
135
135
  return yield unless QueryCache.enabled?
136
136
  return yield if system_collection?
137
- key = cache_key
137
+ key = cache_key.push(context)
138
+
138
139
  if QueryCache.cache_table.has_key?(key)
139
140
  instrument(key) { QueryCache.cache_table[key] }
140
141
  else
@@ -182,7 +183,7 @@ module Mongoid
182
183
  #
183
184
  # @since 4.0.0
184
185
  def first_with_cache
185
- with_cache do
186
+ with_cache(:first) do
186
187
  first_without_cache
187
188
  end
188
189
  end
@@ -190,7 +191,7 @@ module Mongoid
190
191
  private
191
192
 
192
193
  def cache_key
193
- [ operation.database, operation.collection, operation.selector ]
194
+ [ operation.database, operation.collection, operation.selector, operation.limit, operation.skip, operation.fields ]
194
195
  end
195
196
 
196
197
  def system_collection?
@@ -232,7 +233,7 @@ module Mongoid
232
233
  private
233
234
 
234
235
  def cache_key
235
- [ @database, @collection, @selector ]
236
+ [ @database, @collection, @selector, @options[:limit], @options[:skip], @options[:fields] ]
236
237
  end
237
238
 
238
239
  def system_collection?
@@ -1,4 +1,5 @@
1
1
  # encoding: utf-8
2
+
2
3
  module Mongoid
3
4
 
4
5
  # This module contains behaviour for all Mongoid scoping - named scopes,
@@ -45,7 +46,7 @@ module Mongoid
45
46
  # include Mongoid::Document
46
47
  # field :active, type: Boolean
47
48
  #
48
- # scope :active, where(active: true)
49
+ # scope :active, -> { where(active: true) }
49
50
  # end
50
51
  # Band.scopes
51
52
  #
@@ -127,14 +128,14 @@ module Mongoid
127
128
  # field :active, type: Boolean
128
129
  # field :count, type: Integer
129
130
  #
130
- # scope :active, where(active: true)
131
+ # scope :active, -> { where(active: true) }
131
132
  # scope :at_least, ->(count){ where(:count.gt => count) }
132
133
  # end
133
134
  #
134
135
  # @param [ Symbol ] name The name of the scope.
135
- # @param [ Proc, Criteria ] conditions The conditions of the scope.
136
+ # @param [ Proc ] conditions The conditions of the scope.
136
137
  #
137
- # @raise [ Errors::InvalidScope ] If the scope is not a proc or criteria.
138
+ # @raise [ Errors::InvalidScope ] If the scope is not a proc.
138
139
  # @raise [ Errors::ScopeOverwrite ] If the scope name already exists.
139
140
  #
140
141
  # @since 1.0.0
@@ -321,15 +322,15 @@ module Mongoid
321
322
  #
322
323
  # @since 3.0.0
323
324
  def define_scope_method(name)
324
- singleton_class.class_eval <<-SCOPE, __FILE__, __LINE__ + 1
325
- def #{name}(*args)
326
- scoping = _declared_scopes[:#{name}]
325
+ singleton_class.class_eval do
326
+ define_method name do |*args|
327
+ scoping = _declared_scopes[name]
327
328
  scope, extension = scoping[:scope][*args], scoping[:extension]
328
329
  criteria = with_default_scope.merge(scope || queryable)
329
330
  criteria.extend(extension)
330
331
  criteria
331
332
  end
332
- SCOPE
333
+ end
333
334
  end
334
335
 
335
336
  # Process the default scope value. If one already exists, we merge the
@@ -158,10 +158,14 @@ module Mongoid
158
158
 
159
159
  def method_missing(name, *args, &block)
160
160
  set_persistence_options(@target, @options)
161
- @target.send(name, *args, &block)
161
+ ret = @target.send(name, *args, &block)
162
+ if Mongoid::Criteria == ret.class
163
+ ret.with @options
164
+ end
165
+ ret
162
166
  ensure
163
167
  set_persistence_options(@target, nil)
164
- end
168
+ end
165
169
 
166
170
  def send(symbol, *args)
167
171
  __send__(symbol, *args)
@@ -19,9 +19,8 @@ module Mongoid
19
19
  #
20
20
  # @since 3.0.0
21
21
  def validate(klass, options)
22
- if !valid_keys?(options) || !valid_parent?(klass)
23
- raise Errors::InvalidStorageOptions.new(klass, options)
24
- end
22
+ valid_keys?(options) or raise Errors::InvalidStorageOptions.new(klass, options)
23
+ valid_parent?(klass) or raise Errors::InvalidStorageParent.new(klass)
25
24
  end
26
25
 
27
26
  private
@@ -16,7 +16,7 @@ module Mongoid
16
16
  def create_indexes(models = ::Mongoid.models)
17
17
  models.each do |model|
18
18
  next if model.index_specifications.empty?
19
- unless model.embedded?
19
+ if !model.embedded? || model.cyclic?
20
20
  model.create_indexes
21
21
  logger.info("MONGOID: Created indexes on #{model}:")
22
22
  model.index_specifications.each do |spec|
@@ -14,7 +14,6 @@ module Mongoid
14
14
  # provide: validates_associated and validates_uniqueness_of.
15
15
  module Validatable
16
16
  extend ActiveSupport::Concern
17
- include ActiveModel::Validations
18
17
 
19
18
  included do
20
19
  extend Macros
@@ -32,7 +32,7 @@ module Mongoid
32
32
  begin
33
33
  document.begin_validate
34
34
  valid = Array.wrap(value).collect do |doc|
35
- if doc.nil?
35
+ if doc.nil? || doc.flagged_for_destroy?
36
36
  true
37
37
  else
38
38
  doc.validated? ? true : doc.valid?
@@ -267,7 +267,10 @@ module Mongoid
267
267
  # @since 2.4.10
268
268
  def validate_root(document, attribute, value)
269
269
  klass = document.class
270
- klass = klass.superclass while !klass.validators.include?(self)
270
+
271
+ while klass.superclass.respond_to?(:validators) && klass.superclass.validators.include?(self)
272
+ klass = klass.superclass
273
+ end
271
274
  criteria = create_criteria(klass, document, attribute, value)
272
275
  criteria = criteria.merge(options[:conditions].call) if options[:conditions]
273
276
 
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- VERSION = "4.0.0.beta1"
3
+ VERSION = "4.0.0.beta2"
4
4
  end
@@ -0,0 +1,9 @@
1
+ class Draft
2
+ include Mongoid::Document
3
+
4
+ field :text
5
+
6
+ recursively_embeds_one
7
+
8
+ index text: 1
9
+ end
@@ -21,6 +21,41 @@ describe Mongoid::Attributes do
21
21
  Person.create(title: "sir")
22
22
  end
23
23
 
24
+ context "when the attribute is localized" do
25
+
26
+ before do
27
+ person.update_attribute(:desc, "test")
28
+ end
29
+
30
+ context "when the context includes" do
31
+
32
+ context "when the attribute exists" do
33
+
34
+ let(:from_db) do
35
+ Person.only(:desc).first
36
+ end
37
+
38
+ it "does not raise an error" do
39
+ expect(from_db.desc).to eq("test")
40
+ end
41
+ end
42
+ end
43
+
44
+ context "when the context excludes" do
45
+
46
+ context "when the attribute exists" do
47
+
48
+ let(:from_db) do
49
+ Person.without(:pets).first
50
+ end
51
+
52
+ it "does not raise an error" do
53
+ expect(from_db.desc).to eq("test")
54
+ end
55
+ end
56
+ end
57
+ end
58
+
24
59
  context "when excluding with only" do
25
60
 
26
61
  let(:from_db) do
@@ -1142,6 +1142,83 @@ describe Mongoid::Changeable do
1142
1142
  end
1143
1143
  end
1144
1144
 
1145
+ describe "#reset_attribute_to_default!" do
1146
+
1147
+ context "when a default is defined" do
1148
+
1149
+ context "when the document is new" do
1150
+
1151
+ let(:person) do
1152
+ Person.new(pets: true)
1153
+ end
1154
+
1155
+ before do
1156
+ person.reset_pets_to_default!
1157
+ end
1158
+
1159
+ it "resets to the default value" do
1160
+ expect(person.pets).to eq(false)
1161
+ end
1162
+ end
1163
+
1164
+ context "when the document is persisted" do
1165
+
1166
+ let(:person) do
1167
+ Person.create(pets: true)
1168
+ end
1169
+
1170
+ before do
1171
+ person.reset_pets_to_default!
1172
+ end
1173
+
1174
+ it "resets to the default value" do
1175
+ expect(person.pets).to eq(false)
1176
+ end
1177
+
1178
+ it "flags the document dirty" do
1179
+ expect(person).to be_pets_changed
1180
+ end
1181
+ end
1182
+ end
1183
+
1184
+ context "when a default is not defined" do
1185
+
1186
+ context "when the document is new" do
1187
+
1188
+ let(:person) do
1189
+ Person.new(title: "test")
1190
+ end
1191
+
1192
+ before do
1193
+ person.reset_title_to_default!
1194
+ end
1195
+
1196
+ it "resets to nil" do
1197
+ expect(person.title).to be_nil
1198
+ end
1199
+ end
1200
+
1201
+ context "when the document is persisted" do
1202
+
1203
+ let(:person) do
1204
+ Person.create(title: "test")
1205
+ end
1206
+
1207
+ before do
1208
+ person.reset_title_to_default!
1209
+ end
1210
+
1211
+ it "resets to nil" do
1212
+ expect(person.title).to be_nil
1213
+ end
1214
+
1215
+ it "flags the document dirty" do
1216
+ expect(person).to be_title_changed
1217
+ end
1218
+ end
1219
+ end
1220
+ end
1221
+
1145
1222
  context "when fields have been defined pre-dirty inclusion" do
1146
1223
 
1147
1224
  let(:document) do
@@ -104,7 +104,7 @@ describe Mongoid::Contextual::Atomic do
104
104
  end
105
105
 
106
106
  it "does not error on non initialized fields" do
107
- expect(smiths.reload.likes).to be_nil
107
+ expect(smiths.reload.likes).to eq(0)
108
108
  end
109
109
  end
110
110
 
@@ -119,7 +119,7 @@ describe Mongoid::Contextual::Atomic do
119
119
  end
120
120
 
121
121
  it "does not error on non initialized fields" do
122
- expect(smiths.reload.likes).to be_nil
122
+ expect(smiths.reload.likes).to eq(13)
123
123
  end
124
124
  end
125
125
 
@@ -134,7 +134,7 @@ describe Mongoid::Contextual::Atomic do
134
134
  end
135
135
 
136
136
  it "does not error on non initialized fields" do
137
- expect(smiths.reload.likes).to be_nil
137
+ expect(smiths.reload.likes).to eq(10)
138
138
  end
139
139
  end
140
140
  end
@@ -3042,7 +3042,7 @@ describe Mongoid::Criteria do
3042
3042
  end
3043
3043
 
3044
3044
  it "executes the criteria while properly giving the hint to Mongo" do
3045
- expect { criteria.to_ary }.to raise_error(Moped::Errors::QueryFailure, %r{failed with error 10113: "bad hint"})
3045
+ expect { criteria.to_ary }.to raise_error(Moped::Errors::QueryFailure)
3046
3046
  end
3047
3047
  end
3048
3048
 
@@ -3057,7 +3057,7 @@ describe Mongoid::Criteria do
3057
3057
  end
3058
3058
 
3059
3059
  it "executes the criteria while properly giving the hint to Mongo" do
3060
- expect { criteria.to_ary }.to raise_error(Moped::Errors::QueryFailure, %r{failed with error 10113: "bad hint"})
3060
+ expect { criteria.to_ary }.to raise_error(Moped::Errors::QueryFailure)
3061
3061
  end
3062
3062
  end
3063
3063
 
@@ -9,7 +9,7 @@ describe Mongoid::QueryCache do
9
9
 
10
10
  context "when querying for a single document" do
11
11
 
12
- [ :first, :one ].each do |method|
12
+ [ :first, :one, :last ].each do |method|
13
13
 
14
14
  before do
15
15
  Band.all.send(method)
@@ -74,6 +74,40 @@ describe Mongoid::QueryCache do
74
74
  Band.all.to_a
75
75
  end
76
76
  end
77
+
78
+ context "when querying only the first" do
79
+ let(:game) { Game.create!(name: "2048") }
80
+
81
+ before do
82
+ game.ratings.where(:value.gt => 5).asc(:id).all.to_a
83
+ end
84
+
85
+ it "queries again" do
86
+ expect_query(1) do
87
+ game.ratings.where(:value.gt => 5).asc(:id).first
88
+ end
89
+ end
90
+ end
91
+
92
+ context "limiting the result" do
93
+ it "queries again" do
94
+ expect_query(1) do
95
+ Band.limit(2).all.to_a
96
+ end
97
+ end
98
+ end
99
+
100
+ context "specifying a different skip value" do
101
+ before do
102
+ Band.limit(2).skip(1).all.to_a
103
+ end
104
+
105
+ it "queries again" do
106
+ expect_query(1) do
107
+ Band.limit(2).skip(3).all.to_a
108
+ end
109
+ end
110
+ end
77
111
  end
78
112
 
79
113
  context "with different selector" do
@@ -34,6 +34,21 @@ describe Mongoid::Sessions::Options do
34
34
  expect(klass.persistence_options).to eq(options)
35
35
  end
36
36
  end
37
+
38
+ context "when returning a criteria" do
39
+
40
+ let(:criteria) do
41
+ klass.all
42
+ end
43
+
44
+ it "sets the options into the criteria object" do
45
+ expect(criteria.persistence_options).to eq(options)
46
+ end
47
+
48
+ it "doesnt set the options on class level" do
49
+ expect(Band.new.persistence_options).to be_nil
50
+ end
51
+ end
37
52
  end
38
53
  end
39
54
 
@@ -650,7 +650,7 @@ describe Mongoid::Sessions do
650
650
  it "raises an error" do
651
651
  expect {
652
652
  klass.store_in(database: :artists)
653
- }.to raise_error(Mongoid::Errors::InvalidStorageOptions)
653
+ }.to raise_error(Mongoid::Errors::InvalidStorageParent)
654
654
  end
655
655
  end
656
656
 
@@ -13,7 +13,7 @@ describe "Mongoid::Tasks::Database" do
13
13
  end
14
14
 
15
15
  let(:models) do
16
- [ User, Account, Address ]
16
+ [ User, Account, Address, Draft ]
17
17
  end
18
18
 
19
19
  describe ".create_indexes" do
@@ -69,6 +69,18 @@ describe "Mongoid::Tasks::Database" do
69
69
  indexes
70
70
  end
71
71
  end
72
+
73
+ context "when index is defined on self-embedded (cyclic) model" do
74
+
75
+ let(:klass) do
76
+ Draft
77
+ end
78
+
79
+ it "creates the indexes for the models" do
80
+ expect(klass).to receive(:create_indexes).once
81
+ indexes
82
+ end
83
+ end
72
84
  end
73
85
 
74
86
  describe ".undefined_indexes" do
@@ -56,6 +56,29 @@ describe Mongoid::Validatable::AssociatedValidator do
56
56
  expect(description).to_not be_valid
57
57
  end
58
58
  end
59
+
60
+ context "when the documents are flagged for destroy" do
61
+
62
+ let(:user) do
63
+ User.new(name: "test")
64
+ end
65
+
66
+ let(:description) do
67
+ Description.new
68
+ end
69
+
70
+ before do
71
+ description.flagged_for_destroy = true
72
+ user.descriptions << description
73
+ end
74
+
75
+ it "does not run validation on them" do
76
+ expect(description).to receive(:valid?).never
77
+ expect(user).to be_valid
78
+ end
79
+
80
+ end
81
+
59
82
  end
60
83
  end
61
84
 
@@ -85,7 +108,7 @@ describe Mongoid::Validatable::AssociatedValidator do
85
108
  context "when the association is valid" do
86
109
 
87
110
  let(:associated) do
88
- double(valid?: true)
111
+ double(valid?: true, flagged_for_destroy?: false)
89
112
  end
90
113
 
91
114
  before do
@@ -101,7 +124,7 @@ describe Mongoid::Validatable::AssociatedValidator do
101
124
  context "when the association is invalid" do
102
125
 
103
126
  let(:associated) do
104
- double(valid?: false)
127
+ double(valid?: false, flagged_for_destroy?: false)
105
128
  end
106
129
 
107
130
  before do
@@ -135,7 +158,7 @@ describe Mongoid::Validatable::AssociatedValidator do
135
158
  context "when the association has invalid documents" do
136
159
 
137
160
  let(:associated) do
138
- double(valid?: false)
161
+ double(valid?: false, flagged_for_destroy?: false)
139
162
  end
140
163
 
141
164
  before do
@@ -151,7 +174,7 @@ describe Mongoid::Validatable::AssociatedValidator do
151
174
  context "when the assocation has all valid documents" do
152
175
 
153
176
  let(:associated) do
154
- double(valid?: true)
177
+ double(valid?: true, flagged_for_destroy?: false)
155
178
  end
156
179
 
157
180
  before do
@@ -2344,18 +2344,19 @@ describe Mongoid::Validatable::UniquenessValidator do
2344
2344
  end
2345
2345
 
2346
2346
  context "when validation works with inheritance" do
2347
+ class EuropeanActor < Actor
2348
+ validates_uniqueness_of :name
2349
+ end
2347
2350
 
2348
- before do
2349
- Actor.validates_uniqueness_of :name
2350
- Actor.create!(name: "Johnny Depp")
2351
+ class SpanishActor < EuropeanActor
2351
2352
  end
2352
2353
 
2353
- after do
2354
- Actor.reset_callbacks(:validate)
2354
+ before do
2355
+ EuropeanActor.create!(name: "Antonio Banderas")
2355
2356
  end
2356
2357
 
2357
2358
  let!(:subclass_document_with_duplicated_name) do
2358
- Actress.new(name: "Johnny Depp")
2359
+ SpanishActor.new(name: "Antonio Banderas")
2359
2360
  end
2360
2361
 
2361
2362
  it "should be invalid" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0.beta1
4
+ version: 4.0.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-01 00:00:00.000000000 Z
11
+ date: 2014-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -142,6 +142,7 @@ files:
142
142
  - lib/mongoid/errors/invalid_scope.rb
143
143
  - lib/mongoid/errors/invalid_set_polymorphic_relation.rb
144
144
  - lib/mongoid/errors/invalid_storage_options.rb
145
+ - lib/mongoid/errors/invalid_storage_parent.rb
145
146
  - lib/mongoid/errors/invalid_time.rb
146
147
  - lib/mongoid/errors/invalid_value.rb
147
148
  - lib/mongoid/errors/inverse_not_found.rb
@@ -398,6 +399,7 @@ files:
398
399
  - spec/app/models/doctor.rb
399
400
  - spec/app/models/dog.rb
400
401
  - spec/app/models/dokument.rb
402
+ - spec/app/models/draft.rb
401
403
  - spec/app/models/dragon.rb
402
404
  - spec/app/models/driver.rb
403
405
  - spec/app/models/drug.rb
@@ -778,7 +780,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
778
780
  version: 1.3.6
779
781
  requirements: []
780
782
  rubyforge_project: mongoid
781
- rubygems_version: 2.2.1
783
+ rubygems_version: 2.2.2
782
784
  signing_key:
783
785
  specification_version: 4
784
786
  summary: Elegant Persistance in Ruby for MongoDB.
@@ -839,6 +841,7 @@ test_files:
839
841
  - spec/app/models/doctor.rb
840
842
  - spec/app/models/dog.rb
841
843
  - spec/app/models/dokument.rb
844
+ - spec/app/models/draft.rb
842
845
  - spec/app/models/dragon.rb
843
846
  - spec/app/models/driver.rb
844
847
  - spec/app/models/drug.rb