mongoid 8.0.2 → 8.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c2c1f57188d73b1d7f6d965b01a05cd97ddab316855221e6b23f70858e820af
4
- data.tar.gz: 0ad8309b683b8031796d3e3d99c36840481b7b2cc8aa773e32c4d1b050380671
3
+ metadata.gz: 684d53a067a1e3410535a0dc06db2b587c5e43a19c85af0defe7f29c81e1c74d
4
+ data.tar.gz: c285640352d90d219d7ccd6c0ce6c90fdf1b800f853d2aa44898025c9d49ccf8
5
5
  SHA512:
6
- metadata.gz: 00ca56917eafe55f9b4ef394569ac8a64023c67906fdd65522d74a95046a7cacab42ca11baea2917e80f803cadc9c7d484e170227fcdf48a14a8656e96639fdb
7
- data.tar.gz: 159b5215f615ca52777a81127e8312767d4e66adbb77e047d01769ed90281ef4959de4bda42e5ce2eadd9803e4a3d11f5d3d50c8da467241f3dd16bd90f69226
6
+ metadata.gz: 6a13a92d079784bfc190d1ebddff3f2bdd6f0d3d2fb0cee336856ba8e870199b83cc7c809d17cb62a913a64624630ffd397bfa45779503b107b97f82fb4be615
7
+ data.tar.gz: b887e765c135fe7018863908afabe9128007e85628cbcf11bc971cabf06a162833239da37d32293b1c3565838cf9ad14898fd91ac510a29bf37779b782411ee7
checksums.yaml.gz.sig CHANGED
Binary file
@@ -15,7 +15,7 @@ module Mongoid
15
15
  # plural model name.
16
16
  #
17
17
  # If new_record? - will append /new
18
- # If not - will append /id-updated_at.to_s(cache_timestamp_format)
18
+ # If not - will append /id-updated_at.to_formatted_s(cache_timestamp_format)
19
19
  # Without updated_at - will append /id
20
20
  #
21
21
  # This is usually called inside a cache() block
@@ -26,7 +26,7 @@ module Mongoid
26
26
  # @return [ String ] the string with or without updated_at
27
27
  def cache_key
28
28
  return "#{model_key}/new" if new_record?
29
- return "#{model_key}/#{_id}-#{updated_at.utc.to_s(cache_timestamp_format)}" if do_or_do_not(:updated_at)
29
+ return "#{model_key}/#{_id}-#{updated_at.utc.to_formatted_s(cache_timestamp_format)}" if do_or_do_not(:updated_at)
30
30
  "#{model_key}/#{_id}"
31
31
  end
32
32
  end
@@ -72,10 +72,14 @@ module Mongoid
72
72
  BSON::Decimal128.new(object)
73
73
  elsif object.numeric?
74
74
  BSON::Decimal128.new(object.to_s)
75
+ elsif !object.is_a?(String)
76
+ object.try(:to_d)
75
77
  end
76
78
  else
77
79
  if object.is_a?(BSON::Decimal128) || object.numeric?
78
80
  object.to_s
81
+ elsif !object.is_a?(String)
82
+ object.try(:to_d)
79
83
  end
80
84
  end
81
85
  end
@@ -37,8 +37,12 @@ module Mongoid
37
37
  # @return [ Float | nil ] The object mongoized or nil.
38
38
  def mongoize(object)
39
39
  return if object.blank?
40
- if object.numeric?
41
- object.to_f
40
+ if object.is_a?(String)
41
+ if object.numeric?
42
+ object.to_f
43
+ end
44
+ else
45
+ object.try(:to_f)
42
46
  end
43
47
  end
44
48
  alias :demongoize :mongoize
@@ -45,8 +45,12 @@ module Mongoid
45
45
  # @return [ Integer | nil ] The object mongoized or nil.
46
46
  def mongoize(object)
47
47
  return if object.blank?
48
- if object.numeric?
49
- object.to_i
48
+ if object.is_a?(String)
49
+ if object.numeric?
50
+ object.to_i
51
+ end
52
+ else
53
+ object.try(:to_i)
50
54
  end
51
55
  end
52
56
  alias :demongoize :mongoize
@@ -14,7 +14,9 @@ module Mongoid
14
14
  #
15
15
  # @return [ Object ] The value for the current locale.
16
16
  def demongoize(object)
17
- if object
17
+ return if object.nil?
18
+ case object
19
+ when Hash
18
20
  type.demongoize(lookup(object))
19
21
  end
20
22
  end
@@ -76,7 +78,10 @@ module Mongoid
76
78
  end
77
79
  return value unless value.nil?
78
80
  if fallbacks? && ::I18n.respond_to?(:fallbacks)
79
- object[::I18n.fallbacks[locale].map(&:to_s).find{ |loc| object.has_key?(loc) }]
81
+ fallback_key = ::I18n.fallbacks[locale].find do |loc|
82
+ object.key?(loc.to_s) || object.key?(loc)
83
+ end
84
+ object[fallback_key.to_s] || object[fallback_key]
80
85
  end
81
86
  end
82
87
  end
@@ -189,8 +189,7 @@ module Mongoid
189
189
  #
190
190
  # @return [ Mongoid::PersistenceContext ] The persistence context for the object.
191
191
  def set(object, options_or_context)
192
- key = "[mongoid][#{object.object_id}]:context"
193
- existing_context = Thread.current[key]
192
+ existing_context = get_context(object)
194
193
  existing_options = if existing_context
195
194
  existing_context.options
196
195
  else
@@ -201,7 +200,7 @@ module Mongoid
201
200
  end
202
201
  new_options = existing_options.merge(options_or_context)
203
202
  context = PersistenceContext.new(object, new_options)
204
- Thread.current[key] = context
203
+ store_context(object, context)
205
204
  end
206
205
 
207
206
  # Get the persistence context for a particular class or model instance.
@@ -213,7 +212,7 @@ module Mongoid
213
212
  #
214
213
  # @return [ Mongoid::PersistenceContext ] The persistence context for the object.
215
214
  def get(object)
216
- Thread.current["[mongoid][#{object.object_id}]:context"]
215
+ get_context(object)
217
216
  end
218
217
 
219
218
  # Clear the persistence context for a particular class or model instance.
@@ -232,7 +231,44 @@ module Mongoid
232
231
  end
233
232
  end
234
233
  ensure
235
- Thread.current["[mongoid][#{object.object_id}]:context"] = original_context
234
+ store_context(object, original_context)
235
+ end
236
+
237
+ private
238
+
239
+ # Key to store persistence contexts in the thread local storage.
240
+ #
241
+ # @api private
242
+ PERSISTENCE_CONTEXT_KEY = :"[mongoid]:persistence_context"
243
+
244
+ # Get the persistence context for a given object from the thread local
245
+ # storage.
246
+ #
247
+ # @param [ Object ] object Object to get the persistance context for.
248
+ #
249
+ # @return [ Mongoid::PersistenceContext | nil ] The persistence context
250
+ # for the object if previously stored, otherwise nil.
251
+ #
252
+ # @api private
253
+ def get_context(object)
254
+ Thread.current[PERSISTENCE_CONTEXT_KEY] ||= {}
255
+ Thread.current[PERSISTENCE_CONTEXT_KEY][object.object_id]
256
+ end
257
+
258
+ # Store persistence context for a given object in the thread local
259
+ # storage.
260
+ #
261
+ # @param [ Object ] object Object to store the persistance context for.
262
+ # @param [ Mongoid::PersistenceContext ] context Context to store
263
+ #
264
+ # @api private
265
+ def store_context(object, context)
266
+ if context.nil?
267
+ Thread.current[PERSISTENCE_CONTEXT_KEY]&.delete(object.object_id)
268
+ else
269
+ Thread.current[PERSISTENCE_CONTEXT_KEY] ||= {}
270
+ Thread.current[PERSISTENCE_CONTEXT_KEY][object.object_id] = context
271
+ end
236
272
  end
237
273
  end
238
274
  end
@@ -168,7 +168,9 @@ module Mongoid
168
168
  # Band.where(name: "Depeche Mode")
169
169
  # end
170
170
  #
171
- # @note This will force the default scope to be removed.
171
+ # @note This will force the default scope to be removed, but will not
172
+ # remove scopes declared with ``.with_scope``. This will be changed
173
+ # in Mongoid 9.
172
174
  #
173
175
  # @return [ Criteria | Object ] The unscoped criteria or result of the
174
176
  # block.
@@ -249,12 +251,12 @@ module Mongoid
249
251
  if Mongoid.scope_overwrite_exception
250
252
  raise Errors::ScopeOverwrite.new(self.name, name)
251
253
  else
252
- if Mongoid.logger
253
- Mongoid.logger.warn(
254
- "Creating scope :#{name}. " +
255
- "Overwriting existing method #{self.name}.#{name}."
256
- )
257
- end
254
+ Mongoid.logger.warn(
255
+ "Creating scope :#{name} which conflicts with #{self.name}.#{name}. " +
256
+ "Calls to `Mongoid::Criteria##{name}` will delegate to " +
257
+ "`Mongoid::Criteria##{name}` for criteria with klass #{self.name} " +
258
+ "and will ignore the declared scope."
259
+ )
258
260
  end
259
261
  end
260
262
  end
@@ -322,7 +322,7 @@ module Mongoid
322
322
  #
323
323
  # @param [ Mongo::Session ] session The session to save.
324
324
  def set_session(session)
325
- Thread.current[:session] = session
325
+ Thread.current["[mongoid]:session"] = session
326
326
  end
327
327
 
328
328
  # Get the cached session for this thread.
@@ -332,7 +332,7 @@ module Mongoid
332
332
  #
333
333
  # @return [ Mongo::Session | nil ] The session cached on this thread or nil.
334
334
  def get_session
335
- Thread.current[:session]
335
+ Thread.current["[mongoid]:session"]
336
336
  end
337
337
 
338
338
  # Clear the cached session for this thread.
@@ -344,7 +344,7 @@ module Mongoid
344
344
  def clear_session
345
345
  session = get_session
346
346
  session.end_session if session
347
- Thread.current[:session] = nil
347
+ Thread.current["[mongoid]:session"] = nil
348
348
  end
349
349
  end
350
350
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mongoid
4
- VERSION = "8.0.2"
4
+ VERSION = "8.0.3"
5
5
  end
@@ -3,19 +3,7 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe 'i18n fallbacks' do
6
- # These tests modify the environment
7
- before(:all) do
8
- unless %w(yes true 1).include?((ENV['TEST_I18N_FALLBACKS'] || '').downcase)
9
- skip 'Set TEST_I18N_FALLBACKS=1 environment variable to run these tests'
10
- end
11
- end
12
-
13
- before(:all) do
14
- puts "I18n version: #{I18n::VERSION}"
15
-
16
- require "i18n/backend/fallbacks"
17
- I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
18
- end
6
+ with_i18n_fallbacks
19
7
 
20
8
  context 'when fallbacks are enabled with a locale list' do
21
9
  before do
@@ -36,7 +24,6 @@ describe 'i18n fallbacks' do
36
24
  end
37
25
 
38
26
  context 'when translation is missing in active locale and present in fallback locale' do
39
-
40
27
  it 'falls back on default locale' do
41
28
  product = Product.new
42
29
  I18n.locale = :en
@@ -44,7 +31,6 @@ describe 'i18n fallbacks' do
44
31
  I18n.locale = :de
45
32
  product.description.should == 'Marvelous!'
46
33
  end
47
-
48
34
  end
49
35
 
50
36
  context 'when translation is missing in all locales' do
@@ -64,7 +50,6 @@ describe 'i18n fallbacks' do
64
50
  I18n.locale = :ru
65
51
  product.description.should be nil
66
52
  end
67
-
68
53
  end
69
54
 
70
55
  context 'i18n 1.0' do
@@ -82,7 +67,6 @@ describe 'i18n fallbacks' do
82
67
  I18n.locale = :ru
83
68
  product.description.should == 'Marvelous!'
84
69
  end
85
-
86
70
  end
87
71
  end
88
72
  end
@@ -45,7 +45,7 @@ describe Mongoid::Cacheable do
45
45
  context "with the default cache_timestamp_format" do
46
46
 
47
47
  let!(:updated_at) do
48
- document.updated_at.utc.to_s(:nsec)
48
+ document.updated_at.utc.to_formatted_s(:nsec)
49
49
  end
50
50
 
51
51
  it "has the id and updated_at key name" do
@@ -64,7 +64,7 @@ describe Mongoid::Cacheable do
64
64
  end
65
65
 
66
66
  let!(:updated_at) do
67
- document.updated_at.utc.to_s(:number)
67
+ document.updated_at.utc.to_formatted_s(:number)
68
68
  end
69
69
 
70
70
  it "has the id and updated_at key name" do
@@ -103,7 +103,7 @@ describe Mongoid::Cacheable do
103
103
  end
104
104
 
105
105
  let!(:updated_at) do
106
- agent.updated_at.utc.to_s(:nsec)
106
+ agent.updated_at.utc.to_formatted_s(:nsec)
107
107
  end
108
108
 
109
109
  it "has the id and updated_at key name" do
@@ -1042,6 +1042,31 @@ describe Mongoid::Clients do
1042
1042
  end
1043
1043
  end
1044
1044
  end
1045
+
1046
+ context 'when using on different objects' do
1047
+ require_mri
1048
+
1049
+ let(:first_band) do
1050
+ Band.create!(name: "The Beatles")
1051
+ end
1052
+
1053
+ let(:second_band) do
1054
+ Band.create!(name: 'Led Zeppelin')
1055
+ end
1056
+
1057
+ it 'does not create extra symbols symbols' do
1058
+ first_band.with(write: { w: 0, j: false }) do |band|
1059
+ band.set(active: false)
1060
+ end
1061
+ initial_symbols_count = Symbol.all_symbols.size
1062
+ second_band.with(write: { w: 0, j: false }) do |band|
1063
+ band.set(active: false)
1064
+ end
1065
+ expect(Symbol.all_symbols.size).to eq(initial_symbols_count)
1066
+ end
1067
+
1068
+ end
1069
+
1045
1070
  end
1046
1071
 
1047
1072
  context "when overriding the default database" do
@@ -546,7 +546,7 @@ describe Mongoid::Contextual::Memory do
546
546
  end
547
547
 
548
548
  context 'when fallbacks are enabled with a locale list' do
549
- require_fallbacks
549
+ with_i18n_fallbacks
550
550
 
551
551
  around(:all) do |example|
552
552
  prev_fallbacks = I18n.fallbacks.dup
@@ -565,7 +565,7 @@ describe Mongoid::Contextual::Memory do
565
565
 
566
566
  it "correctly uses the fallback" do
567
567
  I18n.locale = :en
568
- d = Dictionary.create!(description: 'english-text')
568
+ Dictionary.create!(description: 'english-text')
569
569
  I18n.locale = :he
570
570
  distinct.should == "english-text"
571
571
  end
@@ -1598,7 +1598,7 @@ describe Mongoid::Contextual::Memory do
1598
1598
  end
1599
1599
 
1600
1600
  context 'when fallbacks are enabled with a locale list' do
1601
- require_fallbacks
1601
+ with_i18n_fallbacks
1602
1602
 
1603
1603
  around(:all) do |example|
1604
1604
  prev_fallbacks = I18n.fallbacks.dup
@@ -1617,7 +1617,7 @@ describe Mongoid::Contextual::Memory do
1617
1617
 
1618
1618
  it "correctly uses the fallback" do
1619
1619
  I18n.locale = :en
1620
- d = Dictionary.create!(description: 'english-text')
1620
+ Dictionary.create!(description: 'english-text')
1621
1621
  I18n.locale = :he
1622
1622
  plucked.should == "english-text"
1623
1623
  end
@@ -1960,7 +1960,6 @@ describe Mongoid::Contextual::Memory do
1960
1960
  address2b.name = "de3"
1961
1961
  person1
1962
1962
  person2
1963
-
1964
1963
  I18n.locale = :en
1965
1964
  end
1966
1965
 
@@ -660,7 +660,7 @@ describe Mongoid::Contextual::Mongo do
660
660
  end
661
661
 
662
662
  context 'when fallbacks are enabled with a locale list' do
663
- require_fallbacks
663
+ with_i18n_fallbacks
664
664
 
665
665
  around(:all) do |example|
666
666
  prev_fallbacks = I18n.fallbacks.dup
@@ -686,7 +686,7 @@ describe Mongoid::Contextual::Mongo do
686
686
 
687
687
  it "correctly uses the fallback" do
688
688
  I18n.locale = :en
689
- d = Dictionary.create!(description: 'english-text')
689
+ Dictionary.create!(description: 'english-text')
690
690
  I18n.locale = :he
691
691
  distinct.should == "english-text"
692
692
  end
@@ -854,7 +854,6 @@ describe Mongoid::Contextual::Mongo do
854
854
  d2.save!
855
855
  d3.save!
856
856
  d4.save!
857
-
858
857
  I18n.locale = :en
859
858
  end
860
859
 
@@ -908,7 +907,6 @@ describe Mongoid::Contextual::Mongo do
908
907
  address2b.name = "de3"
909
908
  Person.create!(addresses: [ address1a, address1b ])
910
909
  Person.create!(addresses: [ address2a, address2b ])
911
-
912
910
  I18n.locale = :en
913
911
  end
914
912
 
@@ -171,7 +171,6 @@ describe Mongoid::Criteria do
171
171
 
172
172
  let(:dictionary) do
173
173
  Dictionary.only(:description).first
174
-
175
174
  end
176
175
 
177
176
  it 'loads all translations' do
@@ -2059,7 +2059,7 @@ describe Mongoid::Criteria do
2059
2059
  end
2060
2060
 
2061
2061
  context 'when fallbacks are enabled with a locale list' do
2062
- require_fallbacks
2062
+ with_i18n_fallbacks
2063
2063
 
2064
2064
  around(:all) do |example|
2065
2065
  prev_fallbacks = I18n.fallbacks.dup
@@ -273,6 +273,21 @@ describe Mongoid::Extensions::BigDecimal do
273
273
  end
274
274
  end
275
275
 
276
+ context "when the value is castable" do
277
+
278
+ let(:value) do
279
+ 2.hours
280
+ end
281
+
282
+ before do
283
+ expect(value).to be_a(ActiveSupport::Duration)
284
+ end
285
+
286
+ it "returns nil" do
287
+ expect(mongoized).to eq(7200)
288
+ end
289
+ end
290
+
276
291
  context "when the value is nil" do
277
292
 
278
293
  let(:value) do
@@ -100,10 +100,10 @@ describe Mongoid::Extensions::Float do
100
100
  end
101
101
  end
102
102
 
103
- context "when the string is numerical" do
103
+ context "when the string starts with a number" do
104
104
 
105
- it "returns the float value for the string" do
106
- expect(Float.send(method, "3")).to eq(3)
105
+ it "returns nil" do
106
+ expect(Float.send(method, "42bogus")).to be_nil
107
107
  end
108
108
  end
109
109
 
@@ -120,6 +120,13 @@ describe Mongoid::Extensions::Float do
120
120
  expect(Float.send(method, nil)).to be_nil
121
121
  end
122
122
  end
123
+
124
+ context "when giving an object that is castable to an Float" do
125
+
126
+ it "returns the integer value" do
127
+ expect(Float.send(method, 2.hours)).to eq(7200)
128
+ end
129
+ end
123
130
  end
124
131
  end
125
132
  end
@@ -94,10 +94,10 @@ describe Mongoid::Extensions::Integer do
94
94
  end
95
95
  end
96
96
 
97
- context "when the string is numerical" do
97
+ context "when the string starts with a number" do
98
98
 
99
- it "returns the integer value for the string" do
100
- expect(Integer.send(method, "3")).to eq(3)
99
+ it "returns nil" do
100
+ expect(Integer.send(method, "42bogus")).to be_nil
101
101
  end
102
102
  end
103
103
 
@@ -114,6 +114,13 @@ describe Mongoid::Extensions::Integer do
114
114
  expect(Integer.send(method, nil)).to be_nil
115
115
  end
116
116
  end
117
+
118
+ context "when giving an object that is castable to an Integer" do
119
+
120
+ it "returns the integer value" do
121
+ expect(Integer.send(method, 2.hours)).to eq(7200)
122
+ end
123
+ end
117
124
  end
118
125
  end
119
126
  end
@@ -102,6 +102,29 @@ describe Mongoid::Fields::Localized do
102
102
  end
103
103
  end
104
104
 
105
+ context "when key is a symbol" do
106
+
107
+ let(:value) do
108
+ field.demongoize({ :de => "This is a test" })
109
+ end
110
+
111
+ it "returns the string from the set locale" do
112
+ expect(value).to eq("This is a test")
113
+ end
114
+ end
115
+
116
+
117
+ context "passing a bogus value" do
118
+
119
+ let(:value) do
120
+ field.demongoize("bogus")
121
+ end
122
+
123
+ it "returns nil" do
124
+ expect(value).to be_nil
125
+ end
126
+ end
127
+
105
128
  context "when the value does not exist" do
106
129
 
107
130
  context "when not using fallbacks" do
@@ -117,10 +140,7 @@ describe Mongoid::Fields::Localized do
117
140
 
118
141
  context "when using fallbacks" do
119
142
 
120
- before(:all) do
121
- require "i18n/backend/fallbacks"
122
- I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
123
- end
143
+ with_i18n_fallbacks
124
144
 
125
145
  context "when fallbacks are defined" do
126
146
 
@@ -139,6 +159,17 @@ describe Mongoid::Fields::Localized do
139
159
  end
140
160
  end
141
161
 
162
+ context "when the fallback translation exists and is a symbol" do
163
+
164
+ let(:value) do
165
+ field.demongoize({ :es => "testing" })
166
+ end
167
+
168
+ it "returns the fallback translation" do
169
+ expect(value).to eq("testing")
170
+ end
171
+ end
172
+
142
173
  context "when another fallback translation exists" do
143
174
 
144
175
  let(:value) do
@@ -275,10 +306,7 @@ describe Mongoid::Fields::Localized do
275
306
 
276
307
  context "when using fallbacks" do
277
308
 
278
- before(:all) do
279
- require "i18n/backend/fallbacks"
280
- I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
281
- end
309
+ with_i18n_fallbacks
282
310
 
283
311
  context "when fallbacks are defined" do
284
312
 
@@ -476,10 +504,7 @@ describe Mongoid::Fields::Localized do
476
504
 
477
505
  context "when fallbacks are defined" do
478
506
 
479
- before(:all) do
480
- require "i18n/backend/fallbacks"
481
- I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
482
- end
507
+ with_i18n_fallbacks
483
508
 
484
509
  context "when the lookup does not need to use fallbacks" do
485
510
 
@@ -2476,7 +2476,6 @@ describe Mongoid::Validatable::UniquenessValidator do
2476
2476
  describe "i18n" do
2477
2477
 
2478
2478
  context 'when using a different locale' do
2479
-
2480
2479
  around do |example|
2481
2480
  I18n.with_locale(:fr) { example.run }
2482
2481
  end
@@ -2,6 +2,10 @@
2
2
 
3
3
  module Mongoid
4
4
  module Macros
5
+ class I18nBackendWithFallbacks < I18n::Backend::Simple
6
+ include I18n::Backend::Fallbacks
7
+ end
8
+
5
9
  def use_spec_mongoid_config
6
10
  around do |example|
7
11
  config_path = File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
@@ -39,6 +43,18 @@ module Mongoid
39
43
  end
40
44
  end
41
45
 
46
+ def with_i18n_fallbacks
47
+ require_fallbacks
48
+
49
+ around do |example|
50
+ old_backend = I18n.backend
51
+ I18n.backend = I18nBackendWithFallbacks.new
52
+ example.run
53
+ ensure
54
+ I18n.backend = old_backend
55
+ end
56
+ end
57
+
42
58
  def driver_config_override(key, value)
43
59
  around do |example|
44
60
  existing = Mongo.send(key)
data.tar.gz.sig CHANGED
Binary file