mongoid 4.0.1 → 4.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +30 -0
- data/README.md +1 -1
- data/lib/mongoid/contextual/memory.rb +5 -5
- data/lib/mongoid/contextual/mongo.rb +10 -0
- data/lib/mongoid/document.rb +2 -2
- data/lib/mongoid/extensions.rb +13 -0
- data/lib/mongoid/findable.rb +27 -5
- data/lib/mongoid/persistable/creatable.rb +2 -1
- data/lib/mongoid/persistable/settable.rb +1 -1
- data/lib/mongoid/persistable/updatable.rb +2 -1
- data/lib/mongoid/relations/many.rb +21 -0
- data/lib/mongoid/sessions/options.rb +7 -2
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +32 -0
- data/spec/app/models/contextable_item.rb +5 -0
- data/spec/mongoid/attributes/nested_spec.rb +139 -144
- data/spec/mongoid/contextual/memory_spec.rb +7 -2
- data/spec/mongoid/criteria/modifiable_spec.rb +5 -8
- data/spec/mongoid/document_spec.rb +7 -3
- data/spec/mongoid/extensions_spec.rb +14 -0
- data/spec/mongoid/findable_spec.rb +97 -9
- data/spec/mongoid/persistable/creatable_spec.rb +41 -0
- data/spec/mongoid/persistable/savable_spec.rb +37 -0
- data/spec/mongoid/persistable/settable_spec.rb +23 -0
- data/spec/mongoid/relations/embedded/many_spec.rb +74 -0
- data/spec/mongoid/relations/referenced/many_spec.rb +130 -0
- data/spec/mongoid/relations/referenced/many_to_many_spec.rb +49 -0
- data/spec/mongoid/sessions/options_spec.rb +2 -1
- data/spec/mongoid/sessions_spec.rb +30 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5340f453822e7cbecd2710795296c23db607d92
|
4
|
+
data.tar.gz: 0adcfd1449d91980fb45bb60aa336e661b1482f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b1733f089831c0b58049cb486a8c13687f16a0845d2d9eb2acfb8c5aa0c99de3bbe3a81d6ab0bae0be9c8e4493c77864d64626b341565b5be8f7d695fb93acc
|
7
|
+
data.tar.gz: 1c7a4d343e583880595366b2f4afcb58a8d0f763c8a3bc5f04cea4134d1fb947f420d3ce7cb9b070902a0fd50cd03e71c1c4188c7f349a0e591774435b6a9908
|
data/CHANGELOG.md
CHANGED
@@ -3,6 +3,36 @@
|
|
3
3
|
For instructions on upgrading to newer versions, visit
|
4
4
|
[mongoid.org](http://mongoid.org/en/mongoid/docs/upgrading.html).
|
5
5
|
|
6
|
+
## 4.0.3 - Not released
|
7
|
+
|
8
|
+
## 4.0.2
|
9
|
+
|
10
|
+
### New Features
|
11
|
+
|
12
|
+
* \#3931 Add #find_or_create_by! method to many associations. (Tom Beynon)
|
13
|
+
|
14
|
+
* \#3731 Add find_by! method. (Guillermo Iguaran)
|
15
|
+
|
16
|
+
### Resolved Issues
|
17
|
+
|
18
|
+
* \#3722 Use the right database name when combining #store_in and #with. (Arthur Neves)
|
19
|
+
|
20
|
+
* \#3934 Dont apply sort when doing a find_by. (Arthur Neves)
|
21
|
+
|
22
|
+
* \#3935 fix multiple fields sorting on contextual memory. (chamnap)
|
23
|
+
|
24
|
+
* \#3904 BSON::Document#symbolize_keys should return keys as symbols. (Arthur Neves)
|
25
|
+
|
26
|
+
* \#3948 Fix remove_undefined_indexes on rails 4.2, to symbolize right the Document keys. (Adam Wróbel)
|
27
|
+
|
28
|
+
* \#3626 Document#to_key, needs to return a ObjectId as String so we can query back using that id. (Arthur Neves)
|
29
|
+
|
30
|
+
* \#3888 raise UnknownAttributeError when 'set' is called on non existing field and Mongoid::Attributes::Dynamic is not included in model. (Shweta Kale)
|
31
|
+
|
32
|
+
* \#3889 'set' will allow to set value of non existing field when Mongoid::Attributes::Dynamic is included in model. (Shweta Kale)
|
33
|
+
|
34
|
+
* \#3812 Fixed validation context when saving (Yaroslav Zemlyanuhin)
|
35
|
+
|
6
36
|
## 4.0.1
|
7
37
|
|
8
38
|
### Resolved Issues
|
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# Mongoid
|
2
|
-
[![Build Status](https://travis-ci.org/mongoid/mongoid.svg)](https://travis-ci.org/mongoid/mongoid)
|
2
|
+
[![Build Status](https://travis-ci.org/mongoid/mongoid.svg?branch=master)](https://travis-ci.org/mongoid/mongoid)
|
3
3
|
[![Code Climate](https://codeclimate.com/github/mongoid/mongoid.svg)](https://codeclimate.com/github/mongoid/mongoid)
|
4
4
|
[![Coverage Status](https://img.shields.io/coveralls/mongoid/mongoid/master.svg)](https://coveralls.io/r/mongoid/mongoid?branch=master)
|
5
5
|
[![Dependency Status](https://www.versioneye.com/ruby/mongoid/4.0.0/badge.svg)](https://www.versioneye.com/ruby/mongoid/4.0.0)
|
@@ -135,6 +135,7 @@ module Mongoid
|
|
135
135
|
doc
|
136
136
|
end
|
137
137
|
alias :one :first
|
138
|
+
alias :find_first :first
|
138
139
|
|
139
140
|
# Create the new in memory context.
|
140
141
|
#
|
@@ -421,12 +422,11 @@ module Mongoid
|
|
421
422
|
#
|
422
423
|
# @since 3.0.0
|
423
424
|
def in_place_sort(values)
|
424
|
-
|
425
|
-
|
425
|
+
documents.sort! do |a, b|
|
426
|
+
values.map do |field, direction|
|
426
427
|
a_value, b_value = a[field], b[field]
|
427
|
-
|
428
|
-
|
429
|
-
end
|
428
|
+
direction * compare(a_value.__sortable__, b_value.__sortable__)
|
429
|
+
end.find { |value| !value.zero? } || 0
|
430
430
|
end
|
431
431
|
end
|
432
432
|
|
@@ -202,6 +202,16 @@ module Mongoid
|
|
202
202
|
end
|
203
203
|
alias :one :first
|
204
204
|
|
205
|
+
# Return the first result without applying sort
|
206
|
+
#
|
207
|
+
# @api private
|
208
|
+
#
|
209
|
+
# @since 4.0.2
|
210
|
+
def find_first
|
211
|
+
return documents.first if cached? && cache_loaded?
|
212
|
+
with_eager_loading(query.first)
|
213
|
+
end
|
214
|
+
|
205
215
|
# Execute a $geoNear command against the database.
|
206
216
|
#
|
207
217
|
# @example Find documents close to 10, 10.
|
data/lib/mongoid/document.rb
CHANGED
@@ -134,11 +134,11 @@ module Mongoid
|
|
134
134
|
# @example Return the key.
|
135
135
|
# document.to_key
|
136
136
|
#
|
137
|
-
# @return [
|
137
|
+
# @return [ String ] The id of the document or nil if new.
|
138
138
|
#
|
139
139
|
# @since 2.4.0
|
140
140
|
def to_key
|
141
|
-
(persisted? || destroyed?) ? [ id ] : nil
|
141
|
+
(persisted? || destroyed?) ? [ id.to_s ] : nil
|
142
142
|
end
|
143
143
|
|
144
144
|
# Return an array with this +Document+ only in it.
|
data/lib/mongoid/extensions.rb
CHANGED
@@ -12,6 +12,19 @@ class Symbol
|
|
12
12
|
remove_method :size if instance_methods.include? :size # temporal fix for ruby 1.9
|
13
13
|
end
|
14
14
|
|
15
|
+
class BSON::Document
|
16
|
+
# We need to override this as ActiveSupport creates a new Object, instead of a new Hash
|
17
|
+
# see https://github.com/rails/rails/commit/f1bad130d0c9bd77c94e43b696adca56c46a66aa
|
18
|
+
def transform_keys
|
19
|
+
return enum_for(:transform_keys) unless block_given?
|
20
|
+
result = {}
|
21
|
+
each_key do |key|
|
22
|
+
result[yield(key)] = self[key]
|
23
|
+
end
|
24
|
+
result
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
15
28
|
require "mongoid/extensions/array"
|
16
29
|
require "mongoid/extensions/big_decimal"
|
17
30
|
require "mongoid/extensions/boolean"
|
data/lib/mongoid/findable.rb
CHANGED
@@ -90,21 +90,24 @@ module Mongoid
|
|
90
90
|
with_default_scope.find(*args)
|
91
91
|
end
|
92
92
|
|
93
|
-
# Find the first +Document+ given the conditions
|
94
|
-
#
|
93
|
+
# Find the first +Document+ given the conditions.
|
94
|
+
# If a matching Document is not found and
|
95
|
+
# Mongoid.raise_not_found_error is true it raises
|
96
|
+
# Mongoid::Errors::DocumentNotFound, return null nil elsewise.
|
95
97
|
#
|
96
98
|
# @example Find the document by attribute other than id
|
97
99
|
# Person.find_by(:username => "superuser")
|
98
100
|
#
|
99
101
|
# @param [ Hash ] attrs The attributes to check.
|
100
102
|
#
|
101
|
-
# @raise [ Errors::DocumentNotFound ] If no document found
|
103
|
+
# @raise [ Errors::DocumentNotFound ] If no document found
|
104
|
+
# and Mongoid.raise_not_found_error is true.
|
102
105
|
#
|
103
|
-
# @return [ Document ] A matching document.
|
106
|
+
# @return [ Document, nil ] A matching document.
|
104
107
|
#
|
105
108
|
# @since 3.0.0
|
106
109
|
def find_by(attrs = {})
|
107
|
-
result = where(attrs).
|
110
|
+
result = where(attrs).find_first
|
108
111
|
if result.nil? && Mongoid.raise_not_found_error
|
109
112
|
raise(Errors::DocumentNotFound.new(self, attrs))
|
110
113
|
end
|
@@ -112,6 +115,25 @@ module Mongoid
|
|
112
115
|
result
|
113
116
|
end
|
114
117
|
|
118
|
+
# Find the first +Document+ given the conditions, or raises
|
119
|
+
# Mongoid::Errors::DocumentNotFound
|
120
|
+
#
|
121
|
+
# @example Find the document by attribute other than id
|
122
|
+
# Person.find_by(:username => "superuser")
|
123
|
+
#
|
124
|
+
# @param [ Hash ] attrs The attributes to check.
|
125
|
+
#
|
126
|
+
# @raise [ Errors::DocumentNotFound ] If no document found.
|
127
|
+
#
|
128
|
+
# @return [ Document ] A matching document.
|
129
|
+
#
|
130
|
+
def find_by!(attrs = {})
|
131
|
+
result = where(attrs).find_first
|
132
|
+
raise(Errors::DocumentNotFound.new(self, attrs)) unless result
|
133
|
+
yield(result) if result && block_given?
|
134
|
+
result
|
135
|
+
end
|
136
|
+
|
115
137
|
# Find the first +Document+ given the conditions.
|
116
138
|
#
|
117
139
|
# @example Find the first document.
|
@@ -111,7 +111,8 @@ module Mongoid
|
|
111
111
|
#
|
112
112
|
# @since 4.0.0
|
113
113
|
def prepare_insert(options = {})
|
114
|
-
return self if performing_validations?(options) &&
|
114
|
+
return self if performing_validations?(options) &&
|
115
|
+
invalid?(options[:context] || :create)
|
115
116
|
result = run_callbacks(:save) do
|
116
117
|
run_callbacks(:create) do
|
117
118
|
yield(self)
|
@@ -22,7 +22,7 @@ module Mongoid
|
|
22
22
|
def set(setters)
|
23
23
|
prepare_atomic_operation do |ops|
|
24
24
|
process_atomic_operations(setters) do |field, value|
|
25
|
-
|
25
|
+
process_attribute(field.to_s, value)
|
26
26
|
ops[atomic_attribute_name(field)] = attributes[field]
|
27
27
|
end
|
28
28
|
{ "$set" => ops }
|
@@ -110,7 +110,8 @@ module Mongoid
|
|
110
110
|
#
|
111
111
|
# @since 4.0.0
|
112
112
|
def prepare_update(options = {})
|
113
|
-
return false if performing_validations?(options) &&
|
113
|
+
return false if performing_validations?(options) &&
|
114
|
+
invalid?(options[:context] || :update)
|
114
115
|
process_flagged_destroys
|
115
116
|
result = run_callbacks(:save) do
|
116
117
|
run_callbacks(:update) do
|
@@ -98,6 +98,27 @@ module Mongoid
|
|
98
98
|
find_or(:create, attrs, type, &block)
|
99
99
|
end
|
100
100
|
|
101
|
+
# Find the first document given the conditions, or creates a new document
|
102
|
+
# with the conditions that were supplied. This will raise an error if validation fails.
|
103
|
+
#
|
104
|
+
# @example Find or create.
|
105
|
+
# person.posts.find_or_create_by!(:title => "Testing")
|
106
|
+
#
|
107
|
+
# @overload find_or_create_by!(attributes = nil, type = nil)
|
108
|
+
# @param [ Hash ] attributes The attributes to search or create with.
|
109
|
+
# @param [ Class ] type The optional type of document to create.
|
110
|
+
#
|
111
|
+
# @overload find_or_create_by!(attributes = nil, type = nil)
|
112
|
+
# @param [ Hash ] attributes The attributes to search or create with.
|
113
|
+
# @param [ Class ] type The optional type of document to create.
|
114
|
+
#
|
115
|
+
# @raise [ Errors::Validations ] If validation failed.
|
116
|
+
#
|
117
|
+
# @return [ Document ] An existing document or newly created one.
|
118
|
+
def find_or_create_by!(attrs = {}, type = nil, &block)
|
119
|
+
find_or(:create!, attrs, type, &block)
|
120
|
+
end
|
121
|
+
|
101
122
|
# Find the first +Document+ given the conditions, or instantiates a new document
|
102
123
|
# with the conditions that were supplied
|
103
124
|
#
|
@@ -39,8 +39,13 @@ module Mongoid
|
|
39
39
|
|
40
40
|
def mongo_session
|
41
41
|
if persistence_options
|
42
|
-
|
43
|
-
|
42
|
+
if persistence_options[:session]
|
43
|
+
session = Sessions.with_name(persistence_options[:session])
|
44
|
+
else
|
45
|
+
session = Sessions.with_name(self.class.session_name)
|
46
|
+
session.use(self.class.database_name)
|
47
|
+
end
|
48
|
+
session.with(persistence_options)
|
44
49
|
end
|
45
50
|
end
|
46
51
|
|
data/lib/mongoid/version.rb
CHANGED
@@ -27,6 +27,38 @@ development:
|
|
27
27
|
# The time in seconds that Moped should wait before retrying an
|
28
28
|
# operation on failure. (default: 0.25)
|
29
29
|
# retry_interval: 0.25
|
30
|
+
|
31
|
+
# The connection pool size per-node. This should match or exceed the
|
32
|
+
# number of threads for a multi-threaded application. (default: 5)
|
33
|
+
# pool_size: 5
|
34
|
+
|
35
|
+
# The time in seconds that Moped should wait for the pool to provide
|
36
|
+
# an available connection. This number should probably remain at the
|
37
|
+
# default, unless for some reason you absolutely need to limit the
|
38
|
+
# pool_size, as this wait is only used when the pool is saturated.
|
39
|
+
# (default: 0.5)
|
40
|
+
# pool_timeout: 0.5
|
41
|
+
|
42
|
+
# The time in seconds before Moped will timeout connection and node
|
43
|
+
# operations. (default: 5)
|
44
|
+
# timeout: 5
|
45
|
+
|
46
|
+
# The amount of time in seconds between forced refreshes of node
|
47
|
+
# information including the discovery of new peers. (default: 300)
|
48
|
+
# refresh_interval: 300
|
49
|
+
|
50
|
+
# The amount of time in seconds that a node will be flagged as down.
|
51
|
+
# (default: 30)
|
52
|
+
# down_interval: 30
|
53
|
+
|
54
|
+
# Whether connections should use SSL. (default: nil/false)
|
55
|
+
# ssl: false
|
56
|
+
|
57
|
+
# Whether Moped will use the existing seeds/nodes to find other peers.
|
58
|
+
# (default: true)
|
59
|
+
# auto_discover: true
|
60
|
+
|
61
|
+
|
30
62
|
# Configure Mongoid specific options. (optional)
|
31
63
|
options:
|
32
64
|
# Includes the root model name in json serialization. (default: false)
|
@@ -55,7 +55,7 @@ describe Mongoid::Attributes::Nested do
|
|
55
55
|
|
56
56
|
context "when the relation is an embeds one" do
|
57
57
|
|
58
|
-
before
|
58
|
+
before do
|
59
59
|
Person.send(:undef_method, :name_attributes=)
|
60
60
|
Person.accepts_nested_attributes_for :name
|
61
61
|
end
|
@@ -71,7 +71,7 @@ describe Mongoid::Attributes::Nested do
|
|
71
71
|
|
72
72
|
context "when the relation is an embeds many" do
|
73
73
|
|
74
|
-
before
|
74
|
+
before do
|
75
75
|
Person.send(:undef_method, :addresses_attributes=)
|
76
76
|
Person.accepts_nested_attributes_for :addresses
|
77
77
|
end
|
@@ -104,7 +104,7 @@ describe Mongoid::Attributes::Nested do
|
|
104
104
|
|
105
105
|
context "when the relation is an embedded in" do
|
106
106
|
|
107
|
-
before
|
107
|
+
before do
|
108
108
|
Video.accepts_nested_attributes_for :person
|
109
109
|
end
|
110
110
|
|
@@ -119,7 +119,7 @@ describe Mongoid::Attributes::Nested do
|
|
119
119
|
|
120
120
|
context "when the relation is a references one" do
|
121
121
|
|
122
|
-
before
|
122
|
+
before do
|
123
123
|
Person.send(:undef_method, :game_attributes=)
|
124
124
|
Person.accepts_nested_attributes_for :game
|
125
125
|
end
|
@@ -135,7 +135,7 @@ describe Mongoid::Attributes::Nested do
|
|
135
135
|
|
136
136
|
context "when the relation is a references many" do
|
137
137
|
|
138
|
-
before
|
138
|
+
before do
|
139
139
|
Person.send(:undef_method, :posts_attributes=)
|
140
140
|
Person.accepts_nested_attributes_for :posts
|
141
141
|
end
|
@@ -151,7 +151,7 @@ describe Mongoid::Attributes::Nested do
|
|
151
151
|
|
152
152
|
context "when the relation is a references and referenced in many" do
|
153
153
|
|
154
|
-
before
|
154
|
+
before do
|
155
155
|
Person.send(:undef_method, :preferences_attributes=)
|
156
156
|
Person.accepts_nested_attributes_for :preferences
|
157
157
|
end
|
@@ -167,7 +167,7 @@ describe Mongoid::Attributes::Nested do
|
|
167
167
|
|
168
168
|
context "when the relation is a referenced in" do
|
169
169
|
|
170
|
-
before
|
170
|
+
before do
|
171
171
|
Post.accepts_nested_attributes_for :person
|
172
172
|
end
|
173
173
|
|
@@ -237,13 +237,13 @@ describe Mongoid::Attributes::Nested do
|
|
237
237
|
|
238
238
|
context "when a reject proc is specified" do
|
239
239
|
|
240
|
-
before
|
240
|
+
before do
|
241
241
|
Person.send(:undef_method, :name_attributes=)
|
242
242
|
Person.accepts_nested_attributes_for \
|
243
243
|
:name, reject_if: ->(attrs){ attrs[:first_name].blank? }
|
244
244
|
end
|
245
245
|
|
246
|
-
after
|
246
|
+
after do
|
247
247
|
Person.send(:undef_method, :name_attributes=)
|
248
248
|
Person.accepts_nested_attributes_for :name
|
249
249
|
end
|
@@ -275,13 +275,13 @@ describe Mongoid::Attributes::Nested do
|
|
275
275
|
|
276
276
|
context "when the relation is not autobuilding" do
|
277
277
|
|
278
|
-
before
|
278
|
+
before do
|
279
279
|
Person.send(:undef_method, :name_attributes=)
|
280
280
|
Person.accepts_nested_attributes_for \
|
281
281
|
:name, reject_if: :all_blank
|
282
282
|
end
|
283
283
|
|
284
|
-
after
|
284
|
+
after do
|
285
285
|
Person.send(:undef_method, :name_attributes=)
|
286
286
|
Person.accepts_nested_attributes_for :name
|
287
287
|
end
|
@@ -311,11 +311,11 @@ describe Mongoid::Attributes::Nested do
|
|
311
311
|
|
312
312
|
context "when the relation is autobuilding" do
|
313
313
|
|
314
|
-
before
|
314
|
+
before do
|
315
315
|
Product.accepts_nested_attributes_for :seo, reject_if: :all_blank
|
316
316
|
end
|
317
317
|
|
318
|
-
after
|
318
|
+
after do
|
319
319
|
Product.send(:undef_method, :seo_attributes=)
|
320
320
|
end
|
321
321
|
|
@@ -349,12 +349,12 @@ describe Mongoid::Attributes::Nested do
|
|
349
349
|
|
350
350
|
context "when allow_destroy is true" do
|
351
351
|
|
352
|
-
before
|
352
|
+
before do
|
353
353
|
Person.send(:undef_method, :name_attributes=)
|
354
354
|
Person.accepts_nested_attributes_for :name, allow_destroy: true
|
355
355
|
end
|
356
356
|
|
357
|
-
after
|
357
|
+
after do
|
358
358
|
Person.send(:undef_method, :name_attributes=)
|
359
359
|
Person.accepts_nested_attributes_for :name
|
360
360
|
end
|
@@ -384,12 +384,12 @@ describe Mongoid::Attributes::Nested do
|
|
384
384
|
|
385
385
|
context "when allow_destroy is false" do
|
386
386
|
|
387
|
-
before
|
387
|
+
before do
|
388
388
|
Person.send(:undef_method, :name_attributes=)
|
389
389
|
Person.accepts_nested_attributes_for :name, allow_destroy: false
|
390
390
|
end
|
391
391
|
|
392
|
-
after
|
392
|
+
after do
|
393
393
|
Person.send(:undef_method, :name_attributes=)
|
394
394
|
Person.accepts_nested_attributes_for :name
|
395
395
|
end
|
@@ -433,12 +433,12 @@ describe Mongoid::Attributes::Nested do
|
|
433
433
|
|
434
434
|
context "when allow_destroy is true" do
|
435
435
|
|
436
|
-
before
|
436
|
+
before do
|
437
437
|
Person.send(:undef_method, :name_attributes=)
|
438
438
|
Person.accepts_nested_attributes_for :name, allow_destroy: true
|
439
439
|
end
|
440
440
|
|
441
|
-
after
|
441
|
+
after do
|
442
442
|
Person.send(:undef_method, :name_attributes=)
|
443
443
|
Person.accepts_nested_attributes_for :name
|
444
444
|
end
|
@@ -455,12 +455,12 @@ describe Mongoid::Attributes::Nested do
|
|
455
455
|
|
456
456
|
context "when allow_destroy is false" do
|
457
457
|
|
458
|
-
before
|
458
|
+
before do
|
459
459
|
Person.send(:undef_method, :name_attributes=)
|
460
460
|
Person.accepts_nested_attributes_for :name, allow_destroy: false
|
461
461
|
end
|
462
462
|
|
463
|
-
after
|
463
|
+
after do
|
464
464
|
Person.send(:undef_method, :name_attributes=)
|
465
465
|
Person.accepts_nested_attributes_for :name
|
466
466
|
end
|
@@ -518,12 +518,12 @@ describe Mongoid::Attributes::Nested do
|
|
518
518
|
|
519
519
|
context "when allow_destroy is true" do
|
520
520
|
|
521
|
-
before
|
521
|
+
before do
|
522
522
|
Person.send(:undef_method, :name_attributes=)
|
523
523
|
Person.accepts_nested_attributes_for :name, allow_destroy: true
|
524
524
|
end
|
525
525
|
|
526
|
-
after
|
526
|
+
after do
|
527
527
|
Person.send(:undef_method, :name_attributes=)
|
528
528
|
Person.accepts_nested_attributes_for :name
|
529
529
|
end
|
@@ -546,11 +546,11 @@ describe Mongoid::Attributes::Nested do
|
|
546
546
|
|
547
547
|
context "when the document has destroy callbacks" do
|
548
548
|
|
549
|
-
before
|
549
|
+
before do
|
550
550
|
PetOwner.accepts_nested_attributes_for :pet, allow_destroy: true
|
551
551
|
end
|
552
552
|
|
553
|
-
after
|
553
|
+
after do
|
554
554
|
PetOwner.send(:undef_method, :pet_attributes=)
|
555
555
|
end
|
556
556
|
|
@@ -596,12 +596,12 @@ describe Mongoid::Attributes::Nested do
|
|
596
596
|
|
597
597
|
context "when allow destroy is false" do
|
598
598
|
|
599
|
-
before
|
599
|
+
before do
|
600
600
|
Person.send(:undef_method, :name_attributes=)
|
601
601
|
Person.accepts_nested_attributes_for :name, allow_destroy: false
|
602
602
|
end
|
603
603
|
|
604
|
-
after
|
604
|
+
after do
|
605
605
|
Person.send(:undef_method, :name_attributes=)
|
606
606
|
Person.accepts_nested_attributes_for :name
|
607
607
|
end
|
@@ -621,7 +621,7 @@ describe Mongoid::Attributes::Nested do
|
|
621
621
|
|
622
622
|
context "when update only is true" do
|
623
623
|
|
624
|
-
before
|
624
|
+
before do
|
625
625
|
Person.send(:undef_method, :name_attributes=)
|
626
626
|
Person.accepts_nested_attributes_for \
|
627
627
|
:name,
|
@@ -629,7 +629,7 @@ describe Mongoid::Attributes::Nested do
|
|
629
629
|
allow_destroy: true
|
630
630
|
end
|
631
631
|
|
632
|
-
after
|
632
|
+
after do
|
633
633
|
Person.send(:undef_method, :name_attributes=)
|
634
634
|
Person.accepts_nested_attributes_for :name
|
635
635
|
end
|
@@ -693,11 +693,11 @@ describe Mongoid::Attributes::Nested do
|
|
693
693
|
|
694
694
|
context "when the nested document is invalid" do
|
695
695
|
|
696
|
-
before
|
696
|
+
before do
|
697
697
|
Person.validates_associated(:pet)
|
698
698
|
end
|
699
699
|
|
700
|
-
after
|
700
|
+
after do
|
701
701
|
Person.reset_callbacks(:validate)
|
702
702
|
end
|
703
703
|
|
@@ -755,12 +755,12 @@ describe Mongoid::Attributes::Nested do
|
|
755
755
|
|
756
756
|
context "when allow_destroy is true" do
|
757
757
|
|
758
|
-
before
|
758
|
+
before do
|
759
759
|
Animal.send(:undef_method, :person_attributes=)
|
760
760
|
Animal.accepts_nested_attributes_for :person, allow_destroy: true
|
761
761
|
end
|
762
762
|
|
763
|
-
after
|
763
|
+
after do
|
764
764
|
Animal.send(:undef_method, :person_attributes=)
|
765
765
|
Animal.accepts_nested_attributes_for :person
|
766
766
|
end
|
@@ -776,12 +776,12 @@ describe Mongoid::Attributes::Nested do
|
|
776
776
|
|
777
777
|
context "when allow_destroy is false" do
|
778
778
|
|
779
|
-
before
|
779
|
+
before do
|
780
780
|
Animal.send(:undef_method, :person_attributes=)
|
781
781
|
Animal.accepts_nested_attributes_for :person, allow_destroy: false
|
782
782
|
end
|
783
783
|
|
784
|
-
after
|
784
|
+
after do
|
785
785
|
Animal.send(:undef_method, :person_attributes=)
|
786
786
|
Animal.accepts_nested_attributes_for :person
|
787
787
|
end
|
@@ -825,12 +825,12 @@ describe Mongoid::Attributes::Nested do
|
|
825
825
|
|
826
826
|
context "when allow destroy is true" do
|
827
827
|
|
828
|
-
before
|
828
|
+
before do
|
829
829
|
Animal.send(:undef_method, :person_attributes=)
|
830
830
|
Animal.accepts_nested_attributes_for :person, allow_destroy: true
|
831
831
|
end
|
832
832
|
|
833
|
-
after
|
833
|
+
after do
|
834
834
|
Animal.send(:undef_method, :person_attributes=)
|
835
835
|
Animal.accepts_nested_attributes_for :person
|
836
836
|
end
|
@@ -868,12 +868,12 @@ describe Mongoid::Attributes::Nested do
|
|
868
868
|
|
869
869
|
context "when allow destroy is false" do
|
870
870
|
|
871
|
-
before
|
871
|
+
before do
|
872
872
|
Animal.send(:undef_method, :person_attributes=)
|
873
873
|
Animal.accepts_nested_attributes_for :person, allow_destroy: false
|
874
874
|
end
|
875
875
|
|
876
|
-
after
|
876
|
+
after do
|
877
877
|
Animal.send(:undef_method, :person_attributes=)
|
878
878
|
Animal.accepts_nested_attributes_for :person
|
879
879
|
end
|
@@ -942,11 +942,11 @@ describe Mongoid::Attributes::Nested do
|
|
942
942
|
|
943
943
|
context "when the nested document is invalid" do
|
944
944
|
|
945
|
-
before
|
945
|
+
before do
|
946
946
|
Person.validates_format_of :ssn, without: /\$\$\$/
|
947
947
|
end
|
948
948
|
|
949
|
-
after
|
949
|
+
after do
|
950
950
|
Person.reset_callbacks(:validate)
|
951
951
|
end
|
952
952
|
|
@@ -1041,12 +1041,12 @@ describe Mongoid::Attributes::Nested do
|
|
1041
1041
|
|
1042
1042
|
context "when a limit is specified" do
|
1043
1043
|
|
1044
|
-
before
|
1044
|
+
before do
|
1045
1045
|
Person.send(:undef_method, :addresses_attributes=)
|
1046
1046
|
Person.accepts_nested_attributes_for :addresses, limit: 2
|
1047
1047
|
end
|
1048
1048
|
|
1049
|
-
after
|
1049
|
+
after do
|
1050
1050
|
Person.send(:undef_method, :addresses_attributes=)
|
1051
1051
|
Person.accepts_nested_attributes_for :addresses
|
1052
1052
|
end
|
@@ -1106,11 +1106,11 @@ describe Mongoid::Attributes::Nested do
|
|
1106
1106
|
|
1107
1107
|
context "when cascading callbacks" do
|
1108
1108
|
|
1109
|
-
before
|
1109
|
+
before do
|
1110
1110
|
Band.accepts_nested_attributes_for :records
|
1111
1111
|
end
|
1112
1112
|
|
1113
|
-
after
|
1113
|
+
after do
|
1114
1114
|
Band.send(:undef_method, :records_attributes=)
|
1115
1115
|
end
|
1116
1116
|
|
@@ -1314,18 +1314,13 @@ describe Mongoid::Attributes::Nested do
|
|
1314
1314
|
|
1315
1315
|
context "when the parent validation failed" do
|
1316
1316
|
|
1317
|
-
|
1318
|
-
|
1319
|
-
|
1320
|
-
end
|
1321
|
-
|
1322
|
-
after(:all) do
|
1323
|
-
Band.send(:undef_method, :records_attributes=)
|
1324
|
-
Band.reset_callbacks(:validate)
|
1317
|
+
class BandWithAllowDestroyedRecords < Band
|
1318
|
+
validates_presence_of :name
|
1319
|
+
accepts_nested_attributes_for :records, :allow_destroy => true
|
1325
1320
|
end
|
1326
1321
|
|
1327
1322
|
let!(:band) do
|
1328
|
-
|
1323
|
+
BandWithAllowDestroyedRecords.create(name: "Depeche Mode")
|
1329
1324
|
end
|
1330
1325
|
|
1331
1326
|
let!(:record) do
|
@@ -1358,11 +1353,11 @@ describe Mongoid::Attributes::Nested do
|
|
1358
1353
|
|
1359
1354
|
context "when the child accesses the parent after destroy" do
|
1360
1355
|
|
1361
|
-
before
|
1356
|
+
before do
|
1362
1357
|
Band.accepts_nested_attributes_for :records, :allow_destroy => true
|
1363
1358
|
end
|
1364
1359
|
|
1365
|
-
after
|
1360
|
+
after do
|
1366
1361
|
Band.send(:undef_method, :records_attributes=)
|
1367
1362
|
end
|
1368
1363
|
|
@@ -1391,11 +1386,11 @@ describe Mongoid::Attributes::Nested do
|
|
1391
1386
|
|
1392
1387
|
context "when the child has defaults" do
|
1393
1388
|
|
1394
|
-
before
|
1389
|
+
before do
|
1395
1390
|
Person.accepts_nested_attributes_for :appointments, allow_destroy: true
|
1396
1391
|
end
|
1397
1392
|
|
1398
|
-
after
|
1393
|
+
after do
|
1399
1394
|
Person.send(:undef_method, :appointments_attributes=)
|
1400
1395
|
end
|
1401
1396
|
|
@@ -1470,12 +1465,12 @@ describe Mongoid::Attributes::Nested do
|
|
1470
1465
|
|
1471
1466
|
context "when the child is not paranoid" do
|
1472
1467
|
|
1473
|
-
before
|
1468
|
+
before do
|
1474
1469
|
Person.send(:undef_method, :addresses_attributes=)
|
1475
1470
|
Person.accepts_nested_attributes_for :addresses, allow_destroy: true
|
1476
1471
|
end
|
1477
1472
|
|
1478
|
-
after
|
1473
|
+
after do
|
1479
1474
|
Person.send(:undef_method, :addresses_attributes=)
|
1480
1475
|
Person.accepts_nested_attributes_for :addresses
|
1481
1476
|
end
|
@@ -1675,12 +1670,12 @@ describe Mongoid::Attributes::Nested do
|
|
1675
1670
|
|
1676
1671
|
context "when allow_destroy is false" do
|
1677
1672
|
|
1678
|
-
before
|
1673
|
+
before do
|
1679
1674
|
Person.send(:undef_method, :addresses_attributes=)
|
1680
1675
|
Person.accepts_nested_attributes_for :addresses, allow_destroy: false
|
1681
1676
|
end
|
1682
1677
|
|
1683
|
-
after
|
1678
|
+
after do
|
1684
1679
|
Person.send(:undef_method, :addresses_attributes=)
|
1685
1680
|
Person.accepts_nested_attributes_for :addresses
|
1686
1681
|
end
|
@@ -1737,7 +1732,7 @@ describe Mongoid::Attributes::Nested do
|
|
1737
1732
|
|
1738
1733
|
context "when allow_destroy is undefined" do
|
1739
1734
|
|
1740
|
-
before
|
1735
|
+
before do
|
1741
1736
|
Person.send(:undef_method, :addresses_attributes=)
|
1742
1737
|
Person.accepts_nested_attributes_for :addresses
|
1743
1738
|
end
|
@@ -1833,13 +1828,13 @@ describe Mongoid::Attributes::Nested do
|
|
1833
1828
|
|
1834
1829
|
context "when a reject block is supplied" do
|
1835
1830
|
|
1836
|
-
before
|
1831
|
+
before do
|
1837
1832
|
Person.send(:undef_method, :addresses_attributes=)
|
1838
1833
|
Person.accepts_nested_attributes_for \
|
1839
1834
|
:addresses, reject_if: ->(attrs){ attrs["street"].blank? }
|
1840
1835
|
end
|
1841
1836
|
|
1842
|
-
after
|
1837
|
+
after do
|
1843
1838
|
Person.send(:undef_method, :addresses_attributes=)
|
1844
1839
|
Person.accepts_nested_attributes_for :addresses
|
1845
1840
|
end
|
@@ -1875,13 +1870,13 @@ describe Mongoid::Attributes::Nested do
|
|
1875
1870
|
|
1876
1871
|
context "when :reject_if => :all_blank is supplied" do
|
1877
1872
|
|
1878
|
-
before
|
1873
|
+
before do
|
1879
1874
|
Person.send(:undef_method, :addresses_attributes=)
|
1880
1875
|
Person.accepts_nested_attributes_for \
|
1881
1876
|
:addresses, reject_if: :all_blank
|
1882
1877
|
end
|
1883
1878
|
|
1884
|
-
after
|
1879
|
+
after do
|
1885
1880
|
Person.send(:undef_method, :addresses_attributes=)
|
1886
1881
|
Person.accepts_nested_attributes_for :addresses
|
1887
1882
|
end
|
@@ -1919,12 +1914,12 @@ describe Mongoid::Attributes::Nested do
|
|
1919
1914
|
|
1920
1915
|
context "when allow_destroy is true" do
|
1921
1916
|
|
1922
|
-
before
|
1917
|
+
before do
|
1923
1918
|
Person.send(:undef_method, :addresses_attributes=)
|
1924
1919
|
Person.accepts_nested_attributes_for :addresses, allow_destroy: true
|
1925
1920
|
end
|
1926
1921
|
|
1927
|
-
after
|
1922
|
+
after do
|
1928
1923
|
Person.send(:undef_method, :addresses_attributes=)
|
1929
1924
|
Person.accepts_nested_attributes_for :addresses
|
1930
1925
|
end
|
@@ -1980,12 +1975,12 @@ describe Mongoid::Attributes::Nested do
|
|
1980
1975
|
|
1981
1976
|
context "when allow destroy is false" do
|
1982
1977
|
|
1983
|
-
before
|
1978
|
+
before do
|
1984
1979
|
Person.send(:undef_method, :addresses_attributes=)
|
1985
1980
|
Person.accepts_nested_attributes_for :addresses, allow_destroy: false
|
1986
1981
|
end
|
1987
1982
|
|
1988
|
-
after
|
1983
|
+
after do
|
1989
1984
|
Person.send(:undef_method, :addresses_attributes=)
|
1990
1985
|
Person.accepts_nested_attributes_for :addresses
|
1991
1986
|
end
|
@@ -2045,7 +2040,7 @@ describe Mongoid::Attributes::Nested do
|
|
2045
2040
|
|
2046
2041
|
context "when allow destroy is not defined" do
|
2047
2042
|
|
2048
|
-
before
|
2043
|
+
before do
|
2049
2044
|
Person.send(:undef_method, :addresses_attributes=)
|
2050
2045
|
Person.accepts_nested_attributes_for :addresses
|
2051
2046
|
end
|
@@ -2106,13 +2101,13 @@ describe Mongoid::Attributes::Nested do
|
|
2106
2101
|
|
2107
2102
|
context "when 'reject_if: :all_blank' and 'allow_destroy: true' are specified" do
|
2108
2103
|
|
2109
|
-
before
|
2104
|
+
before do
|
2110
2105
|
Person.send(:undef_method, :addresses_attributes=)
|
2111
2106
|
Person.accepts_nested_attributes_for \
|
2112
2107
|
:addresses, reject_if: :all_blank, allow_destroy: true
|
2113
2108
|
end
|
2114
2109
|
|
2115
|
-
after
|
2110
|
+
after do
|
2116
2111
|
Person.send(:undef_method, :addresses_attributes=)
|
2117
2112
|
Person.accepts_nested_attributes_for :addresses
|
2118
2113
|
end
|
@@ -2133,11 +2128,11 @@ describe Mongoid::Attributes::Nested do
|
|
2133
2128
|
|
2134
2129
|
context "when the nested document is invalid" do
|
2135
2130
|
|
2136
|
-
before
|
2131
|
+
before do
|
2137
2132
|
Person.validates_associated(:addresses)
|
2138
2133
|
end
|
2139
2134
|
|
2140
|
-
after
|
2135
|
+
after do
|
2141
2136
|
Person.reset_callbacks(:validate)
|
2142
2137
|
end
|
2143
2138
|
|
@@ -2183,13 +2178,13 @@ describe Mongoid::Attributes::Nested do
|
|
2183
2178
|
|
2184
2179
|
context "when a reject proc is specified" do
|
2185
2180
|
|
2186
|
-
before
|
2181
|
+
before do
|
2187
2182
|
Person.send(:undef_method, :game_attributes=)
|
2188
2183
|
Person.accepts_nested_attributes_for \
|
2189
2184
|
:game, reject_if: ->(attrs){ attrs[:name].blank? }
|
2190
2185
|
end
|
2191
2186
|
|
2192
|
-
after
|
2187
|
+
after do
|
2193
2188
|
Person.send(:undef_method, :game_attributes=)
|
2194
2189
|
Person.accepts_nested_attributes_for :game
|
2195
2190
|
end
|
@@ -2219,13 +2214,13 @@ describe Mongoid::Attributes::Nested do
|
|
2219
2214
|
|
2220
2215
|
context "when reject_if => :all_blank is specified" do
|
2221
2216
|
|
2222
|
-
before
|
2217
|
+
before do
|
2223
2218
|
Person.send(:undef_method, :game_attributes=)
|
2224
2219
|
Person.accepts_nested_attributes_for \
|
2225
2220
|
:game, reject_if: :all_blank
|
2226
2221
|
end
|
2227
2222
|
|
2228
|
-
after
|
2223
|
+
after do
|
2229
2224
|
Person.send(:undef_method, :game_attributes=)
|
2230
2225
|
Person.accepts_nested_attributes_for :game
|
2231
2226
|
end
|
@@ -2270,12 +2265,12 @@ describe Mongoid::Attributes::Nested do
|
|
2270
2265
|
|
2271
2266
|
context "when allow_destroy is true" do
|
2272
2267
|
|
2273
|
-
before
|
2268
|
+
before do
|
2274
2269
|
Person.send(:undef_method, :game_attributes=)
|
2275
2270
|
Person.accepts_nested_attributes_for :game, allow_destroy: true
|
2276
2271
|
end
|
2277
2272
|
|
2278
|
-
after
|
2273
|
+
after do
|
2279
2274
|
Person.send(:undef_method, :game_attributes=)
|
2280
2275
|
Person.accepts_nested_attributes_for :game
|
2281
2276
|
end
|
@@ -2291,12 +2286,12 @@ describe Mongoid::Attributes::Nested do
|
|
2291
2286
|
|
2292
2287
|
context "when allow_destroy is false" do
|
2293
2288
|
|
2294
|
-
before
|
2289
|
+
before do
|
2295
2290
|
Person.send(:undef_method, :game_attributes=)
|
2296
2291
|
Person.accepts_nested_attributes_for :game, allow_destroy: false
|
2297
2292
|
end
|
2298
2293
|
|
2299
|
-
after
|
2294
|
+
after do
|
2300
2295
|
Person.send(:undef_method, :game_attributes=)
|
2301
2296
|
Person.accepts_nested_attributes_for :game
|
2302
2297
|
end
|
@@ -2356,12 +2351,12 @@ describe Mongoid::Attributes::Nested do
|
|
2356
2351
|
|
2357
2352
|
context "when allow_destroy is true" do
|
2358
2353
|
|
2359
|
-
before
|
2354
|
+
before do
|
2360
2355
|
Person.send(:undef_method, :game_attributes=)
|
2361
2356
|
Person.accepts_nested_attributes_for :game, allow_destroy: true
|
2362
2357
|
end
|
2363
2358
|
|
2364
|
-
after
|
2359
|
+
after do
|
2365
2360
|
Person.send(:undef_method, :game_attributes=)
|
2366
2361
|
Person.accepts_nested_attributes_for :game
|
2367
2362
|
end
|
@@ -2378,12 +2373,12 @@ describe Mongoid::Attributes::Nested do
|
|
2378
2373
|
|
2379
2374
|
context "when allow_destroy is false" do
|
2380
2375
|
|
2381
|
-
before
|
2376
|
+
before do
|
2382
2377
|
Person.send(:undef_method, :game_attributes=)
|
2383
2378
|
Person.accepts_nested_attributes_for :game, allow_destroy: false
|
2384
2379
|
end
|
2385
2380
|
|
2386
|
-
after
|
2381
|
+
after do
|
2387
2382
|
Person.send(:undef_method, :game_attributes=)
|
2388
2383
|
Person.accepts_nested_attributes_for :game
|
2389
2384
|
end
|
@@ -2441,12 +2436,12 @@ describe Mongoid::Attributes::Nested do
|
|
2441
2436
|
|
2442
2437
|
context "when allow_destroy is true" do
|
2443
2438
|
|
2444
|
-
before
|
2439
|
+
before do
|
2445
2440
|
Person.send(:undef_method, :game_attributes=)
|
2446
2441
|
Person.accepts_nested_attributes_for :game, allow_destroy: true
|
2447
2442
|
end
|
2448
2443
|
|
2449
|
-
after
|
2444
|
+
after do
|
2450
2445
|
Person.send(:undef_method, :game_attributes=)
|
2451
2446
|
Person.accepts_nested_attributes_for :game
|
2452
2447
|
end
|
@@ -2484,12 +2479,12 @@ describe Mongoid::Attributes::Nested do
|
|
2484
2479
|
|
2485
2480
|
context "when allow destroy is false" do
|
2486
2481
|
|
2487
|
-
before
|
2482
|
+
before do
|
2488
2483
|
Person.send(:undef_method, :game_attributes=)
|
2489
2484
|
Person.accepts_nested_attributes_for :game, allow_destroy: false
|
2490
2485
|
end
|
2491
2486
|
|
2492
|
-
after
|
2487
|
+
after do
|
2493
2488
|
Person.send(:undef_method, :game_attributes=)
|
2494
2489
|
Person.accepts_nested_attributes_for :game
|
2495
2490
|
end
|
@@ -2509,7 +2504,7 @@ describe Mongoid::Attributes::Nested do
|
|
2509
2504
|
|
2510
2505
|
context "when update only is true" do
|
2511
2506
|
|
2512
|
-
before
|
2507
|
+
before do
|
2513
2508
|
Person.send(:undef_method, :game_attributes=)
|
2514
2509
|
Person.accepts_nested_attributes_for \
|
2515
2510
|
:game,
|
@@ -2517,7 +2512,7 @@ describe Mongoid::Attributes::Nested do
|
|
2517
2512
|
allow_destroy: true
|
2518
2513
|
end
|
2519
2514
|
|
2520
|
-
after
|
2515
|
+
after do
|
2521
2516
|
Person.send(:undef_method, :game_attributes=)
|
2522
2517
|
Person.accepts_nested_attributes_for :game
|
2523
2518
|
end
|
@@ -2564,11 +2559,11 @@ describe Mongoid::Attributes::Nested do
|
|
2564
2559
|
|
2565
2560
|
context "when the nested document is invalid" do
|
2566
2561
|
|
2567
|
-
before
|
2562
|
+
before do
|
2568
2563
|
Person.validates_associated(:game)
|
2569
2564
|
end
|
2570
2565
|
|
2571
|
-
after
|
2566
|
+
after do
|
2572
2567
|
Person.reset_callbacks(:validate)
|
2573
2568
|
end
|
2574
2569
|
|
@@ -2626,12 +2621,12 @@ describe Mongoid::Attributes::Nested do
|
|
2626
2621
|
|
2627
2622
|
context "when allow_destroy is true" do
|
2628
2623
|
|
2629
|
-
before
|
2624
|
+
before do
|
2630
2625
|
Game.send(:undef_method, :person_attributes=)
|
2631
2626
|
Game.accepts_nested_attributes_for :person, allow_destroy: true
|
2632
2627
|
end
|
2633
2628
|
|
2634
|
-
after
|
2629
|
+
after do
|
2635
2630
|
Game.send(:undef_method, :person_attributes=)
|
2636
2631
|
Game.accepts_nested_attributes_for :person
|
2637
2632
|
end
|
@@ -2647,12 +2642,12 @@ describe Mongoid::Attributes::Nested do
|
|
2647
2642
|
|
2648
2643
|
context "when allow_destroy is false" do
|
2649
2644
|
|
2650
|
-
before
|
2645
|
+
before do
|
2651
2646
|
Game.send(:undef_method, :person_attributes=)
|
2652
2647
|
Game.accepts_nested_attributes_for :person, allow_destroy: false
|
2653
2648
|
end
|
2654
2649
|
|
2655
|
-
after
|
2650
|
+
after do
|
2656
2651
|
Game.send(:undef_method, :person_attributes=)
|
2657
2652
|
Game.accepts_nested_attributes_for :person
|
2658
2653
|
end
|
@@ -2696,12 +2691,12 @@ describe Mongoid::Attributes::Nested do
|
|
2696
2691
|
|
2697
2692
|
context "when allow destroy is true" do
|
2698
2693
|
|
2699
|
-
before
|
2694
|
+
before do
|
2700
2695
|
Game.send(:undef_method, :person_attributes=)
|
2701
2696
|
Game.accepts_nested_attributes_for :person, allow_destroy: true
|
2702
2697
|
end
|
2703
2698
|
|
2704
|
-
after
|
2699
|
+
after do
|
2705
2700
|
Game.send(:undef_method, :person_attributes=)
|
2706
2701
|
Game.accepts_nested_attributes_for :person
|
2707
2702
|
end
|
@@ -2739,12 +2734,12 @@ describe Mongoid::Attributes::Nested do
|
|
2739
2734
|
|
2740
2735
|
context "when allow destroy is false" do
|
2741
2736
|
|
2742
|
-
before
|
2737
|
+
before do
|
2743
2738
|
Game.send(:undef_method, :person_attributes=)
|
2744
2739
|
Game.accepts_nested_attributes_for :person, allow_destroy: false
|
2745
2740
|
end
|
2746
2741
|
|
2747
|
-
after
|
2742
|
+
after do
|
2748
2743
|
Game.send(:undef_method, :person_attributes=)
|
2749
2744
|
Game.accepts_nested_attributes_for :person
|
2750
2745
|
end
|
@@ -2813,11 +2808,11 @@ describe Mongoid::Attributes::Nested do
|
|
2813
2808
|
|
2814
2809
|
context "when the nested document is invalid" do
|
2815
2810
|
|
2816
|
-
before
|
2811
|
+
before do
|
2817
2812
|
Person.validates_format_of :ssn, without: /\$\$\$/
|
2818
2813
|
end
|
2819
2814
|
|
2820
|
-
after
|
2815
|
+
after do
|
2821
2816
|
Person.reset_callbacks(:validate)
|
2822
2817
|
end
|
2823
2818
|
|
@@ -2866,12 +2861,12 @@ describe Mongoid::Attributes::Nested do
|
|
2866
2861
|
|
2867
2862
|
context "when a limit is specified" do
|
2868
2863
|
|
2869
|
-
before
|
2864
|
+
before do
|
2870
2865
|
Person.send(:undef_method, :posts_attributes=)
|
2871
2866
|
Person.accepts_nested_attributes_for :posts, limit: 2
|
2872
2867
|
end
|
2873
2868
|
|
2874
|
-
after
|
2869
|
+
after do
|
2875
2870
|
Person.send(:undef_method, :posts_attributes=)
|
2876
2871
|
Person.accepts_nested_attributes_for :posts
|
2877
2872
|
end
|
@@ -3141,12 +3136,12 @@ describe Mongoid::Attributes::Nested do
|
|
3141
3136
|
|
3142
3137
|
context "when allow_destroy is undefined" do
|
3143
3138
|
|
3144
|
-
before
|
3139
|
+
before do
|
3145
3140
|
Person.send(:undef_method, :posts_attributes=)
|
3146
3141
|
Person.accepts_nested_attributes_for :posts
|
3147
3142
|
end
|
3148
3143
|
|
3149
|
-
after
|
3144
|
+
after do
|
3150
3145
|
Person.send(:undef_method, :posts_attributes=)
|
3151
3146
|
Person.accepts_nested_attributes_for :posts
|
3152
3147
|
end
|
@@ -3294,13 +3289,13 @@ describe Mongoid::Attributes::Nested do
|
|
3294
3289
|
|
3295
3290
|
context "when a reject block is supplied" do
|
3296
3291
|
|
3297
|
-
before
|
3292
|
+
before do
|
3298
3293
|
Person.send(:undef_method, :posts_attributes=)
|
3299
3294
|
Person.accepts_nested_attributes_for \
|
3300
3295
|
:posts, reject_if: ->(attrs){ attrs["title"].blank? }
|
3301
3296
|
end
|
3302
3297
|
|
3303
|
-
after
|
3298
|
+
after do
|
3304
3299
|
Person.send(:undef_method, :posts_attributes=)
|
3305
3300
|
Person.accepts_nested_attributes_for :posts
|
3306
3301
|
end
|
@@ -3336,13 +3331,13 @@ describe Mongoid::Attributes::Nested do
|
|
3336
3331
|
|
3337
3332
|
context "when :reject_if => :all_blank is supplied" do
|
3338
3333
|
|
3339
|
-
before
|
3334
|
+
before do
|
3340
3335
|
Person.send(:undef_method, :posts_attributes=)
|
3341
3336
|
Person.accepts_nested_attributes_for \
|
3342
3337
|
:posts, reject_if: :all_blank
|
3343
3338
|
end
|
3344
3339
|
|
3345
|
-
after
|
3340
|
+
after do
|
3346
3341
|
Person.send(:undef_method, :posts_attributes=)
|
3347
3342
|
Person.accepts_nested_attributes_for :posts
|
3348
3343
|
end
|
@@ -3380,12 +3375,12 @@ describe Mongoid::Attributes::Nested do
|
|
3380
3375
|
|
3381
3376
|
context "when allow_destroy is true" do
|
3382
3377
|
|
3383
|
-
before
|
3378
|
+
before do
|
3384
3379
|
Person.send(:undef_method, :posts_attributes=)
|
3385
3380
|
Person.accepts_nested_attributes_for :posts, allow_destroy: true
|
3386
3381
|
end
|
3387
3382
|
|
3388
|
-
after
|
3383
|
+
after do
|
3389
3384
|
Person.send(:undef_method, :posts_attributes=)
|
3390
3385
|
Person.accepts_nested_attributes_for :posts
|
3391
3386
|
end
|
@@ -3441,12 +3436,12 @@ describe Mongoid::Attributes::Nested do
|
|
3441
3436
|
|
3442
3437
|
context "when allow destroy is false" do
|
3443
3438
|
|
3444
|
-
before
|
3439
|
+
before do
|
3445
3440
|
Person.send(:undef_method, :posts_attributes=)
|
3446
3441
|
Person.accepts_nested_attributes_for :posts, allow_destroy: false
|
3447
3442
|
end
|
3448
3443
|
|
3449
|
-
after
|
3444
|
+
after do
|
3450
3445
|
Person.send(:undef_method, :posts_attributes=)
|
3451
3446
|
Person.accepts_nested_attributes_for :posts
|
3452
3447
|
end
|
@@ -3506,7 +3501,7 @@ describe Mongoid::Attributes::Nested do
|
|
3506
3501
|
|
3507
3502
|
context "when allow destroy is not defined" do
|
3508
3503
|
|
3509
|
-
before
|
3504
|
+
before do
|
3510
3505
|
Person.send(:undef_method, :posts_attributes=)
|
3511
3506
|
Person.accepts_nested_attributes_for :posts
|
3512
3507
|
end
|
@@ -3568,11 +3563,11 @@ describe Mongoid::Attributes::Nested do
|
|
3568
3563
|
|
3569
3564
|
context "when the nested document is invalid" do
|
3570
3565
|
|
3571
|
-
before
|
3566
|
+
before do
|
3572
3567
|
Person.validates_associated(:posts)
|
3573
3568
|
end
|
3574
3569
|
|
3575
|
-
after
|
3570
|
+
after do
|
3576
3571
|
Person.reset_callbacks(:validate)
|
3577
3572
|
end
|
3578
3573
|
|
@@ -3626,12 +3621,12 @@ describe Mongoid::Attributes::Nested do
|
|
3626
3621
|
|
3627
3622
|
context "when a limit is specified" do
|
3628
3623
|
|
3629
|
-
before
|
3624
|
+
before do
|
3630
3625
|
Person.send(:undef_method, :preferences_attributes=)
|
3631
3626
|
Person.accepts_nested_attributes_for :preferences, limit: 2
|
3632
3627
|
end
|
3633
3628
|
|
3634
|
-
after
|
3629
|
+
after do
|
3635
3630
|
Person.send(:undef_method, :preferences_attributes=)
|
3636
3631
|
Person.accepts_nested_attributes_for :preferences
|
3637
3632
|
end
|
@@ -3727,12 +3722,12 @@ describe Mongoid::Attributes::Nested do
|
|
3727
3722
|
|
3728
3723
|
context "when allow_destroy is true" do
|
3729
3724
|
|
3730
|
-
before
|
3725
|
+
before do
|
3731
3726
|
Person.send(:undef_method, :preferences_attributes=)
|
3732
3727
|
Person.accepts_nested_attributes_for :preferences, allow_destroy: true
|
3733
3728
|
end
|
3734
3729
|
|
3735
|
-
after
|
3730
|
+
after do
|
3736
3731
|
Person.send(:undef_method, :preferences_attributes=)
|
3737
3732
|
Person.accepts_nested_attributes_for :preferences
|
3738
3733
|
end
|
@@ -3790,12 +3785,12 @@ describe Mongoid::Attributes::Nested do
|
|
3790
3785
|
|
3791
3786
|
context "when allow_destroy is false" do
|
3792
3787
|
|
3793
|
-
before
|
3788
|
+
before do
|
3794
3789
|
Person.send(:undef_method, :preferences_attributes=)
|
3795
3790
|
Person.accepts_nested_attributes_for :preferences, allow_destroy: false
|
3796
3791
|
end
|
3797
3792
|
|
3798
|
-
after
|
3793
|
+
after do
|
3799
3794
|
Person.send(:undef_method, :preferences_attributes=)
|
3800
3795
|
Person.accepts_nested_attributes_for :preferences
|
3801
3796
|
end
|
@@ -3858,7 +3853,7 @@ describe Mongoid::Attributes::Nested do
|
|
3858
3853
|
|
3859
3854
|
context "when allow_destroy is undefined" do
|
3860
3855
|
|
3861
|
-
before
|
3856
|
+
before do
|
3862
3857
|
Person.send(:undef_method, :preferences_attributes=)
|
3863
3858
|
Person.accepts_nested_attributes_for :preferences
|
3864
3859
|
end
|
@@ -3960,13 +3955,13 @@ describe Mongoid::Attributes::Nested do
|
|
3960
3955
|
|
3961
3956
|
context "when a reject block is supplied" do
|
3962
3957
|
|
3963
|
-
before
|
3958
|
+
before do
|
3964
3959
|
Person.send(:undef_method, :preferences_attributes=)
|
3965
3960
|
Person.accepts_nested_attributes_for \
|
3966
3961
|
:preferences, reject_if: ->(attrs){ attrs["name"].blank? }
|
3967
3962
|
end
|
3968
3963
|
|
3969
|
-
after
|
3964
|
+
after do
|
3970
3965
|
Person.send(:undef_method, :preferences_attributes=)
|
3971
3966
|
Person.accepts_nested_attributes_for :preferences
|
3972
3967
|
end
|
@@ -4002,13 +3997,13 @@ describe Mongoid::Attributes::Nested do
|
|
4002
3997
|
|
4003
3998
|
context "when :reject_if => :all_blank is supplied" do
|
4004
3999
|
|
4005
|
-
before
|
4000
|
+
before do
|
4006
4001
|
Person.send(:undef_method, :preferences_attributes=)
|
4007
4002
|
Person.accepts_nested_attributes_for \
|
4008
4003
|
:preferences, reject_if: :all_blank
|
4009
4004
|
end
|
4010
4005
|
|
4011
|
-
after
|
4006
|
+
after do
|
4012
4007
|
Person.send(:undef_method, :preferences_attributes=)
|
4013
4008
|
Person.accepts_nested_attributes_for :preferences
|
4014
4009
|
end
|
@@ -4046,12 +4041,12 @@ describe Mongoid::Attributes::Nested do
|
|
4046
4041
|
|
4047
4042
|
context "when allow_destroy is true" do
|
4048
4043
|
|
4049
|
-
before
|
4044
|
+
before do
|
4050
4045
|
Person.send(:undef_method, :preferences_attributes=)
|
4051
4046
|
Person.accepts_nested_attributes_for :preferences, allow_destroy: true
|
4052
4047
|
end
|
4053
4048
|
|
4054
|
-
after
|
4049
|
+
after do
|
4055
4050
|
Person.send(:undef_method, :preferences_attributes=)
|
4056
4051
|
Person.accepts_nested_attributes_for :preferences
|
4057
4052
|
end
|
@@ -4107,12 +4102,12 @@ describe Mongoid::Attributes::Nested do
|
|
4107
4102
|
|
4108
4103
|
context "when allow destroy is false" do
|
4109
4104
|
|
4110
|
-
before
|
4105
|
+
before do
|
4111
4106
|
Person.send(:undef_method, :preferences_attributes=)
|
4112
4107
|
Person.accepts_nested_attributes_for :preferences, allow_destroy: false
|
4113
4108
|
end
|
4114
4109
|
|
4115
|
-
after
|
4110
|
+
after do
|
4116
4111
|
Person.send(:undef_method, :preferences_attributes=)
|
4117
4112
|
Person.accepts_nested_attributes_for :preferences
|
4118
4113
|
end
|
@@ -4172,7 +4167,7 @@ describe Mongoid::Attributes::Nested do
|
|
4172
4167
|
|
4173
4168
|
context "when allow destroy is not defined" do
|
4174
4169
|
|
4175
|
-
before
|
4170
|
+
before do
|
4176
4171
|
Person.send(:undef_method, :preferences_attributes=)
|
4177
4172
|
Person.accepts_nested_attributes_for :preferences
|
4178
4173
|
end
|
@@ -4234,11 +4229,11 @@ describe Mongoid::Attributes::Nested do
|
|
4234
4229
|
|
4235
4230
|
context "when the nested document is invalid" do
|
4236
4231
|
|
4237
|
-
before
|
4232
|
+
before do
|
4238
4233
|
Person.validates_associated(:preferences)
|
4239
4234
|
end
|
4240
4235
|
|
4241
|
-
after
|
4236
|
+
after do
|
4242
4237
|
Person.reset_callbacks(:validate)
|
4243
4238
|
end
|
4244
4239
|
|
@@ -4259,7 +4254,7 @@ describe Mongoid::Attributes::Nested do
|
|
4259
4254
|
|
4260
4255
|
describe "#update_attributes" do
|
4261
4256
|
|
4262
|
-
before
|
4257
|
+
before do
|
4263
4258
|
Person.send(:undef_method, :addresses_attributes=)
|
4264
4259
|
Person.accepts_nested_attributes_for :addresses
|
4265
4260
|
end
|
@@ -4348,7 +4343,7 @@ describe Mongoid::Attributes::Nested do
|
|
4348
4343
|
|
4349
4344
|
context "when nesting multiple levels and parent is timestamped" do
|
4350
4345
|
|
4351
|
-
before
|
4346
|
+
before do
|
4352
4347
|
class Address
|
4353
4348
|
after_save do
|
4354
4349
|
addressable.touch
|
@@ -4356,7 +4351,7 @@ describe Mongoid::Attributes::Nested do
|
|
4356
4351
|
end
|
4357
4352
|
end
|
4358
4353
|
|
4359
|
-
after
|
4354
|
+
after do
|
4360
4355
|
Address.reset_callbacks(:save)
|
4361
4356
|
end
|
4362
4357
|
|
@@ -4452,12 +4447,12 @@ describe Mongoid::Attributes::Nested do
|
|
4452
4447
|
|
4453
4448
|
context "when cascading callbacks" do
|
4454
4449
|
|
4455
|
-
before
|
4450
|
+
before do
|
4456
4451
|
Band.accepts_nested_attributes_for :records
|
4457
4452
|
Record.accepts_nested_attributes_for :tracks, allow_destroy: true
|
4458
4453
|
end
|
4459
4454
|
|
4460
|
-
after
|
4455
|
+
after do
|
4461
4456
|
Band.send(:undef_method, :records_attributes=)
|
4462
4457
|
Record.send(:undef_method, :tracks_attributes=)
|
4463
4458
|
end
|