mongoid 6.0.0 → 6.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ba5a14c6242427982c5072b4f2e56ab499ff7559
4
- data.tar.gz: 39d47341e981c5facfd7360ae9a83c6b3bd2dc00
3
+ metadata.gz: ed08df05221cb2fd51ee6873d0057b4966a5c0b8
4
+ data.tar.gz: a741c71fcb479876d2ea795cecb20662732a2333
5
5
  SHA512:
6
- metadata.gz: 9e932dbd7064df798fae822b792a82b5b387a9c8daa93826158b15e5cbffe0ee59a0f92eb5b003d1e051097f4b017d6fc6f0c989bc00b363f4a8853a45689ccc
7
- data.tar.gz: 4121c17953fa7b3786e30ed6d3f0c473d5ed7bd26dc59e700c9d41061cd83e318d0c6a62c0539a4d74d7561f8b9bf9eaa437cce41421358d98c139705ad9325b
6
+ metadata.gz: 0cae8bddd78b2ede9c0a8ace4ce8c468699e2a9f3b4377b3af9dde6948a83d911271e5425944ff12a385a56f4c3316f360a313933db558e801b76cb1b23ecd47
7
+ data.tar.gz: 4611925c73babd6ec1b7b3297e764f78ecd3b49d8dea449dc43d45385151ca59b69dba0f8b454cd554583c4d5217fd1c24f83d7a16cddfd32e1e82611e7c0a95
@@ -1,2 +1 @@
1
- X� ��Vlܲ:��Ut�$KlY�=߽�r�zc��8Bu�N�Jd�2?�1�v�+��t�g�ӽ�w���Κ ���=$9��>�����=���ΝU��5ܩ `6C���Bx�j�Bzp�<�6^G{�2�#[ ����&�^��O� )b�zx�T�� K\Hy�����V�a��S ZUT7���H�Д,���,dW�ƴ�{W�����8mI���1&
2
1
  X�B�߁]c��
3
- ��8쬏�a���j��%�R�Dv�bϋ
2
+ Wǻ�fJw������ �<L�����ڐ�5��>�������1R9$�nPK)�M#����v��GM��q�?��S��j��YJA����{_�������%45f-{Hy�;BO�_M�$,��@S*i�HI}�З�����] ���Dd~1���@��;����j}�����la���]~0��ob8-�����t�_h�:��f�jF��J��̀�R���T8)w
data.tar.gz.sig CHANGED
Binary file
@@ -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:
@@ -336,7 +336,7 @@ module Mongoid
336
336
  end
337
337
 
338
338
  def lookup_attribute_presence(name, value)
339
- if localized_fields.has_key?(name)
339
+ if localized_fields.has_key?(name) && value
340
340
  value = localized_fields[name].send(:lookup, value)
341
341
  end
342
342
  value.present?
@@ -130,9 +130,7 @@ module Mongoid
130
130
  #
131
131
  # @since 3.0.0
132
132
  def first
133
- doc = documents.first
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
- doc = documents.last
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.
@@ -245,9 +245,15 @@ module Mongoid
245
245
  return documents.first if cached? && cache_loaded?
246
246
  try_cache(:first) do
247
247
  if sort = view.sort || ({ _id: 1 } unless opts[:id_sort] == :none)
248
- with_eager_loading(view.sort(sort).limit(-1).first)
248
+ if raw_doc = view.sort(sort).limit(-1).first
249
+ doc = Factory.from_db(klass, raw_doc, criteria.options[:fields])
250
+ eager_load([doc]).first
251
+ end
249
252
  else
250
- with_eager_loading(view.limit(-1).first)
253
+ if raw_doc = view.limit(-1).first
254
+ doc = Factory.from_db(klass, raw_doc, criteria.options[:fields])
255
+ eager_load([doc]).first
256
+ end
251
257
  end
252
258
  end
253
259
  end
@@ -260,7 +266,10 @@ module Mongoid
260
266
  # @since 4.0.2
261
267
  def find_first
262
268
  return documents.first if cached? && cache_loaded?
263
- with_eager_loading(view.first)
269
+ if raw_doc = view.first
270
+ doc = Factory.from_db(klass, raw_doc, criteria.options[:fields])
271
+ eager_load([doc]).first
272
+ end
264
273
  end
265
274
 
266
275
  # Execute a $geoNear command against the database.
@@ -348,7 +357,10 @@ module Mongoid
348
357
  def last(opts = {})
349
358
  try_cache(:last) do
350
359
  with_inverse_sorting(opts) do
351
- with_eager_loading(view.limit(-1).first)
360
+ if raw_doc = view.limit(-1).first
361
+ doc = Factory.from_db(klass, raw_doc, criteria.options[:fields])
362
+ eager_load([doc]).first
363
+ end
352
364
  end
353
365
  end
354
366
  end
@@ -657,15 +669,9 @@ module Mongoid
657
669
  #
658
670
  # @since 3.0.0
659
671
  def documents_for_iteration
660
- if cached? && !documents.empty?
661
- documents
662
- elsif eager_loadable?
663
- docs = view.map{ |doc| Factory.from_db(klass, doc, criteria.options[:fields]) }
664
- eager_load(docs)
665
- docs
666
- else
667
- view
668
- end
672
+ return documents if cached? && !documents.empty?
673
+ docs = view.map{ |doc| Factory.from_db(klass, doc, criteria.options[:fields]) }
674
+ eager_load(docs)
669
675
  end
670
676
 
671
677
  # Yield to the document.
@@ -681,10 +687,8 @@ module Mongoid
681
687
  #
682
688
  # @since 3.0.0
683
689
  def yield_document(document, &block)
684
- doc = document.respond_to?(:_id) ?
685
- document : Factory.from_db(klass, document, criteria.options[:fields])
686
- yield(doc)
687
- documents.push(doc) if cacheable?
690
+ yield(document)
691
+ documents.push(document) if cacheable?
688
692
  end
689
693
  end
690
694
  end
@@ -24,7 +24,7 @@ module Mongoid
24
24
  attrs = clone_document.except("_id", "id")
25
25
  dynamic_attrs = {}
26
26
  attrs.reject! do |attr_name, value|
27
- dynamic_attrs[attr_name] = value unless self.attribute_names.include?(attr_name)
27
+ dynamic_attrs.merge!(attr_name => value) unless self.attribute_names.include?(attr_name)
28
28
  end
29
29
  self.class.new(attrs).tap do |object|
30
30
  dynamic_attrs.each do |attr_name, value|
@@ -27,13 +27,7 @@ module Mongoid
27
27
  #
28
28
  # @since 2.2.0
29
29
  def includes(*relations)
30
- relations.flatten.each do |relation|
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, relation)
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
- # Extract inclusion definitions from a list.
86
- #
87
- # @example Extract the inclusions from a list.
88
- # criteria.extract_relations_list(:posts, [{ :alerts => :items }])
89
- #
90
- # @param [ Symbol ] association The name of the association.
91
- # @param [ Array ] relations A list of associations.
92
- #
93
- # @since 5.1.0
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
- add_inclusion(association, relation)
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
@@ -21,7 +21,7 @@ module Mongoid
21
21
  other.each_pair do |key, value|
22
22
  if value.is_a?(Hash) && self[key.to_s].is_a?(Hash)
23
23
  value = self[key.to_s].merge(value) do |_key, old_val, new_val|
24
- multi_value?(_key) ? (old_val + new_val).uniq : new_val
24
+ _key == '$in' ? new_val & old_val : old_val | new_val
25
25
  end
26
26
  end
27
27
  if multi_selection?(key)
@@ -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
- process_attribute(field.to_s, value)
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
@@ -107,10 +107,10 @@ module Mongoid
107
107
 
108
108
  after_update do
109
109
  if record = __send__(name)
110
- id_field = "#{name}_id"
110
+ foreign_key = meta.foreign_key
111
111
 
112
- if attribute_changed?(id_field)
113
- original, current = attribute_change(id_field)
112
+ if attribute_changed?(foreign_key)
113
+ original, current = attribute_change(foreign_key)
114
114
 
115
115
  unless original.nil?
116
116
  record.class.with(persistence_context) do |_class|
@@ -9,28 +9,16 @@ module Mongoid
9
9
  module Relations
10
10
  module Eager
11
11
 
12
- attr_accessor :eager_loaded
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
- return false unless eager_loadable?
32
- preload(criteria.inclusions, docs)
33
- self.eager_loaded = true
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)
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- VERSION = "6.0.0"
3
+ VERSION = "6.0.1"
4
4
  end
@@ -1,4 +1,6 @@
1
1
  class Church
2
2
  include Mongoid::Document
3
3
  has_many :acolytes, validate: false
4
+ field :location, type: Hash
5
+ field :name, type: String
4
6
  end
@@ -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
@@ -0,0 +1,6 @@
1
+ class PostGenre
2
+ include Mongoid::Document
3
+ field :posts_count, type: Integer, default: 0
4
+
5
+ has_many :posts, inverse_of: :genre
6
+ end
@@ -1772,15 +1772,22 @@ describe Mongoid::Attributes do
1772
1772
 
1773
1773
  context 'when the attribute is localized' do
1774
1774
  let(:person) do
1775
- Person.create(desc: 'localized')
1775
+ Person.create
1776
1776
  end
1777
1777
 
1778
- before do
1779
- person.desc = nil
1778
+ context 'after initialization when the field is nil' do
1779
+
1780
+ it 'returns false' do
1781
+ expect(person.desc?).to be(false)
1782
+ end
1780
1783
  end
1781
1784
 
1782
- it 'applies the localization when checking the attribute' do
1783
- expect(person.desc?).to be(false)
1785
+ context 'when setting the field to nil' do
1786
+
1787
+ it 'applies the localization when checking the attribute' do
1788
+ person.desc = nil
1789
+ expect(person.desc?).to be(false)
1790
+ end
1784
1791
  end
1785
1792
 
1786
1793
  context 'when the field is a boolean' do
@@ -61,7 +61,8 @@ describe Mongoid::Copyable do
61
61
  end
62
62
 
63
63
  before do
64
- Actor.collection.find(_id: actor.id).update_one("$set" => { "this_is_not_a_field" => 1 })
64
+ legacy_fields = { "this_is_not_a_field" => 1, "this_legacy_field_is_nil" => nil }
65
+ Actor.collection.find(_id: actor.id).update_one("$set" => legacy_fields)
65
66
  end
66
67
 
67
68
  let(:cloned) do
@@ -72,7 +73,11 @@ describe Mongoid::Copyable do
72
73
  expect(cloned.attributes['this_is_not_a_field']).to eq(1)
73
74
  end
74
75
 
75
- it "copies the known attriutes" do
76
+ it "contains legacy attributes that are nil" do
77
+ expect(cloned.attributes.key?('this_legacy_field_is_nil')).to eq(true)
78
+ end
79
+
80
+ it "copies the known attributes" do
76
81
  expect(cloned.name).to eq('test')
77
82
  end
78
83
  end
@@ -70,6 +70,51 @@ describe Mongoid::Criteria::Queryable::Selector do
70
70
  end
71
71
  end
72
72
 
73
+ context "when selector contains a $in" do
74
+
75
+ let(:initial) do
76
+ { "$in" => [1, 2] }
77
+ end
78
+
79
+ before do
80
+ selector["field"] = initial
81
+ end
82
+
83
+ context "when merging in a new $in with an intersecting value" do
84
+
85
+ let(:other) do
86
+ { "field" => { "$in" => [1] } }
87
+ end
88
+
89
+ before do
90
+ selector.merge!(other)
91
+ end
92
+
93
+ it "intersects the $in values" do
94
+ expect(selector).to eq({
95
+ "field" => { "$in" => [1] }
96
+ })
97
+ end
98
+ end
99
+
100
+ context "when merging in a new $in with no intersecting values" do
101
+
102
+ let(:other) do
103
+ { "field" => { "$in" => [3] } }
104
+ end
105
+
106
+ before do
107
+ selector.merge!(other)
108
+ end
109
+
110
+ it "intersects the $in values" do
111
+ expect(selector).to eq({
112
+ "field" => { "$in" => [] }
113
+ })
114
+ end
115
+ end
116
+ end
117
+
73
118
  context "when selector is not nested" do
74
119
 
75
120
  before do
@@ -1148,6 +1148,29 @@ describe Mongoid::Criteria do
1148
1148
  end
1149
1149
  end
1150
1150
 
1151
+ context "when providing one association" do
1152
+
1153
+ let!(:user) do
1154
+ User.create(posts: [ post1 ])
1155
+ end
1156
+
1157
+ let!(:post1) do
1158
+ Post.create
1159
+ end
1160
+
1161
+ let(:result) do
1162
+ User.includes(:posts).first
1163
+ end
1164
+
1165
+ it "executes the query" do
1166
+ expect(result).to eq(user)
1167
+ end
1168
+
1169
+ it "includes the related objects" do
1170
+ expect(result.posts).to eq([ post1 ])
1171
+ end
1172
+ end
1173
+
1151
1174
  context "when providing a list of associations" do
1152
1175
 
1153
1176
  let!(:user) do
@@ -1505,13 +1528,6 @@ describe Mongoid::Criteria do
1505
1528
  end
1506
1529
  end
1507
1530
 
1508
- it "does not eager load the first document" do
1509
- doc = criteria.first
1510
- expect_query(1) do
1511
- expect(doc.person).to eq(person)
1512
- end
1513
- end
1514
-
1515
1531
  it "returns the last document" do
1516
1532
  expect(document).to eq(post_two)
1517
1533
  end
@@ -1567,13 +1583,6 @@ describe Mongoid::Criteria do
1567
1583
  end
1568
1584
  end
1569
1585
 
1570
- it "does not eager load the last document" do
1571
- doc = criteria.last
1572
- expect_query(1) do
1573
- expect(doc.band).to eq(tool)
1574
- end
1575
- end
1576
-
1577
1586
  it "returns the document" do
1578
1587
  expect(document).to eq(address_one)
1579
1588
  end
@@ -1599,13 +1608,6 @@ describe Mongoid::Criteria do
1599
1608
  end
1600
1609
  end
1601
1610
 
1602
- it "does not eager load the first document" do
1603
- doc = criteria.first
1604
- expect_query(1) do
1605
- expect(doc.band).to eq(depeche)
1606
- end
1607
- end
1608
-
1609
1611
  it "returns the document" do
1610
1612
  expect(document).to eq(address_two)
1611
1613
  end
@@ -1699,7 +1701,7 @@ describe Mongoid::Criteria do
1699
1701
  end
1700
1702
 
1701
1703
  before do
1702
- expect(new_context).to receive(:eager_load_one).with(person).once.and_call_original
1704
+ expect(new_context).to receive(:eager_load).with([person]).once.and_call_original
1703
1705
  end
1704
1706
 
1705
1707
  let!(:from_db) do
@@ -1750,7 +1752,7 @@ describe Mongoid::Criteria do
1750
1752
  end
1751
1753
 
1752
1754
  before do
1753
- expect(context).to receive(:eager_load_one).with(person).once.and_call_original
1755
+ expect(context).to receive(:eager_load).with([person]).once.and_call_original
1754
1756
  end
1755
1757
 
1756
1758
  let!(:from_db) do
@@ -2129,7 +2131,7 @@ describe Mongoid::Criteria do
2129
2131
  end
2130
2132
 
2131
2133
  before do
2132
- expect(context).to receive(:eager_load).with([ game_one, game_two ]).once.and_call_original
2134
+ expect(context).to receive(:preload).twice.and_call_original
2133
2135
  end
2134
2136
 
2135
2137
  let!(:documents) do
@@ -2192,7 +2194,7 @@ describe Mongoid::Criteria do
2192
2194
  end
2193
2195
 
2194
2196
  before do
2195
- expect(context).to receive(:eager_load).with([ person ]).once.and_call_original
2197
+ expect(context).to receive(:preload).twice.and_call_original
2196
2198
  end
2197
2199
 
2198
2200
  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
@@ -412,6 +412,18 @@ describe Mongoid::Relations::CounterCache do
412
412
  it "updates the original object's counter cache" do
413
413
  expect(person1.reload.drugs_count).to eq(1)
414
414
  end
415
+
416
+ context 'when foreign_key differs from model name' do
417
+
418
+ let(:genre) { PostGenre.create }
419
+
420
+ let(:post) { Post.create }
421
+
422
+ it 'updates correct counter cache' do
423
+ post.update post_genre: genre
424
+ expect(genre.reload.posts_count).to eq 1
425
+ end
426
+ end
415
427
  end
416
428
  end
417
429
 
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: 6.0.0
4
+ version: 6.0.1
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-09-21 00:00:00.000000000 Z
33
+ date: 2016-10-19 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: activemodel
@@ -509,6 +509,7 @@ files:
509
509
  - spec/app/models/pizza.rb
510
510
  - spec/app/models/player.rb
511
511
  - spec/app/models/post.rb
512
+ - spec/app/models/post_genre.rb
512
513
  - spec/app/models/powerup.rb
513
514
  - spec/app/models/preference.rb
514
515
  - spec/app/models/princess.rb
@@ -999,6 +1000,7 @@ test_files:
999
1000
  - spec/app/models/pizza.rb
1000
1001
  - spec/app/models/player.rb
1001
1002
  - spec/app/models/post.rb
1003
+ - spec/app/models/post_genre.rb
1002
1004
  - spec/app/models/powerup.rb
1003
1005
  - spec/app/models/preference.rb
1004
1006
  - spec/app/models/princess.rb
metadata.gz.sig CHANGED
Binary file