mongoid 7.2.0.rc1 → 7.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/Rakefile +45 -10
- data/lib/config/locales/en.yml +2 -2
- data/lib/mongoid/association/accessors.rb +1 -1
- data/lib/mongoid/association/constrainable.rb +1 -1
- data/lib/mongoid/association/depending.rb +4 -4
- data/lib/mongoid/association/embedded/batchable.rb +1 -1
- data/lib/mongoid/association/embedded/embedded_in.rb +1 -1
- data/lib/mongoid/association/embedded/embeds_many/proxy.rb +10 -3
- data/lib/mongoid/association/nested/many.rb +1 -1
- data/lib/mongoid/association/nested/one.rb +4 -2
- data/lib/mongoid/association/proxy.rb +6 -1
- data/lib/mongoid/association/referenced/auto_save.rb +2 -2
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +493 -495
- data/lib/mongoid/association/referenced/has_many/proxy.rb +2 -2
- data/lib/mongoid/association/referenced/has_one/buildable.rb +8 -0
- data/lib/mongoid/association/referenced/has_one/nested_builder.rb +2 -2
- data/lib/mongoid/association/referenced/has_one/proxy.rb +6 -1
- data/lib/mongoid/attributes.rb +32 -14
- data/lib/mongoid/attributes/projector.rb +120 -0
- data/lib/mongoid/cacheable.rb +2 -2
- data/lib/mongoid/clients.rb +1 -1
- data/lib/mongoid/clients/factory.rb +22 -8
- data/lib/mongoid/config.rb +19 -2
- data/lib/mongoid/contextual/aggregable/mongo.rb +10 -8
- data/lib/mongoid/copyable.rb +6 -2
- data/lib/mongoid/criteria.rb +4 -5
- data/lib/mongoid/criteria/findable.rb +1 -1
- data/lib/mongoid/criteria/queryable/expandable.rb +0 -24
- data/lib/mongoid/criteria/queryable/extensions.rb +0 -4
- data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -1
- data/lib/mongoid/criteria/queryable/mergeable.rb +46 -20
- data/lib/mongoid/criteria/queryable/selectable.rb +8 -8
- data/lib/mongoid/criteria/queryable/selector.rb +0 -4
- data/lib/mongoid/document.rb +4 -17
- data/lib/mongoid/errors/delete_restriction.rb +8 -9
- data/lib/mongoid/evolvable.rb +1 -1
- data/lib/mongoid/extensions.rb +1 -0
- data/lib/mongoid/extensions/boolean.rb +1 -2
- data/lib/mongoid/extensions/false_class.rb +1 -1
- data/lib/mongoid/extensions/hash.rb +2 -2
- data/lib/mongoid/extensions/true_class.rb +1 -1
- data/lib/mongoid/fields.rb +46 -5
- data/lib/mongoid/inspectable.rb +1 -1
- data/lib/mongoid/interceptable.rb +3 -1
- data/lib/mongoid/matcher.rb +26 -43
- data/lib/mongoid/matcher/bits.rb +41 -0
- data/lib/mongoid/matcher/bits_all_clear.rb +20 -0
- data/lib/mongoid/matcher/bits_all_set.rb +20 -0
- data/lib/mongoid/matcher/bits_any_clear.rb +20 -0
- data/lib/mongoid/matcher/bits_any_set.rb +20 -0
- data/lib/mongoid/matcher/elem_match.rb +2 -1
- data/lib/mongoid/matcher/expression.rb +9 -14
- data/lib/mongoid/matcher/field_expression.rb +4 -5
- data/lib/mongoid/matcher/field_operator.rb +13 -11
- data/lib/mongoid/matcher/mod.rb +17 -0
- data/lib/mongoid/matcher/type.rb +99 -0
- data/lib/mongoid/persistable/deletable.rb +1 -2
- data/lib/mongoid/persistable/destroyable.rb +8 -2
- data/lib/mongoid/persistable/updatable.rb +27 -2
- data/lib/mongoid/query_cache.rb +35 -29
- data/lib/mongoid/reloadable.rb +5 -0
- data/lib/mongoid/selectable.rb +5 -7
- data/lib/mongoid/shardable.rb +21 -5
- data/lib/mongoid/stringified_symbol.rb +53 -0
- data/lib/mongoid/touchable.rb +23 -4
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/config_generator.rb +8 -1
- data/spec/README.md +19 -4
- data/spec/integration/app_spec.rb +175 -88
- data/spec/integration/associations/embeds_many_spec.rb +68 -0
- data/spec/integration/associations/embeds_one_spec.rb +24 -0
- data/spec/integration/associations/has_many_spec.rb +60 -0
- data/spec/integration/associations/has_one_spec.rb +108 -0
- data/spec/integration/callbacks_models.rb +49 -0
- data/spec/integration/callbacks_spec.rb +216 -0
- data/spec/integration/criteria/date_field_spec.rb +1 -1
- data/spec/integration/document_spec.rb +30 -0
- data/spec/integration/matcher_operator_data/bits_all_clear.yml +159 -0
- data/spec/integration/matcher_operator_data/bits_all_set.yml +159 -0
- data/spec/integration/matcher_operator_data/bits_any_clear.yml +159 -0
- data/spec/integration/matcher_operator_data/bits_any_set.yml +159 -0
- data/spec/integration/matcher_operator_data/comment.yml +22 -0
- data/spec/integration/matcher_operator_data/elem_match.yml +46 -0
- data/spec/integration/matcher_operator_data/gt_types.yml +63 -0
- data/spec/integration/matcher_operator_data/gte_types.yml +15 -0
- data/spec/integration/matcher_operator_data/implicit_traversal.yml +96 -0
- data/spec/integration/matcher_operator_data/in.yml +16 -0
- data/spec/integration/matcher_operator_data/lt_types.yml +15 -0
- data/spec/integration/matcher_operator_data/lte_types.yml +15 -0
- data/spec/integration/matcher_operator_data/mod.yml +55 -0
- data/spec/integration/matcher_operator_data/ne_types.yml +15 -0
- data/spec/integration/matcher_operator_data/type.yml +70 -0
- data/spec/integration/matcher_operator_data/type_array.yml +16 -0
- data/spec/integration/matcher_operator_data/type_binary.yml +18 -0
- data/spec/integration/matcher_operator_data/type_boolean.yml +39 -0
- data/spec/integration/matcher_operator_data/type_code.yml +26 -0
- data/spec/integration/matcher_operator_data/type_code_with_scope.yml +26 -0
- data/spec/integration/matcher_operator_data/type_date.yml +39 -0
- data/spec/integration/matcher_operator_data/type_db_pointer.yml +19 -0
- data/spec/integration/matcher_operator_data/type_decimal.yml +40 -0
- data/spec/integration/matcher_operator_data/type_double.yml +15 -0
- data/spec/integration/matcher_operator_data/type_int32.yml +33 -0
- data/spec/integration/matcher_operator_data/type_int64.yml +33 -0
- data/spec/integration/matcher_operator_data/type_max_key.yml +17 -0
- data/spec/integration/matcher_operator_data/type_min_key.yml +17 -0
- data/spec/integration/matcher_operator_data/type_null.yml +23 -0
- data/spec/integration/matcher_operator_data/type_object.yml +23 -0
- data/spec/integration/matcher_operator_data/type_object_id.yml +25 -0
- data/spec/integration/matcher_operator_data/type_regex.yml +44 -0
- data/spec/integration/matcher_operator_data/type_string.yml +15 -0
- data/spec/integration/matcher_operator_data/type_symbol.yml +32 -0
- data/spec/integration/matcher_operator_data/type_timestamp.yml +25 -0
- data/spec/integration/matcher_operator_data/type_undefined.yml +17 -0
- data/spec/integration/stringified_symbol_field_spec.rb +190 -0
- data/spec/lite_spec_helper.rb +9 -7
- data/spec/mongoid/association/depending_spec.rb +391 -352
- data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +50 -0
- data/spec/mongoid/association/nested/one_spec.rb +18 -14
- data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +25 -8
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +1 -1
- data/spec/mongoid/association/referenced/has_many/binding_spec.rb +1 -1
- data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +1 -1
- data/spec/mongoid/association/referenced/has_many_models.rb +12 -0
- data/spec/mongoid/association/referenced/has_one_models.rb +20 -0
- data/spec/mongoid/association/referenced/has_one_spec.rb +1 -1
- data/spec/mongoid/atomic/paths_spec.rb +105 -12
- data/spec/mongoid/attributes/projector_data/embedded.yml +105 -0
- data/spec/mongoid/attributes/projector_data/fields.yml +93 -0
- data/spec/mongoid/attributes/projector_spec.rb +41 -0
- data/spec/mongoid/attributes_spec.rb +333 -0
- data/spec/mongoid/clients/factory_spec.rb +48 -0
- data/spec/mongoid/config_spec.rb +32 -0
- data/spec/mongoid/contextual/atomic_spec.rb +17 -4
- data/spec/mongoid/contextual/mongo_spec.rb +2 -2
- data/spec/mongoid/copyable_spec.rb +44 -17
- data/spec/mongoid/copyable_spec_models.rb +14 -0
- data/spec/mongoid/criteria/modifiable_spec.rb +1 -1
- data/spec/mongoid/criteria/queryable/expandable_spec.rb +0 -73
- data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +1 -1
- data/spec/mongoid/criteria/queryable/mergeable_spec.rb +105 -7
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +265 -24
- data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +39 -0
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -565
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +590 -0
- data/spec/mongoid/criteria_projection_spec.rb +411 -0
- data/spec/mongoid/criteria_spec.rb +0 -275
- data/spec/mongoid/document_fields_spec.rb +26 -0
- data/spec/mongoid/document_spec.rb +13 -13
- data/spec/mongoid/equality_spec.rb +0 -1
- data/spec/mongoid/errors/delete_restriction_spec.rb +1 -1
- data/spec/mongoid/extensions/false_class_spec.rb +1 -1
- data/spec/mongoid/extensions/string_spec.rb +5 -5
- data/spec/mongoid/extensions/stringified_symbol_spec.rb +85 -0
- data/spec/mongoid/extensions/true_class_spec.rb +1 -1
- data/spec/mongoid/fields/localized_spec.rb +4 -4
- data/spec/mongoid/fields_spec.rb +4 -4
- data/spec/mongoid/inspectable_spec.rb +12 -4
- data/spec/mongoid/matcher/extract_attribute_data/numeric_keys.yml +104 -0
- data/spec/mongoid/matcher/extract_attribute_data/traversal.yml +68 -88
- data/spec/mongoid/matcher/extract_attribute_spec.rb +3 -13
- data/spec/mongoid/persistable/deletable_spec.rb +175 -1
- data/spec/mongoid/persistable/destroyable_spec.rb +191 -3
- data/spec/mongoid/persistable/savable_spec.rb +3 -5
- data/spec/mongoid/persistable/settable_spec.rb +30 -0
- data/spec/mongoid/persistable/upsertable_spec.rb +1 -1
- data/spec/mongoid/query_cache_middleware_spec.rb +8 -0
- data/spec/mongoid/reloadable_spec.rb +18 -1
- data/spec/mongoid/shardable_spec.rb +44 -0
- data/spec/mongoid/touchable_spec.rb +104 -16
- data/spec/mongoid/touchable_spec_models.rb +52 -0
- data/spec/mongoid/validatable_spec.rb +1 -1
- data/spec/shared/LICENSE +20 -0
- data/spec/shared/bin/get-mongodb-download-url +17 -0
- data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
- data/spec/shared/lib/mrss/cluster_config.rb +221 -0
- data/spec/shared/lib/mrss/constraints.rb +354 -0
- data/spec/shared/lib/mrss/docker_runner.rb +265 -0
- data/spec/shared/lib/mrss/lite_constraints.rb +191 -0
- data/spec/shared/lib/mrss/server_version_registry.rb +115 -0
- data/spec/shared/lib/mrss/spec_organizer.rb +162 -0
- data/spec/shared/lib/mrss/utils.rb +15 -0
- data/spec/shared/share/Dockerfile.erb +231 -0
- data/spec/shared/shlib/distro.sh +73 -0
- data/spec/shared/shlib/server.sh +290 -0
- data/spec/shared/shlib/set_env.sh +128 -0
- data/spec/spec_helper.rb +7 -1
- data/spec/support/client_registry.rb +9 -0
- data/spec/support/constraints.rb +0 -226
- data/spec/support/models/bolt.rb +8 -0
- data/spec/support/models/customer.rb +11 -0
- data/spec/support/models/customer_address.rb +12 -0
- data/spec/support/models/dictionary.rb +6 -0
- data/spec/support/models/hole.rb +13 -0
- data/spec/support/models/mop.rb +9 -0
- data/spec/support/models/nut.rb +8 -0
- data/spec/support/models/order.rb +11 -0
- data/spec/support/models/person.rb +8 -0
- data/spec/support/models/sealer.rb +8 -0
- data/spec/support/models/series.rb +1 -0
- data/spec/support/models/shirt.rb +12 -0
- data/spec/support/models/spacer.rb +8 -0
- data/spec/support/models/threadlocker.rb +8 -0
- data/spec/support/models/washer.rb +8 -0
- data/spec/support/models/wiki_page.rb +1 -0
- data/spec/support/spec_config.rb +8 -0
- metadata +655 -507
- metadata.gz.sig +0 -0
- data/spec/support/child_process_helper.rb +0 -79
- data/spec/support/cluster_config.rb +0 -158
- data/spec/support/lite_constraints.rb +0 -22
- data/spec/support/spec_organizer.rb +0 -130
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
|
|
4
|
+
require 'spec_helper'
|
|
5
|
+
|
|
6
|
+
describe 'embeds_many associations' do
|
|
7
|
+
|
|
8
|
+
context 're-associating the same object' do
|
|
9
|
+
context 'with dependent: destroy' do
|
|
10
|
+
let(:canvas) do
|
|
11
|
+
Canvas.create!(shapes: [Shape.new])
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
let!(:shape) { canvas.shapes.first }
|
|
15
|
+
|
|
16
|
+
it 'does not destroy the dependent object' do
|
|
17
|
+
canvas.shapes = [shape]
|
|
18
|
+
canvas.save!
|
|
19
|
+
canvas.reload
|
|
20
|
+
canvas.shapes.should == [shape]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
context 'clearing association when parent is not saved' do
|
|
26
|
+
let!(:parent) { Canvas.create!(shapes: [Shape.new]) }
|
|
27
|
+
|
|
28
|
+
let(:unsaved_parent) { Canvas.new(id: parent.id, shapes: [Shape.new]) }
|
|
29
|
+
|
|
30
|
+
context "using #clear" do
|
|
31
|
+
it 'deletes the target from the database' do
|
|
32
|
+
unsaved_parent.shapes.clear
|
|
33
|
+
|
|
34
|
+
unsaved_parent.shapes.should be_empty
|
|
35
|
+
|
|
36
|
+
unsaved_parent.new_record?.should be true
|
|
37
|
+
parent.reload
|
|
38
|
+
parent.shapes.should be_empty
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
shared_examples 'does not delete the target from the database' do
|
|
43
|
+
it 'does not delete the target from the database' do
|
|
44
|
+
unsaved_parent.shapes.should be_empty
|
|
45
|
+
|
|
46
|
+
unsaved_parent.new_record?.should be true
|
|
47
|
+
parent.reload
|
|
48
|
+
parent.shapes.length.should == 1
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
context "using #delete_all" do
|
|
53
|
+
before do
|
|
54
|
+
unsaved_parent.shapes.delete_all
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
include_examples 'does not delete the target from the database'
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
context "using #destroy_all" do
|
|
61
|
+
before do
|
|
62
|
+
unsaved_parent.shapes.destroy_all
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
include_examples 'does not delete the target from the database'
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
|
|
4
|
+
require 'spec_helper'
|
|
5
|
+
|
|
6
|
+
describe 'embeds_one associations' do
|
|
7
|
+
|
|
8
|
+
context 're-associating the same object' do
|
|
9
|
+
context 'with dependent: destroy' do
|
|
10
|
+
let(:canvas) do
|
|
11
|
+
Canvas.create!(palette: Palette.new)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
let!(:palette) { canvas.palette }
|
|
15
|
+
|
|
16
|
+
it 'does not destroy the dependent object' do
|
|
17
|
+
canvas.palette = canvas.palette
|
|
18
|
+
canvas.save!
|
|
19
|
+
canvas.reload
|
|
20
|
+
canvas.palette.should == palette
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -66,4 +66,64 @@ describe 'has_many associations' do
|
|
|
66
66
|
end
|
|
67
67
|
end
|
|
68
68
|
end
|
|
69
|
+
|
|
70
|
+
context 'when child does not have parent association' do
|
|
71
|
+
context 'Child.new' do
|
|
72
|
+
it 'creates a child instance' do
|
|
73
|
+
HmmBusSeat.new.should be_a(HmmBusSeat)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
context 'assignment to child in parent' do
|
|
78
|
+
let(:parent) { HmmBus.new }
|
|
79
|
+
|
|
80
|
+
it 'raises InverseNotFound' do
|
|
81
|
+
lambda do
|
|
82
|
+
parent.seats << HmmBusSeat.new
|
|
83
|
+
end.should raise_error(Mongoid::Errors::InverseNotFound)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
context 're-associating the same object' do
|
|
89
|
+
context 'with dependent: destroy' do
|
|
90
|
+
let(:wiki_page) do
|
|
91
|
+
WikiPage.create!
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
let!(:comment) do
|
|
95
|
+
Comment.create!(wiki_page: wiki_page, title: 'hi') do
|
|
96
|
+
wiki_page.reload
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
it 'does not destroy the dependent object' do
|
|
101
|
+
wiki_page.comments.should == [comment]
|
|
102
|
+
wiki_page.comments = [comment]
|
|
103
|
+
wiki_page.save!
|
|
104
|
+
wiki_page.reload
|
|
105
|
+
wiki_page.comments.should == [comment]
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
context 'without dependent: destroy' do
|
|
110
|
+
let(:series) do
|
|
111
|
+
Series.create!
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
let!(:book) do
|
|
115
|
+
Book.create!(series: series).tap do
|
|
116
|
+
series.reload
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
it 'does not destroy the dependent object' do
|
|
121
|
+
series.books.should == [book]
|
|
122
|
+
series.books = [book]
|
|
123
|
+
series.save!
|
|
124
|
+
series.reload
|
|
125
|
+
series.books.should == [book]
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
69
129
|
end
|
|
@@ -66,4 +66,112 @@ describe 'has_one associations' do
|
|
|
66
66
|
end
|
|
67
67
|
end
|
|
68
68
|
end
|
|
69
|
+
|
|
70
|
+
context 'when calling methods on target' do
|
|
71
|
+
let(:parent) do
|
|
72
|
+
HomCollege.create!.tap do |college|
|
|
73
|
+
HomAccreditation.create!(college: college)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
shared_examples 'delegates to the field' do |reloaded: false|
|
|
78
|
+
context 'non-conflicting field name' do
|
|
79
|
+
it 'delegates to the field' do
|
|
80
|
+
parent.accreditation.price.should == 42
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
context 'using send' do
|
|
84
|
+
it 'delegates to the field' do
|
|
85
|
+
parent.accreditation.send(:price).should == 42
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
context 'field name that conflicts with Kernel' do
|
|
91
|
+
it 'delegates to the field' do
|
|
92
|
+
parent.accreditation.format.should == 'fmt'
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
context 'using send' do
|
|
96
|
+
it 'delegates to the field' do
|
|
97
|
+
if reloaded
|
|
98
|
+
pending 'MONGOID-4018'
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
parent.accreditation.send(:format).should == 'fmt'
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
include_examples 'delegates to the field'
|
|
108
|
+
|
|
109
|
+
context 'after reloading parent' do
|
|
110
|
+
before do
|
|
111
|
+
parent.reload
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
include_examples 'delegates to the field', reloaded: true
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
context 'when child does not have parent association' do
|
|
119
|
+
context 'Child.new' do
|
|
120
|
+
it 'creates a child instance' do
|
|
121
|
+
HomBusDriver.new.should be_a(HomBusDriver)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
context 'assignment to child in parent' do
|
|
126
|
+
let(:parent) { HomBus.new }
|
|
127
|
+
|
|
128
|
+
it 'raises InverseNotFound' do
|
|
129
|
+
lambda do
|
|
130
|
+
parent.driver = HomBusDriver.new
|
|
131
|
+
end.should raise_error(Mongoid::Errors::InverseNotFound)
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
context 're-associating the same object' do
|
|
137
|
+
context 'with dependent: destroy' do
|
|
138
|
+
let(:person) do
|
|
139
|
+
Person.create!
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
let!(:game) do
|
|
143
|
+
Game.create!(person: person) do
|
|
144
|
+
person.reload
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
it 'does not destroy the dependent object' do
|
|
149
|
+
person.game.should == game
|
|
150
|
+
person.game = person.game
|
|
151
|
+
person.save!
|
|
152
|
+
person.reload
|
|
153
|
+
person.game.should == game
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
context 'without dependent: destroy' do
|
|
158
|
+
let(:person) do
|
|
159
|
+
Person.create!
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
let!(:account) do
|
|
163
|
+
Account.create!(person: person, name: 'foo').tap do
|
|
164
|
+
person.reload
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
it 'does not destroy the dependent object' do
|
|
169
|
+
person.account.should == account
|
|
170
|
+
person.account = person.account
|
|
171
|
+
person.save!
|
|
172
|
+
person.reload
|
|
173
|
+
person.account.should == account
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
69
177
|
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
class Galaxy
|
|
2
|
+
include Mongoid::Document
|
|
3
|
+
|
|
4
|
+
field :age, type: Integer
|
|
5
|
+
|
|
6
|
+
before_validation :set_age
|
|
7
|
+
|
|
8
|
+
embeds_many :stars
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
def set_age
|
|
13
|
+
self.age ||= 100_000
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class Star
|
|
18
|
+
include Mongoid::Document
|
|
19
|
+
|
|
20
|
+
embedded_in :galaxy
|
|
21
|
+
|
|
22
|
+
field :age, type: Integer
|
|
23
|
+
|
|
24
|
+
before_validation :set_age
|
|
25
|
+
|
|
26
|
+
embeds_many :planets
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def set_age
|
|
31
|
+
self.age ||= 42_000
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
class Planet
|
|
36
|
+
include Mongoid::Document
|
|
37
|
+
|
|
38
|
+
embedded_in :star
|
|
39
|
+
|
|
40
|
+
field :age, type: Integer
|
|
41
|
+
|
|
42
|
+
before_validation :set_age
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def set_age
|
|
47
|
+
self.age ||= 2_000
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
|
|
4
|
+
require 'spec_helper'
|
|
5
|
+
require_relative './callbacks_models'
|
|
6
|
+
|
|
7
|
+
describe 'callbacks integration tests' do
|
|
8
|
+
context 'when modifying attributes in a callback' do
|
|
9
|
+
|
|
10
|
+
context 'when creating top-level document' do
|
|
11
|
+
context 'top level document' do
|
|
12
|
+
let(:instance) do
|
|
13
|
+
Galaxy.create!
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it 'writes the attribute value into the model' do
|
|
17
|
+
instance.age.should == 100_000
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it 'persists the attribute value' do
|
|
21
|
+
Galaxy.find(instance.id).age.should == 100_000
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
context 'embedded document' do
|
|
26
|
+
shared_examples 'persists the attribute value' do
|
|
27
|
+
it 'writes the attribute value into the model' do
|
|
28
|
+
instance.stars.first.age.should == 42_000
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'persists the attribute value' do
|
|
32
|
+
Galaxy.find(instance.id).stars.first.age.should == 42_000
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
context 'set as a document instance' do
|
|
37
|
+
let(:instance) do
|
|
38
|
+
Galaxy.create!(stars: [Star.new])
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
include_examples 'persists the attribute value'
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
context 'set as attributes on parent' do
|
|
45
|
+
let(:instance) do
|
|
46
|
+
Galaxy.create!(stars: [{}])
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
include_examples 'persists the attribute value'
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
context 'nested embedded document' do
|
|
54
|
+
shared_examples 'persists the attribute value' do
|
|
55
|
+
it 'writes the attribute value into the model' do
|
|
56
|
+
instance.stars.first.planets.first.age.should == 2_000
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it 'persists the attribute value' do
|
|
60
|
+
Galaxy.find(instance.id).stars.first.planets.first.age.should == 2_000
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
context 'set as a document instance' do
|
|
65
|
+
let(:instance) do
|
|
66
|
+
Galaxy.create!(stars: [Star.new(
|
|
67
|
+
planets: [Planet.new],
|
|
68
|
+
)])
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
include_examples 'persists the attribute value'
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
context 'set as attributes on parent' do
|
|
75
|
+
let(:instance) do
|
|
76
|
+
Galaxy.create!(stars: [
|
|
77
|
+
planets: [{}],
|
|
78
|
+
])
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
include_examples 'persists the attribute value'
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
context 'when updating top-level document via #save' do
|
|
87
|
+
let!(:instance) do
|
|
88
|
+
Galaxy.create!
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
context 'embedded document' do
|
|
92
|
+
shared_examples 'persists the attribute value' do
|
|
93
|
+
it 'writes the attribute value into the model' do
|
|
94
|
+
instance.stars.first.age.should == 42_000
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it 'persists the attribute value' do
|
|
98
|
+
Galaxy.find(instance.id).stars.first.age.should == 42_000
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
context 'set as a document instance' do
|
|
103
|
+
before do
|
|
104
|
+
instance.stars = [Star.new]
|
|
105
|
+
instance.save!
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
include_examples 'persists the attribute value'
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
context 'set as attributes on parent' do
|
|
112
|
+
before do
|
|
113
|
+
instance.stars = [{}]
|
|
114
|
+
instance.save!
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
include_examples 'persists the attribute value'
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
context 'nested embedded document' do
|
|
122
|
+
shared_examples 'persists the attribute value' do
|
|
123
|
+
it 'writes the attribute value into the model' do
|
|
124
|
+
instance.stars.first.planets.first.age.should == 2_000
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
it 'persists the attribute value' do
|
|
128
|
+
Galaxy.find(instance.id).stars.first.planets.first.age.should == 2_000
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
context 'set as a document instance' do
|
|
133
|
+
before do
|
|
134
|
+
instance.stars = [Star.new(planets: [Planet.new])]
|
|
135
|
+
instance.save!
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
include_examples 'persists the attribute value'
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
context 'set as attributes on parent' do
|
|
142
|
+
before do
|
|
143
|
+
instance.stars = [planets: [{}]]
|
|
144
|
+
instance.save!
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
include_examples 'persists the attribute value'
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
context 'when updating top-level document via #update_attributes' do
|
|
153
|
+
let!(:instance) do
|
|
154
|
+
Galaxy.create!
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
context 'embedded document' do
|
|
158
|
+
shared_examples 'persists the attribute value' do
|
|
159
|
+
it 'writes the attribute value into the model' do
|
|
160
|
+
instance.stars.first.age.should == 42_000
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
it 'persists the attribute value' do
|
|
164
|
+
Galaxy.find(instance.id).stars.first.age.should == 42_000
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
context 'set as a document instance' do
|
|
169
|
+
before do
|
|
170
|
+
instance.update_attributes(stars: [Star.new])
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
include_examples 'persists the attribute value'
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
context 'set as attributes on parent' do
|
|
177
|
+
before do
|
|
178
|
+
instance.update_attributes(stars: [{}])
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
include_examples 'persists the attribute value'
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
context 'nested embedded document' do
|
|
186
|
+
shared_examples 'persists the attribute value' do
|
|
187
|
+
it 'writes the attribute value into the model' do
|
|
188
|
+
instance.stars.first.planets.first.age.should == 2_000
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
it 'persists the attribute value' do
|
|
192
|
+
pending 'MONGOID-4476'
|
|
193
|
+
|
|
194
|
+
Galaxy.find(instance.id).stars.first.planets.first.age.should == 2_000
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
context 'set as a document instance' do
|
|
199
|
+
before do
|
|
200
|
+
instance.update_attributes(stars: [Star.new(planets: [Planet.new])])
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
include_examples 'persists the attribute value'
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
context 'set as attributes on parent' do
|
|
207
|
+
before do
|
|
208
|
+
instance.update_attributes(stars: [planets: [{}]])
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
include_examples 'persists the attribute value'
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
end
|