mongoid 5.1.4 → 5.1.5
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/lib/config/locales/en.yml +1 -4
- data/lib/mongoid/clients/options.rb +2 -2
- data/lib/mongoid/contextual/memory.rb +2 -6
- data/lib/mongoid/contextual/mongo.rb +17 -16
- data/lib/mongoid/criteria/includable.rb +14 -61
- data/lib/mongoid/persistable/settable.rb +10 -1
- data/lib/mongoid/relations/counter_cache.rb +3 -3
- data/lib/mongoid/relations/eager.rb +7 -19
- data/lib/mongoid/relations/eager/base.rb +1 -1
- data/lib/mongoid/version.rb +1 -1
- data/spec/app/models/church.rb +2 -0
- data/spec/app/models/post.rb +1 -0
- data/spec/app/models/post_genre.rb +6 -0
- data/spec/mongoid/clients_spec.rb +30 -15
- data/spec/mongoid/contextual/mongo_spec.rb +0 -15
- data/spec/mongoid/criteria_spec.rb +27 -25
- data/spec/mongoid/persistable/settable_spec.rb +112 -0
- data/spec/mongoid/relations/counter_cache_spec.rb +12 -0
- metadata +4 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: abf3bc71acef310e6e22c9900a173ef44adcffe9
|
4
|
+
data.tar.gz: 3178495b48b2377e77929352b9349c02adee7017
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 16106f196625909b1bbefd3bea0ab5e7b3397cd8055108c01c7f5b1ae7dc485810a9fa78ab5975b3794e2db8a2fe2b82d9c8d3e18b69e762f2ff994727397eba
|
7
|
+
data.tar.gz: e08c0e8efaa17427d5d3e329322dd367b72a44eb5f3a5c9e2d0d1c9bf5d4bebcae313ee155dbb5f78578cd98a767f2a7e78fad5747c87221176da465321384ab
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/config/locales/en.yml
CHANGED
@@ -115,10 +115,7 @@ en:
|
|
115
115
|
invalid_includes:
|
116
116
|
message: "Invalid includes directive: %{klass}.includes(%{args})"
|
117
117
|
summary: "Eager loading in Mongoid only supports providing arguments
|
118
|
-
to %{klass}.includes that are the names of relations on the %{klass}
|
119
|
-
model, and only supports one level of eager loading. (ie, eager loading
|
120
|
-
associations not on the %{klass} but one step away via another relation
|
121
|
-
is not allowed."
|
118
|
+
to %{klass}.includes that are the names of relations on the %{klass}."
|
122
119
|
resolution: "Ensure that each parameter passed to %{klass}.includes is
|
123
120
|
a valid name of a relation on the %{klass} model. These are: %{relations}."
|
124
121
|
invalid_index:
|
@@ -44,12 +44,12 @@ module Mongoid
|
|
44
44
|
|
45
45
|
def mongo_client
|
46
46
|
tmp = persistence_options
|
47
|
-
if opts = tmp && tmp.dup
|
47
|
+
if (opts = tmp && !tmp.empty? && tmp.dup)
|
48
48
|
if opts[:client]
|
49
49
|
client = Clients.with_name(opts[:client])
|
50
50
|
else
|
51
51
|
client = Clients.with_name(self.class.client_name)
|
52
|
-
client.use(self.class.database_name)
|
52
|
+
client = client.use(self.class.database_name)
|
53
53
|
end
|
54
54
|
client.with(opts.reject{ |k, v| k == :collection || k == :client })
|
55
55
|
end
|
@@ -130,9 +130,7 @@ module Mongoid
|
|
130
130
|
#
|
131
131
|
# @since 3.0.0
|
132
132
|
def first
|
133
|
-
|
134
|
-
eager_load_one(doc)
|
135
|
-
doc
|
133
|
+
eager_load([documents.first]).first
|
136
134
|
end
|
137
135
|
alias :one :first
|
138
136
|
alias :find_first :first
|
@@ -165,9 +163,7 @@ module Mongoid
|
|
165
163
|
#
|
166
164
|
# @since 3.0.0
|
167
165
|
def last
|
168
|
-
|
169
|
-
eager_load_one(doc)
|
170
|
-
doc
|
166
|
+
eager_load([documents.last]).first
|
171
167
|
end
|
172
168
|
|
173
169
|
# Get the length of matching documents in the context.
|
@@ -237,7 +237,10 @@ module Mongoid
|
|
237
237
|
def first
|
238
238
|
return documents.first if cached? && cache_loaded?
|
239
239
|
try_cache(:first) do
|
240
|
-
|
240
|
+
if raw_doc = view.limit(-1).first
|
241
|
+
doc = Factory.from_db(klass, raw_doc, criteria.options[:fields])
|
242
|
+
eager_load([doc]).first
|
243
|
+
end
|
241
244
|
end
|
242
245
|
end
|
243
246
|
alias :one :first
|
@@ -249,7 +252,10 @@ module Mongoid
|
|
249
252
|
# @since 4.0.2
|
250
253
|
def find_first
|
251
254
|
return documents.first if cached? && cache_loaded?
|
252
|
-
|
255
|
+
if raw_doc = view.first
|
256
|
+
doc = Factory.from_db(klass, raw_doc, criteria.options[:fields])
|
257
|
+
eager_load([doc]).first
|
258
|
+
end
|
253
259
|
end
|
254
260
|
|
255
261
|
# Execute a $geoNear command against the database.
|
@@ -334,7 +340,10 @@ module Mongoid
|
|
334
340
|
def last
|
335
341
|
try_cache(:last) do
|
336
342
|
with_inverse_sorting do
|
337
|
-
|
343
|
+
if raw_doc = view.limit(-1).first
|
344
|
+
doc = Factory.from_db(klass, raw_doc, criteria.options[:fields])
|
345
|
+
eager_load([doc]).first
|
346
|
+
end
|
338
347
|
end
|
339
348
|
end
|
340
349
|
end
|
@@ -643,15 +652,9 @@ module Mongoid
|
|
643
652
|
#
|
644
653
|
# @since 3.0.0
|
645
654
|
def documents_for_iteration
|
646
|
-
if cached? && !documents.empty?
|
647
|
-
|
648
|
-
|
649
|
-
docs = view.map{ |doc| Factory.from_db(klass, doc, criteria.options[:fields]) }
|
650
|
-
eager_load(docs)
|
651
|
-
docs
|
652
|
-
else
|
653
|
-
view
|
654
|
-
end
|
655
|
+
return documents if cached? && !documents.empty?
|
656
|
+
docs = view.map{ |doc| Factory.from_db(klass, doc, criteria.options[:fields]) }
|
657
|
+
eager_load(docs)
|
655
658
|
end
|
656
659
|
|
657
660
|
# Yield to the document.
|
@@ -667,10 +670,8 @@ module Mongoid
|
|
667
670
|
#
|
668
671
|
# @since 3.0.0
|
669
672
|
def yield_document(document, &block)
|
670
|
-
|
671
|
-
|
672
|
-
yield(doc)
|
673
|
-
documents.push(doc) if cacheable?
|
673
|
+
yield(document)
|
674
|
+
documents.push(document) if cacheable?
|
674
675
|
end
|
675
676
|
end
|
676
677
|
end
|
@@ -27,13 +27,7 @@ module Mongoid
|
|
27
27
|
#
|
28
28
|
# @since 2.2.0
|
29
29
|
def includes(*relations)
|
30
|
-
relations
|
31
|
-
if relation.is_a?(Hash)
|
32
|
-
extract_nested_inclusion(klass, relation)
|
33
|
-
else
|
34
|
-
add_inclusion(klass, relation)
|
35
|
-
end
|
36
|
-
end
|
30
|
+
extract_includes_list(klass, relations)
|
37
31
|
clone
|
38
32
|
end
|
39
33
|
|
@@ -76,67 +70,26 @@ module Mongoid
|
|
76
70
|
# @raise [ Errors::InvalidIncludes ] If no relation is found.
|
77
71
|
#
|
78
72
|
# @since 5.1.0
|
79
|
-
def add_inclusion(_klass,
|
80
|
-
metadata = get_inclusion_metadata(_klass, relation)
|
81
|
-
raise Errors::InvalidIncludes.new(_klass, [ relation ]) unless metadata
|
73
|
+
def add_inclusion(_klass, metadata)
|
82
74
|
inclusions.push(metadata) unless inclusions.include?(metadata)
|
83
75
|
end
|
84
76
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
def extract_relations_list(association, relations)
|
95
|
-
relations.each do |relation|
|
96
|
-
if relation.is_a?(Hash)
|
97
|
-
extract_nested_inclusion(association, relation)
|
77
|
+
def extract_includes_list(_parent_class, *relations_list)
|
78
|
+
relations_list.flatten.each do |relation_object|
|
79
|
+
if relation_object.is_a?(Hash)
|
80
|
+
relation_object.each do |relation, _includes|
|
81
|
+
metadata = _parent_class.reflect_on_association(relation)
|
82
|
+
raise Errors::InvalidIncludes.new(_klass, [ relation ]) unless metadata
|
83
|
+
add_inclusion(_parent_class, metadata)
|
84
|
+
extract_includes_list(metadata.klass, _includes)
|
85
|
+
end
|
98
86
|
else
|
99
|
-
|
87
|
+
metadata = _parent_class.reflect_on_association(relation_object)
|
88
|
+
raise Errors::InvalidIncludes.new(_parent_class, [ relation_object ]) unless metadata
|
89
|
+
add_inclusion(_parent_class, metadata)
|
100
90
|
end
|
101
91
|
end
|
102
92
|
end
|
103
|
-
|
104
|
-
# Extract nested inclusion.
|
105
|
-
#
|
106
|
-
# @example Extract the inclusions from a nested definition.
|
107
|
-
# criteria.extract_nested_inclusion(User, { :posts => [:alerts] })
|
108
|
-
#
|
109
|
-
# @param [ Class, Symbol ] _klass The class for which the inclusion should be added.
|
110
|
-
# @param [ Hash ] relation The nested inclusion.
|
111
|
-
#
|
112
|
-
# @since 5.1.0
|
113
|
-
def extract_nested_inclusion(_klass, relation)
|
114
|
-
relation.each do |association, _inclusion|
|
115
|
-
add_inclusion(_klass, association)
|
116
|
-
if _inclusion.is_a?(Array)
|
117
|
-
extract_relations_list(association, _inclusion)
|
118
|
-
else
|
119
|
-
add_inclusion(association, _inclusion)
|
120
|
-
end
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
# Get the metadata for an inclusion.
|
125
|
-
#
|
126
|
-
# @example Get the metadata for an inclusion definition.
|
127
|
-
# criteria.get_inclusion_metadata(User, :posts)
|
128
|
-
#
|
129
|
-
# @param [ Class, Symbol, String ] _klass The class for determining the association metadata
|
130
|
-
# @param [ Symbol ] association The name of the association.
|
131
|
-
#
|
132
|
-
# @since 5.1.0
|
133
|
-
def get_inclusion_metadata(_klass, association)
|
134
|
-
if _klass.is_a?(Class)
|
135
|
-
_klass.reflect_on_association(association)
|
136
|
-
else
|
137
|
-
_klass.to_s.classify.constantize.reflect_on_association(association)
|
138
|
-
end
|
139
|
-
end
|
140
93
|
end
|
141
94
|
end
|
142
95
|
end
|
@@ -22,7 +22,16 @@ 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
|
+
|
26
|
+
field_and_value_hash = hasherizer(field.split('.'), value)
|
27
|
+
field = field_and_value_hash.keys.first.to_s
|
28
|
+
|
29
|
+
if fields[field] && fields[field].type == Hash && attributes.key?(field)
|
30
|
+
process_attribute(field.to_s, attributes[field].merge(field_and_value_hash[field]))
|
31
|
+
else
|
32
|
+
process_attribute(field.to_s, field_and_value_hash[field])
|
33
|
+
end
|
34
|
+
|
26
35
|
unless relations.include?(field.to_s)
|
27
36
|
ops[atomic_attribute_name(field)] = attributes[field]
|
28
37
|
end
|
@@ -105,10 +105,10 @@ module Mongoid
|
|
105
105
|
|
106
106
|
after_update do
|
107
107
|
if record = __send__(name)
|
108
|
-
|
108
|
+
foreign_key = meta.foreign_key
|
109
109
|
|
110
|
-
if attribute_changed?(
|
111
|
-
original, current = attribute_change(
|
110
|
+
if attribute_changed?(foreign_key)
|
111
|
+
original, current = attribute_change(foreign_key)
|
112
112
|
|
113
113
|
unless original.nil?
|
114
114
|
record.class.decrement_counter(cache_column, original)
|
@@ -9,28 +9,16 @@ module Mongoid
|
|
9
9
|
module Relations
|
10
10
|
module Eager
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
def with_eager_loading(document)
|
15
|
-
return nil unless document
|
16
|
-
doc = Factory.from_db(klass, document, criteria.options[:fields])
|
17
|
-
eager_load_one(doc)
|
18
|
-
doc
|
19
|
-
end
|
20
|
-
|
21
|
-
def eager_load_one(doc)
|
22
|
-
eager_load([doc])
|
23
|
-
end
|
24
|
-
|
25
|
-
def eager_loadable?(document = nil)
|
26
|
-
return false if criteria.inclusions.empty?
|
27
|
-
!eager_loaded
|
12
|
+
def eager_loadable?
|
13
|
+
!criteria.inclusions.empty?
|
28
14
|
end
|
29
15
|
|
30
16
|
def eager_load(docs)
|
31
|
-
|
32
|
-
|
33
|
-
|
17
|
+
docs.tap do |docs|
|
18
|
+
if eager_loadable?
|
19
|
+
preload(criteria.inclusions, docs)
|
20
|
+
end
|
21
|
+
end
|
34
22
|
end
|
35
23
|
|
36
24
|
def preload(relations, docs)
|
data/lib/mongoid/version.rb
CHANGED
data/spec/app/models/church.rb
CHANGED
data/spec/app/models/post.rb
CHANGED
@@ -11,6 +11,7 @@ class Post
|
|
11
11
|
|
12
12
|
belongs_to :person, counter_cache: true
|
13
13
|
belongs_to :author, foreign_key: :author_id, class_name: "User"
|
14
|
+
belongs_to :post_genre, foreign_key: :genre_id, counter_cache: true
|
14
15
|
has_and_belongs_to_many :tags, before_add: :before_add_tag, after_add: :after_add_tag, before_remove: :before_remove_tag, after_remove: :after_remove_tag
|
15
16
|
has_many :videos, validate: false
|
16
17
|
has_many :roles, validate: false
|
@@ -731,27 +731,42 @@ describe Mongoid::Clients do
|
|
731
731
|
|
732
732
|
context "when the override is global" do
|
733
733
|
|
734
|
-
|
735
|
-
Mongoid.override_database(:mongoid_optional)
|
736
|
-
end
|
734
|
+
shared_examples_for "a global database override" do
|
737
735
|
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
end
|
736
|
+
before do
|
737
|
+
Mongoid.override_database(:mongoid_optional)
|
738
|
+
end
|
742
739
|
|
743
|
-
|
744
|
-
|
745
|
-
|
740
|
+
after do
|
741
|
+
Band.delete_all
|
742
|
+
Mongoid.override_database(nil)
|
743
|
+
end
|
744
|
+
|
745
|
+
let!(:band) do
|
746
|
+
klass.create(name: "Tool")
|
747
|
+
end
|
748
|
+
|
749
|
+
it "persists to the overridden database" do
|
750
|
+
mongo_client = Band.mongo_client.with(database: :mongoid_optional)
|
751
|
+
expect(mongo_client[:bands].count(name: "Tool")).to eq(1)
|
752
|
+
end
|
746
753
|
|
747
|
-
|
748
|
-
|
749
|
-
expect(sess[:bands].find(name: "Tool")).to_not be_nil
|
754
|
+
it 'uses that database for the model mongo_client' do
|
755
|
+
expect(Band.mongo_client.database.name).to eq('mongoid_optional')
|
750
756
|
end
|
751
757
|
end
|
752
758
|
|
753
|
-
|
754
|
-
|
759
|
+
context "when normal usage" do
|
760
|
+
let(:klass) { Band }
|
761
|
+
|
762
|
+
it_behaves_like "a global database override"
|
763
|
+
end
|
764
|
+
|
765
|
+
context "when overriding the persistence options" do
|
766
|
+
|
767
|
+
let(:klass) { Band.with(connect_timeout: 10) }
|
768
|
+
|
769
|
+
it_behaves_like "a global database override"
|
755
770
|
end
|
756
771
|
end
|
757
772
|
end
|
@@ -115,21 +115,6 @@ describe Mongoid::Contextual::Mongo do
|
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
|
-
context "when provided a document" do
|
119
|
-
|
120
|
-
let(:context) do
|
121
|
-
described_class.new(criteria)
|
122
|
-
end
|
123
|
-
|
124
|
-
let(:count) do
|
125
|
-
context.count(depeche)
|
126
|
-
end
|
127
|
-
|
128
|
-
it "returns the number of documents that match" do
|
129
|
-
expect(count).to eq(1)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
118
|
context "when provided a block" do
|
134
119
|
|
135
120
|
let(:context) do
|
@@ -1147,6 +1147,29 @@ describe Mongoid::Criteria do
|
|
1147
1147
|
end
|
1148
1148
|
end
|
1149
1149
|
|
1150
|
+
context "when providing one association" do
|
1151
|
+
|
1152
|
+
let!(:user) do
|
1153
|
+
User.create(posts: [ post1 ])
|
1154
|
+
end
|
1155
|
+
|
1156
|
+
let!(:post1) do
|
1157
|
+
Post.create
|
1158
|
+
end
|
1159
|
+
|
1160
|
+
let(:result) do
|
1161
|
+
User.includes(:posts).first
|
1162
|
+
end
|
1163
|
+
|
1164
|
+
it "executes the query" do
|
1165
|
+
expect(result).to eq(user)
|
1166
|
+
end
|
1167
|
+
|
1168
|
+
it "includes the related objects" do
|
1169
|
+
expect(result.posts).to eq([ post1 ])
|
1170
|
+
end
|
1171
|
+
end
|
1172
|
+
|
1150
1173
|
context "when providing a list of associations" do
|
1151
1174
|
|
1152
1175
|
let!(:user) do
|
@@ -1504,13 +1527,6 @@ describe Mongoid::Criteria do
|
|
1504
1527
|
end
|
1505
1528
|
end
|
1506
1529
|
|
1507
|
-
it "does not eager load the first document" do
|
1508
|
-
doc = criteria.first
|
1509
|
-
expect_query(1) do
|
1510
|
-
expect(doc.person).to eq(person)
|
1511
|
-
end
|
1512
|
-
end
|
1513
|
-
|
1514
1530
|
it "returns the last document" do
|
1515
1531
|
expect(document).to eq(post_two)
|
1516
1532
|
end
|
@@ -1566,13 +1582,6 @@ describe Mongoid::Criteria do
|
|
1566
1582
|
end
|
1567
1583
|
end
|
1568
1584
|
|
1569
|
-
it "does not eager load the last document" do
|
1570
|
-
doc = criteria.last
|
1571
|
-
expect_query(1) do
|
1572
|
-
expect(doc.band).to eq(tool)
|
1573
|
-
end
|
1574
|
-
end
|
1575
|
-
|
1576
1585
|
it "returns the document" do
|
1577
1586
|
expect(document).to eq(address_one)
|
1578
1587
|
end
|
@@ -1598,13 +1607,6 @@ describe Mongoid::Criteria do
|
|
1598
1607
|
end
|
1599
1608
|
end
|
1600
1609
|
|
1601
|
-
it "does not eager load the first document" do
|
1602
|
-
doc = criteria.first
|
1603
|
-
expect_query(1) do
|
1604
|
-
expect(doc.band).to eq(depeche)
|
1605
|
-
end
|
1606
|
-
end
|
1607
|
-
|
1608
1610
|
it "returns the document" do
|
1609
1611
|
expect(document).to eq(address_two)
|
1610
1612
|
end
|
@@ -1698,7 +1700,7 @@ describe Mongoid::Criteria do
|
|
1698
1700
|
end
|
1699
1701
|
|
1700
1702
|
before do
|
1701
|
-
expect(new_context).to receive(:
|
1703
|
+
expect(new_context).to receive(:eager_load).with([person]).once.and_call_original
|
1702
1704
|
end
|
1703
1705
|
|
1704
1706
|
let!(:from_db) do
|
@@ -1749,7 +1751,7 @@ describe Mongoid::Criteria do
|
|
1749
1751
|
end
|
1750
1752
|
|
1751
1753
|
before do
|
1752
|
-
expect(context).to receive(:
|
1754
|
+
expect(context).to receive(:eager_load).with([person]).once.and_call_original
|
1753
1755
|
end
|
1754
1756
|
|
1755
1757
|
let!(:from_db) do
|
@@ -2128,7 +2130,7 @@ describe Mongoid::Criteria do
|
|
2128
2130
|
end
|
2129
2131
|
|
2130
2132
|
before do
|
2131
|
-
expect(context).to receive(:
|
2133
|
+
expect(context).to receive(:preload).twice.and_call_original
|
2132
2134
|
end
|
2133
2135
|
|
2134
2136
|
let!(:documents) do
|
@@ -2191,7 +2193,7 @@ describe Mongoid::Criteria do
|
|
2191
2193
|
end
|
2192
2194
|
|
2193
2195
|
before do
|
2194
|
-
expect(context).to receive(:
|
2196
|
+
expect(context).to receive(:preload).twice.and_call_original
|
2195
2197
|
end
|
2196
2198
|
|
2197
2199
|
let!(:documents) do
|
@@ -204,4 +204,116 @@ describe Mongoid::Persistable::Settable do
|
|
204
204
|
expect(agent.reload.title).to eq title
|
205
205
|
end
|
206
206
|
end
|
207
|
+
|
208
|
+
context 'when the field is already set locally' do
|
209
|
+
|
210
|
+
let(:church) do
|
211
|
+
Church.new.tap do |a|
|
212
|
+
a.location = { 'city' => 'Berlin' }
|
213
|
+
a.name = 'Church1'
|
214
|
+
a.save
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
context 'when the field is a Hash type' do
|
219
|
+
|
220
|
+
before do
|
221
|
+
church.set('location.neighborhood' => 'Kreuzberg')
|
222
|
+
end
|
223
|
+
|
224
|
+
it 'updates the hash while keeping existing key and values locally' do
|
225
|
+
expect(church.location).to eq({ 'city' => 'Berlin', 'neighborhood' => 'Kreuzberg'})
|
226
|
+
end
|
227
|
+
|
228
|
+
it 'updates the hash in the database' do
|
229
|
+
expect(church.reload.location).to eq({ 'city' => 'Berlin', 'neighborhood' => 'Kreuzberg'})
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
context 'when the field type is String' do
|
234
|
+
|
235
|
+
before do
|
236
|
+
church.set('name' => 'Church2')
|
237
|
+
end
|
238
|
+
|
239
|
+
it 'updates the field locally' do
|
240
|
+
expect(church.name).to eq('Church2')
|
241
|
+
end
|
242
|
+
|
243
|
+
it 'updates the field in the database' do
|
244
|
+
expect(church.reload.name).to eq('Church2')
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
context 'when there are two fields of type Hash and String' do
|
249
|
+
|
250
|
+
before do
|
251
|
+
church.set('name' => 'Church2', 'location.street' => 'Yorckstr.')
|
252
|
+
end
|
253
|
+
|
254
|
+
it 'updates the fields locally' do
|
255
|
+
expect(church.name).to eq('Church2')
|
256
|
+
expect(church.location).to eq({ 'city' => 'Berlin', 'street' => 'Yorckstr.'})
|
257
|
+
end
|
258
|
+
|
259
|
+
it 'updates the fields in the database' do
|
260
|
+
expect(church.reload.name).to eq('Church2')
|
261
|
+
expect(church.reload.location).to eq({ 'city' => 'Berlin', 'street' => 'Yorckstr.'})
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
context 'when the field is not already set locally' do
|
267
|
+
|
268
|
+
let(:church) do
|
269
|
+
Church.create
|
270
|
+
end
|
271
|
+
|
272
|
+
context 'when the field is a Hash type' do
|
273
|
+
|
274
|
+
before do
|
275
|
+
church.set('location.neighborhood' => 'Kreuzberg')
|
276
|
+
end
|
277
|
+
|
278
|
+
it 'sets the hash locally' do
|
279
|
+
expect(church.location).to eq({ 'neighborhood' => 'Kreuzberg'})
|
280
|
+
end
|
281
|
+
|
282
|
+
it 'sets the hash in the database' do
|
283
|
+
expect(church.reload.location).to eq({ 'neighborhood' => 'Kreuzberg'})
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
context 'when the field type is String' do
|
288
|
+
|
289
|
+
before do
|
290
|
+
church.set('name' => 'Church2')
|
291
|
+
end
|
292
|
+
|
293
|
+
it 'sets the field locally' do
|
294
|
+
expect(church.name).to eq('Church2')
|
295
|
+
end
|
296
|
+
|
297
|
+
it 'sets the field in the database' do
|
298
|
+
expect(church.reload.name).to eq('Church2')
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
context 'when there are two fields of type Hash and String' do
|
303
|
+
|
304
|
+
before do
|
305
|
+
church.set('name' => 'Church2', 'location.street' => 'Yorckstr.')
|
306
|
+
end
|
307
|
+
|
308
|
+
it 'sets the fields locally' do
|
309
|
+
expect(church.name).to eq('Church2')
|
310
|
+
expect(church.location).to eq({ 'street' => 'Yorckstr.'})
|
311
|
+
end
|
312
|
+
|
313
|
+
it 'sets the fields in the database' do
|
314
|
+
expect(church.reload.name).to eq('Church2')
|
315
|
+
expect(church.reload.location).to eq({ 'street' => 'Yorckstr.'})
|
316
|
+
end
|
317
|
+
end
|
318
|
+
end
|
207
319
|
end
|
@@ -351,6 +351,18 @@ describe Mongoid::Relations::CounterCache do
|
|
351
351
|
it "updates the original object's counter cache" do
|
352
352
|
expect(person1.reload.drugs_count).to eq(1)
|
353
353
|
end
|
354
|
+
|
355
|
+
context 'when foreign_key differs from model name' do
|
356
|
+
|
357
|
+
let(:genre) { PostGenre.create }
|
358
|
+
|
359
|
+
let(:post) { Post.create }
|
360
|
+
|
361
|
+
it 'updates correct counter cache' do
|
362
|
+
post.update post_genre: genre
|
363
|
+
expect(genre.reload.posts_count).to eq 1
|
364
|
+
end
|
365
|
+
end
|
354
366
|
end
|
355
367
|
end
|
356
368
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.1.
|
4
|
+
version: 5.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Durran Jordan
|
@@ -30,7 +30,7 @@ cert_chain:
|
|
30
30
|
ZIvvwAhgCjVW5QCi2I1noxXLmtZ3XDawWu8kaGtu8giHXcwL3941m8hvFZ/Wr9Yi
|
31
31
|
JvcXJt2a4/JvwnIs2hmKuyfhZmB9HEE5wQQaCMnnC14=
|
32
32
|
-----END CERTIFICATE-----
|
33
|
-
date: 2016-
|
33
|
+
date: 2016-10-19 00:00:00.000000000 Z
|
34
34
|
dependencies:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: activemodel
|
@@ -503,6 +503,7 @@ files:
|
|
503
503
|
- spec/app/models/pizza.rb
|
504
504
|
- spec/app/models/player.rb
|
505
505
|
- spec/app/models/post.rb
|
506
|
+
- spec/app/models/post_genre.rb
|
506
507
|
- spec/app/models/powerup.rb
|
507
508
|
- spec/app/models/preference.rb
|
508
509
|
- spec/app/models/princess.rb
|
@@ -958,6 +959,7 @@ test_files:
|
|
958
959
|
- spec/app/models/pizza.rb
|
959
960
|
- spec/app/models/player.rb
|
960
961
|
- spec/app/models/post.rb
|
962
|
+
- spec/app/models/post_genre.rb
|
961
963
|
- spec/app/models/powerup.rb
|
962
964
|
- spec/app/models/preference.rb
|
963
965
|
- spec/app/models/princess.rb
|
metadata.gz.sig
CHANGED
Binary file
|