mongoid 5.1.1 → 5.1.2

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: ca6e4c501995a1483885dd73ee311c6f1ef818db
4
- data.tar.gz: 7637900bde88e2c32c0b40e4f6ad09b82f1fd409
3
+ metadata.gz: 922955f679b61344863a31c45286a9e6a98fef4c
4
+ data.tar.gz: 70619f1566afa0b012a1125fd19532cb8e7ab7b1
5
5
  SHA512:
6
- metadata.gz: a471975f86db1e3f63fca5267f86722946ca19352445e22003b2181b279dfce7510d141037da8f14ccd4f81b8477dcecc07772ea61fbfe424f412ba637aabf0d
7
- data.tar.gz: 42232d489eb95693bc6e0fcc49fa84b19e7b1ce5365b3a16fb2c3e676019208b50b3791139463a3d7d60089c1d7ae5ca1554046427a4f177c7feb06b861ec80c
6
+ metadata.gz: 200a3e9686b108018cc53c8278333f71ad8aff307a730d7e5ab5c09d1849b56a34dfd3e59f48af397314d2b936261ed4d482a26ca5d83e8f0b1bc7ebb9b962eb
7
+ data.tar.gz: c4daaaa77a96197384841ba4849621a63b9ea8add033cd691ff3323285d0221ffec05dce0024e8b59f012f1e0a56548da40158b5c76184b1baa1d9333a853cfa
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -374,7 +374,8 @@ module Mongoid
374
374
  return {} unless atomic_updates.key?("$set")
375
375
  touches = {}
376
376
  updates["$set"].each_pair do |key, value|
377
- touches.merge!({ key => value }) if key =~ /updated_at|#{field}/
377
+ key_regex = /updated_at|u_at#{"|" + field if field.present?}/
378
+ touches.merge!({ key => value }) if key =~ key_regex
378
379
  end
379
380
  { "$set" => touches }
380
381
  end
@@ -259,7 +259,7 @@ module Mongoid
259
259
  if field && field.localized?
260
260
  selection.key?("#{name}.#{::I18n.locale}")
261
261
  else
262
- selection.key?(name)
262
+ selection.keys.collect { |k| k.partition('.').first }.include?(name)
263
263
  end
264
264
  end
265
265
 
@@ -345,5 +345,12 @@ module Mongoid
345
345
  end
346
346
  end
347
347
  end
348
+
349
+ def lookup_attribute_presence(name, value)
350
+ if localized_fields.has_key?(name)
351
+ value = localized_fields[name].send(:lookup, value)
352
+ end
353
+ !!value
354
+ end
348
355
  end
349
356
  end
@@ -173,7 +173,7 @@ module Mongoid
173
173
  # @since 3.0.0
174
174
  def create_document(method, attrs = nil, &block)
175
175
  attributes = selector.reduce(attrs ? attrs.dup : {}) do |hash, (key, value)|
176
- unless key.to_s =~ /\$/ || value.is_a?(Hash)
176
+ unless key.to_s =~ /\$/ || (value.is_a?(Hash) && !attribute_names.include?(key))
177
177
  hash[key] = value
178
178
  end
179
179
  hash
@@ -480,8 +480,8 @@ module Mongoid
480
480
  def create_field_check(name, meth)
481
481
  generated_methods.module_eval do
482
482
  re_define_method("#{meth}?") do
483
- attr = read_attribute(name)
484
- attr == true || attr.present?
483
+ value = read_attribute(name)
484
+ lookup_attribute_presence(name, value)
485
485
  end
486
486
  end
487
487
  end
@@ -22,9 +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
- field_and_value_hash = hasherizer(field.split('.'), value)
26
- field = field_and_value_hash.keys.first
27
- process_attribute(field, field_and_value_hash[field])
25
+ process_attribute(field.to_s, value)
28
26
  ops[atomic_attribute_name(field)] = attributes[field]
29
27
  end
30
28
  { "$set" => ops }
@@ -34,7 +34,7 @@ module Mongoid
34
34
  #
35
35
  # @since 2.0.0.rc.7
36
36
  def convert(object)
37
- return object if metadata.polymorphic?
37
+ return convert_polymorphic(object) if metadata.polymorphic?
38
38
  klass, field = metadata.klass, metadata.klass.fields["_id"]
39
39
  if klass.using_object_ids?
40
40
  BSON::ObjectId.mongoize(object)
@@ -44,6 +44,12 @@ module Mongoid
44
44
  field.mongoize(object)
45
45
  end
46
46
  end
47
+
48
+ private
49
+
50
+ def convert_polymorphic(object)
51
+ object.respond_to?(:id) ? object.id : object
52
+ end
47
53
  end
48
54
  end
49
55
  end
@@ -29,7 +29,7 @@ module Mongoid
29
29
  write_attribute(field, current) if field
30
30
 
31
31
  touches = touch_atomic_updates(field)
32
- unless touches.empty?
32
+ unless touches["$set"].blank?
33
33
  selector = atomic_selector
34
34
  _root.collection.find(selector).update_one(positionally(selector, touches))
35
35
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- VERSION = "5.1.1"
3
+ VERSION = "5.1.2"
4
4
  end
@@ -39,6 +39,25 @@ describe Mongoid::Attributes do
39
39
  expect(from_db.desc).to eq("test")
40
40
  end
41
41
  end
42
+
43
+ context 'when the attribute is a hash field' do
44
+
45
+ before do
46
+ person.update_attribute(:map, map)
47
+ end
48
+
49
+ let(:map) do
50
+ { 'dates' => { 'y' => { '2016' => 'Berlin' } } }
51
+ end
52
+
53
+ let(:from_db) do
54
+ Person.only('map.dates.y.2016').first
55
+ end
56
+
57
+ it "does not raise an error" do
58
+ expect(from_db.map).to eq(map)
59
+ end
60
+ end
42
61
  end
43
62
 
44
63
  context "when the context excludes" do
@@ -1630,4 +1649,46 @@ describe Mongoid::Attributes do
1630
1649
  end
1631
1650
  end
1632
1651
  end
1652
+
1653
+ context 'when calling the attribute check method' do
1654
+
1655
+ context 'when the attribute is localized' do
1656
+ let(:person) do
1657
+ Person.create(desc: 'localized')
1658
+ end
1659
+
1660
+ before do
1661
+ person.desc = nil
1662
+ end
1663
+
1664
+ it 'applies the localization when checking the attribute' do
1665
+ expect(person.desc?).to be(false)
1666
+ end
1667
+
1668
+ context 'when the field is a boolean' do
1669
+
1670
+ before do
1671
+ person.desc = false
1672
+ end
1673
+
1674
+ it 'applies the localization when checking the attribute' do
1675
+ expect(person.desc?).to be(false)
1676
+ end
1677
+ end
1678
+ end
1679
+
1680
+ context 'when the attribute is not localized' do
1681
+ let(:person) do
1682
+ Person.create(username: 'localized')
1683
+ end
1684
+
1685
+ before do
1686
+ person.username = nil
1687
+ end
1688
+
1689
+ it 'does not apply localization when checking the attribute' do
1690
+ expect(person.name?).to be(false)
1691
+ end
1692
+ end
1693
+ end
1633
1694
  end
@@ -612,6 +612,17 @@ describe Mongoid::Criteria::Modifiable do
612
612
  end
613
613
  end
614
614
  end
615
+
616
+ context 'when the criteria selector includes a hash field' do
617
+
618
+ let(:document) do
619
+ Person.where(map: { foo: :bar }).first_or_create
620
+ end
621
+
622
+ it 'sets the hash field' do
623
+ expect(document.map).to eq({ foo: :bar })
624
+ end
625
+ end
615
626
  end
616
627
  end
617
628
 
@@ -736,6 +747,17 @@ describe Mongoid::Criteria::Modifiable do
736
747
  end
737
748
  end
738
749
  end
750
+
751
+ context 'when the criteria selector includes a hash field' do
752
+
753
+ let(:document) do
754
+ Person.where(map: { foo: :bar }).first_or_create
755
+ end
756
+
757
+ it 'sets the hash field' do
758
+ expect(document.map).to eq({ foo: :bar })
759
+ end
760
+ end
739
761
  end
740
762
  end
741
763
 
@@ -3349,6 +3349,21 @@ describe Mongoid::Criteria do
3349
3349
  expect(from_db).to eq(band)
3350
3350
  end
3351
3351
  end
3352
+
3353
+ context 'when querying on a polymorphic relation' do
3354
+
3355
+ let(:movie) do
3356
+ Movie.create
3357
+ end
3358
+
3359
+ let(:selector) do
3360
+ Rating.where(ratable: movie).selector
3361
+ end
3362
+
3363
+ it 'properly converts the object to an ObjectId' do
3364
+ expect(selector['ratable_id']).to eq(movie.id)
3365
+ end
3366
+ end
3352
3367
  end
3353
3368
  end
3354
3369
 
@@ -227,21 +227,4 @@ describe Mongoid::Persistable do
227
227
  }.to raise_error(Mongoid::Errors::Callback)
228
228
  end
229
229
  end
230
-
231
- example "able use set value using dot notation" do
232
- class Customer
233
- include Mongoid::Document
234
-
235
- field :address, type: Hash, default: -> {{}}
236
- end
237
-
238
- customer = Customer.new
239
- customer.save
240
- customer.set 'address.country' => 'India'
241
- expect(customer.address).to eql({ 'country' => 'India' })
242
- customer.set 'address.country.state' => 'MH'
243
- expect(customer.address).to eql({'country' => { 'state' => 'MH' }})
244
- customer.set 'address' => { 'country' => 'India'}
245
- expect(customer.address).to eql({ 'country' => 'India' })
246
- end
247
230
  end
@@ -329,5 +329,34 @@ describe Mongoid::Relations::Touchable do
329
329
  end
330
330
  end
331
331
  end
332
+
333
+ context "when other document attributes have been changed" do
334
+
335
+ let(:band) do
336
+ Band.create(name: "Placebo")
337
+ end
338
+
339
+ context "when an attribute is provided" do
340
+ before do
341
+ band.name = 'Nocebo'
342
+ band.touch(:last_release)
343
+ end
344
+
345
+ it "does not persist other attribute changes" do
346
+ expect(band.reload.name).not_to eq('Nocebo')
347
+ end
348
+ end
349
+
350
+ context "when an attribute is not provided" do
351
+ before do
352
+ band.name = 'Nocebo'
353
+ band.touch
354
+ end
355
+
356
+ it "does not persist other attribute changes" do
357
+ expect(band.reload.name).not_to eq('Nocebo')
358
+ end
359
+ end
360
+ end
332
361
  end
333
362
  end
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.1
4
+ version: 5.1.2
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-02-18 00:00:00.000000000 Z
33
+ date: 2016-03-31 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: activemodel
metadata.gz.sig CHANGED
Binary file