shopify-money 0.15.0 → 1.0.1.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -30,13 +30,18 @@ end
30
30
 
31
31
  class MoneyWithDelegatedCurrency < ActiveRecord::Base
32
32
  self.table_name = 'money_records'
33
- attr_accessor :delegated_record
34
33
  delegate :currency, to: :delegated_record
35
34
  money_column :price, currency_column: 'currency', currency_read_only: true
36
35
  money_column :prix, currency_column: 'currency2', currency_read_only: true
37
36
  def currency2
38
37
  delegated_record.currency
39
38
  end
39
+
40
+ private
41
+
42
+ def delegated_record
43
+ MoneyRecord.new(currency: 'USD')
44
+ end
40
45
  end
41
46
 
42
47
  class MoneyWithCustomAccessors < ActiveRecord::Base
@@ -97,11 +102,13 @@ RSpec.describe 'MoneyColumn' do
97
102
  end
98
103
 
99
104
  it 'returns money with null currency when the currency in the DB is invalid' do
100
- expect(Money).to receive(:deprecate).once
101
- record.update_columns(currency: 'invalid')
102
- record.reload
103
- expect(record.price.currency).to be_a(Money::NullCurrency)
104
- expect(record.price.value).to eq(1.23)
105
+ configure(legacy_deprecations: true) do
106
+ expect(Money).to receive(:deprecate).once
107
+ record.update_columns(currency: 'invalid')
108
+ record.reload
109
+ expect(record.price.currency).to be_a(Money::NullCurrency)
110
+ expect(record.price.value).to eq(1.23)
111
+ end
105
112
  end
106
113
 
107
114
  it 'handles legacy support for saving floats' do
@@ -114,12 +121,12 @@ RSpec.describe 'MoneyColumn' do
114
121
  expect(record.prix.currency.to_s).to eq('CAD')
115
122
  end
116
123
 
117
- it 'handles legacy support for saving floats with correct currency rounding' do
124
+ it 'handles legacy support for saving floats as provided' do
118
125
  record.update(price: 3.2112, prix: 3.2156)
119
- expect(record.attributes['price']).to eq(3.21)
126
+ expect(record.attributes['price']).to eq(3.2112)
120
127
  expect(record.price.value).to eq(3.21)
121
128
  expect(record.price.currency.to_s).to eq(currency)
122
- expect(record.attributes['prix']).to eq(3.22)
129
+ expect(record.attributes['prix']).to eq(3.2156)
123
130
  expect(record.prix.value).to eq(3.22)
124
131
  expect(record.prix.currency.to_s).to eq('CAD')
125
132
  end
@@ -165,8 +172,8 @@ RSpec.describe 'MoneyColumn' do
165
172
  describe 'garbage amount' do
166
173
  let(:amount) { 'foo' }
167
174
 
168
- it 'raises a deprecation warning' do
169
- expect { subject }.to raise_error(ActiveSupport::DeprecationException)
175
+ it 'raises an ArgumentError' do
176
+ expect { subject }.to raise_error(ArgumentError)
170
177
  end
171
178
  end
172
179
 
@@ -174,7 +181,7 @@ RSpec.describe 'MoneyColumn' do
174
181
  let(:currency) { 'foo' }
175
182
 
176
183
  it 'raises an UnknownCurrency error' do
177
- expect { subject }.to raise_error(ActiveSupport::DeprecationException)
184
+ expect { subject }.to raise_error(Money::Currency::UnknownCurrency)
178
185
  end
179
186
  end
180
187
 
@@ -217,11 +224,20 @@ RSpec.describe 'MoneyColumn' do
217
224
  describe 'read_only_currency true' do
218
225
  it 'does not write the currency to the db' do
219
226
  record = MoneyWithReadOnlyCurrency.create
220
- record.update_columns(price: 1, currency: 'USD')
221
- expect(Money).to receive(:deprecate).once
222
- record.update(price: Money.new(4, 'CAD'))
223
- expect(record.price.value).to eq(4)
224
- expect(record.price.currency.to_s).to eq('USD')
227
+ record.update_columns(currency: 'USD')
228
+ expect { record.update(price: Money.new(4, 'CAD')) }.to raise_error(MoneyColumn::CurrencyReadOnlyError)
229
+ end
230
+
231
+ it 'legacy_deprecations does not write the currency to the db' do
232
+ configure(legacy_deprecations: true) do
233
+ record = MoneyWithReadOnlyCurrency.create
234
+ record.update_columns(currency: 'USD')
235
+
236
+ expect(Money).to receive(:deprecate).once
237
+ record.update(price: Money.new(4, 'CAD'))
238
+ expect(record.price.value).to eq(4)
239
+ expect(record.price.currency.to_s).to eq('USD')
240
+ end
225
241
  end
226
242
 
227
243
  it 'reads the currency that is already in the db' do
@@ -233,12 +249,14 @@ RSpec.describe 'MoneyColumn' do
233
249
  end
234
250
 
235
251
  it 'reads an invalid currency from the db and generates a no currency object' do
236
- expect(Money).to receive(:deprecate).once
237
- record = MoneyWithReadOnlyCurrency.create
238
- record.update_columns(currency: 'invalid', price: 1)
239
- record.reload
240
- expect(record.price.value).to eq(1)
241
- expect(record.price.currency.to_s).to eq('')
252
+ configure(legacy_deprecations: true) do
253
+ expect(Money).to receive(:deprecate).once
254
+ record = MoneyWithReadOnlyCurrency.create
255
+ record.update_columns(currency: 'invalid', price: 1)
256
+ record.reload
257
+ expect(record.price.value).to eq(1)
258
+ expect(record.price.currency.to_s).to eq('')
259
+ end
242
260
  end
243
261
 
244
262
  it 'sets the currency correctly when the currency is changed' do
@@ -248,12 +266,12 @@ RSpec.describe 'MoneyColumn' do
248
266
  end
249
267
 
250
268
  it 'handle cases where the delegate allow_nil is false' do
251
- record = MoneyWithDelegatedCurrency.new(price: Money.new(10, 'USD'), delegated_record: MoneyRecord.new(currency: 'USD'))
269
+ record = MoneyWithDelegatedCurrency.new(price: Money.new(10, 'USD'))
252
270
  expect(record.price.currency.to_s).to eq('USD')
253
271
  end
254
272
 
255
273
  it 'handle cases where a manual delegate does not allow nil' do
256
- record = MoneyWithDelegatedCurrency.new(prix: Money.new(10, 'USD'), delegated_record: MoneyRecord.new(currency: 'USD'))
274
+ record = MoneyWithDelegatedCurrency.new(prix: Money.new(10, 'USD'))
257
275
  expect(record.price.currency.to_s).to eq('USD')
258
276
  end
259
277
  end
@@ -370,10 +388,7 @@ RSpec.describe 'MoneyColumn' do
370
388
 
371
389
  describe 'default_currency = nil' do
372
390
  around do |example|
373
- default_currency = Money.default_currency
374
- Money.default_currency = nil
375
- example.run
376
- Money.default_currency = default_currency
391
+ configure(default_currency: nil) { example.run }
377
392
  end
378
393
 
379
394
  it 'writes currency from input value to the db' do
@@ -384,11 +399,17 @@ RSpec.describe 'MoneyColumn' do
384
399
  expect(record.price.currency.to_s).to eq('GBP')
385
400
  end
386
401
 
387
- it 'raises missing currency error when input is not a money object' do
388
- record.update(currency: nil)
402
+ it 'raises missing currency error reading a value that was saved using legacy non-money object' do
403
+ record.update(currency: nil, price: 3)
404
+ expect { record.price }.to raise_error(ArgumentError, 'missing currency')
405
+ end
389
406
 
390
- expect { record.update(price: 3) }
391
- .to raise_error(ArgumentError, 'missing currency')
407
+ it 'handles legacy support for saving price and currency separately' do
408
+ record.update(currency: nil)
409
+ record.update(price: 7, currency: 'GBP')
410
+ record.reload
411
+ expect(record.price.value).to eq(7)
412
+ expect(record.price.currency.to_s).to eq('GBP')
392
413
  end
393
414
  end
394
415
  end
@@ -12,9 +12,11 @@ RSpec.describe MoneyParser do
12
12
  end
13
13
 
14
14
  it "parses an invalid string when not strict" do
15
- expect(Money).to receive(:deprecate).twice
16
- expect(@parser.parse("no money")).to eq(Money.new(0, Money::NULL_CURRENCY))
17
- expect(@parser.parse("1..")).to eq(Money.new(1))
15
+ configure(legacy_deprecations: true) do
16
+ expect(Money).to receive(:deprecate).twice
17
+ expect(@parser.parse("no money", 'USD')).to eq(Money.new(0, 'USD'))
18
+ expect(@parser.parse("1..", 'USD')).to eq(Money.new(1, 'USD'))
19
+ end
18
20
  end
19
21
 
20
22
  it "parses raise with an invalid string and strict option" do
@@ -22,6 +24,12 @@ RSpec.describe MoneyParser do
22
24
  expect { @parser.parse("1..1", strict: true) }.to raise_error(MoneyParser::MoneyFormatError)
23
25
  end
24
26
 
27
+ it "parses raise with an invalid when a currency is missing" do
28
+ configure do
29
+ expect { @parser.parse("1") }.to raise_error(Money::Currency::UnknownCurrency)
30
+ end
31
+ end
32
+
25
33
  it "parses a single digit integer string" do
26
34
  expect(@parser.parse("1")).to eq(Money.new(1.00))
27
35
  end
@@ -144,8 +152,10 @@ RSpec.describe MoneyParser do
144
152
  end
145
153
 
146
154
  it "parses amount with multiple inconsistent thousands delimiters" do
147
- expect(Money).to receive(:deprecate).once
148
- expect(@parser.parse("1.1.11.111")).to eq(Money.new(1_111_111))
155
+ configure(legacy_deprecations: true) do
156
+ expect(Money).to receive(:deprecate).once
157
+ expect(@parser.parse("1.1.11.111", 'USD')).to eq(Money.new(1_111_111, 'USD'))
158
+ end
149
159
  end
150
160
 
151
161
  it "parses raises with multiple inconsistent thousands delimiters and strict option" do
@@ -216,8 +226,10 @@ RSpec.describe MoneyParser do
216
226
  end
217
227
 
218
228
  it "parses amount with multiple inconsistent thousands delimiters" do
219
- expect(Money).to receive(:deprecate).once
220
- expect(@parser.parse("1,1,11,111")).to eq(Money.new(1_111_111))
229
+ configure(legacy_deprecations: true) do
230
+ expect(Money).to receive(:deprecate).once
231
+ expect(@parser.parse("1,1,11,111", 'USD')).to eq(Money.new(1_111_111, 'USD'))
232
+ end
221
233
  end
222
234
 
223
235
  it "parses raises with multiple inconsistent thousands delimiters and strict option" do
data/spec/money_spec.rb CHANGED
@@ -3,23 +3,20 @@ require 'spec_helper'
3
3
  require 'yaml'
4
4
 
5
5
  RSpec.describe "Money" do
6
-
7
6
  let (:money) { Money.new(1) }
8
7
  let (:amount_money) { Money.new(1.23, 'USD') }
9
8
  let (:non_fractional_money) { Money.new(1, 'JPY') }
10
9
  let (:zero_money) { Money.new(0) }
11
10
 
12
- context "default currency not set" do
13
- before(:each) do
14
- @default_currency = Money.default_currency
15
- Money.default_currency = nil
16
- end
17
- after(:each) do
18
- Money.default_currency = @default_currency
19
- end
11
+ it "has a version" do
12
+ expect(Money::VERSION).not_to(eq(nil))
13
+ end
20
14
 
15
+ context "default currency not set" do
21
16
  it "raises an error" do
22
- expect { money }.to raise_error(ArgumentError)
17
+ configure(default_currency: nil) do
18
+ expect { money }.to raise_error(ArgumentError)
19
+ end
23
20
  end
24
21
  end
25
22
 
@@ -39,9 +36,15 @@ RSpec.describe "Money" do
39
36
  expect(Money.new(1).to_money('CAD')).to eq(Money.new(1, 'CAD'))
40
37
  end
41
38
 
42
- it "#to_money doesn't overwrite the money object's currency" do
43
- expect(Money).to receive(:deprecate).once
44
- expect(Money.new(1, 'USD').to_money('CAD')).to eq(Money.new(1, 'USD'))
39
+ it "legacy_deprecations #to_money doesn't overwrite the money object's currency" do
40
+ configure(legacy_deprecations: true) do
41
+ expect(Money).to receive(:deprecate).once
42
+ expect(Money.new(1, 'USD').to_money('CAD')).to eq(Money.new(1, 'USD'))
43
+ end
44
+ end
45
+
46
+ it "#to_money raises when changing currency" do
47
+ expect{ Money.new(1, 'USD').to_money('CAD') }.to raise_error(Money::IncompatibleCurrencyError)
45
48
  end
46
49
 
47
50
  it "defaults to 0 when constructed with no arguments" do
@@ -52,9 +55,15 @@ RSpec.describe "Money" do
52
55
  expect(Money.new('')).to eq(Money.new(0))
53
56
  end
54
57
 
55
- it "defaults to 0 when constructed with an invalid string" do
56
- expect(Money).to receive(:deprecate).once
57
- expect(Money.new('invalid')).to eq(Money.new(0.00))
58
+ it "legacy_deprecations defaults to 0 when constructed with an invalid string" do
59
+ configure(legacy_deprecations: true) do
60
+ expect(Money).to receive(:deprecate).once
61
+ expect(Money.new('invalid', 'USD')).to eq(Money.new(0.00, 'USD'))
62
+ end
63
+ end
64
+
65
+ it "raises when constructed with an invalid string" do
66
+ expect{ Money.new('invalid') }.to raise_error(ArgumentError)
58
67
  end
59
68
 
60
69
  it "to_s correctly displays the right number of decimal places" do
@@ -62,14 +71,14 @@ RSpec.describe "Money" do
62
71
  expect(non_fractional_money.to_s).to eq("1")
63
72
  end
64
73
 
65
- it "to_s with a legacy_dollars style" do
66
- expect(amount_money.to_s(:legacy_dollars)).to eq("1.23")
67
- expect(non_fractional_money.to_s(:legacy_dollars)).to eq("1.00")
74
+ it "to_fs with a legacy_dollars style" do
75
+ expect(amount_money.to_fs(:legacy_dollars)).to eq("1.23")
76
+ expect(non_fractional_money.to_fs(:legacy_dollars)).to eq("1.00")
68
77
  end
69
78
 
70
- it "to_s with a amount style" do
71
- expect(amount_money.to_s(:amount)).to eq("1.23")
72
- expect(non_fractional_money.to_s(:amount)).to eq("1")
79
+ it "to_fs with a amount style" do
80
+ expect(amount_money.to_fs(:amount)).to eq("1.23")
81
+ expect(non_fractional_money.to_fs(:amount)).to eq("1")
73
82
  end
74
83
 
75
84
  it "to_s correctly displays negative numbers" do
@@ -95,12 +104,31 @@ RSpec.describe "Money" do
95
104
  expect(Money.new("999999999999999999.99", "USD").to_s).to eq("999999999999999999.99")
96
105
  end
97
106
 
98
- it "to_s raises ArgumentError on unsupported style" do
99
- expect{ money.to_s(:some_weird_style) }.to raise_error(ArgumentError)
107
+ it "to_fs raises ArgumentError on unsupported style" do
108
+ expect{ money.to_fs(:some_weird_style) }.to raise_error(ArgumentError)
109
+ end
110
+
111
+ it "to_fs is aliased as to_s for backward compatibility" do
112
+ expect(money.method(:to_s)).to eq(money.method(:to_fs))
113
+ end
114
+
115
+ it "to_fs is aliased as to_formatted_s for backward compatibility" do
116
+ expect(money.method(:to_formatted_s)).to eq(money.method(:to_fs))
117
+ end
118
+
119
+ it "legacy_json_format makes as_json return the legacy format" do
120
+ configure(legacy_json_format: true) do
121
+ expect(Money.new(1, 'CAD').as_json).to eq("1.00")
122
+ end
123
+ end
124
+
125
+ it "legacy_format correctly sets the json format" do
126
+ expect(Money.new(1, 'CAD').as_json(legacy_format: true)).to eq("1.00")
127
+ expect(Money.new(1, 'CAD').to_json(legacy_format: true)).to eq("1.00")
100
128
  end
101
129
 
102
- it "as_json as a float with 2 decimal places" do
103
- expect(money.as_json).to eq("1.00")
130
+ it "as_json as a json containing the value and currency" do
131
+ expect(money.as_json).to eq(value: "1.00", currency: "CAD")
104
132
  end
105
133
 
106
134
  it "is constructable with a BigDecimal" do
@@ -139,9 +167,15 @@ RSpec.describe "Money" do
139
167
  expect((Money.new + Money.new)).to eq(Money.new)
140
168
  end
141
169
 
142
- it "adds inconsistent currencies" do
143
- expect(Money).to receive(:deprecate).once
144
- expect(Money.new(5, 'USD') + Money.new(1, 'CAD')).to eq(Money.new(6, 'USD'))
170
+ it "legacy_deprecations adds inconsistent currencies" do
171
+ configure(legacy_deprecations: true) do
172
+ expect(Money).to receive(:deprecate).once
173
+ expect(Money.new(5, 'USD') + Money.new(1, 'CAD')).to eq(Money.new(6, 'USD'))
174
+ end
175
+ end
176
+
177
+ it "raises when adding inconsistent currencies" do
178
+ expect{ Money.new(5, 'USD') + Money.new(1, 'CAD') }.to raise_error(Money::IncompatibleCurrencyError)
145
179
  end
146
180
 
147
181
  it "is subtractable" do
@@ -157,8 +191,10 @@ RSpec.describe "Money" do
157
191
  end
158
192
 
159
193
  it "logs a deprecation warning when adding across currencies" do
160
- expect(Money).to receive(:deprecate)
161
- expect(Money.new(10, 'USD') - Money.new(1, 'JPY')).to eq(Money.new(9, 'USD'))
194
+ configure(legacy_deprecations: true) do
195
+ expect(Money).to receive(:deprecate)
196
+ expect(Money.new(10, 'USD') - Money.new(1, 'JPY')).to eq(Money.new(9, 'USD'))
197
+ end
162
198
  end
163
199
 
164
200
  it "keeps currency when doing a computation with a null currency" do
@@ -254,9 +290,15 @@ RSpec.describe "Money" do
254
290
  expect(((1.0 / 12) * Money.new(3.3))).to eq(Money.new(0.28))
255
291
  end
256
292
 
257
- it "is multipliable by a money object" do
258
- expect(Money).to receive(:deprecate).once
259
- expect((Money.new(3.3) * Money.new(1))).to eq(Money.new(3.3))
293
+ it "legacy_deprecations is multipliable by a money object" do
294
+ configure(legacy_deprecations: true) do
295
+ expect(Money).to receive(:deprecate).once
296
+ expect((Money.new(3.3, 'USD') * Money.new(1, 'USD'))).to eq(Money.new(3.3, 'USD'))
297
+ end
298
+ end
299
+
300
+ it "raises when multiplied by a money object" do
301
+ expect{ (Money.new(3.3) * Money.new(1)) }.to raise_error(ArgumentError)
260
302
  end
261
303
 
262
304
  it "rounds multiplication result with fractional penny of 5 or higher up" do
@@ -294,7 +336,14 @@ RSpec.describe "Money" do
294
336
  end
295
337
 
296
338
  it "returns cents in to_json" do
297
- expect(Money.new(1.00).to_json).to eq("1.00")
339
+ configure(legacy_json_format: true) do
340
+ expect(Money.new('1.23', 'USD').to_json).to eq('1.23')
341
+ end
342
+ end
343
+
344
+ it "returns value and currency in to_json" do
345
+ expect(Money.new(1.00).to_json).to eq('{"value":"1.00","currency":"CAD"}')
346
+ expect(JSON.dump(Money.new(1.00, "CAD"))).to eq('{"value":"1.00","currency":"CAD"}')
298
347
  end
299
348
 
300
349
  it "supports absolute value" do
@@ -352,9 +401,36 @@ RSpec.describe "Money" do
352
401
  end
353
402
 
354
403
  it "generates a true rational" do
355
- expect(Money.rational(Money.new(10.0), Money.new(15.0))).to eq(Rational(2,3))
356
- expect(Money).to receive(:deprecate).once
357
- expect(Money.rational(Money.new(10.0, 'USD'), Money.new(15.0, 'JPY'))).to eq(Rational(2,3))
404
+ expect(Money.rational(Money.new(10.0, 'USD'), Money.new(15.0, 'USD'))).to eq(Rational(2,3))
405
+
406
+ configure(legacy_deprecations: true) do
407
+ expect(Money).to receive(:deprecate).once
408
+ expect(Money.rational(Money.new(10.0, 'USD'), Money.new(15.0, 'JPY'))).to eq(Rational(2,3))
409
+ end
410
+ end
411
+
412
+ it "does not allocate a new money object when multiplying by 1" do
413
+ expect((money * 1).object_id).to eq(money.object_id)
414
+ end
415
+
416
+ it "does not allocate a new money object when adding 0" do
417
+ expect((money + 0).object_id).to eq(money.object_id)
418
+ end
419
+
420
+ it "does not allocate a new money object when subtracting 0" do
421
+ expect((money - 0).object_id).to eq(money.object_id)
422
+ end
423
+
424
+ it "does not allocate when computing absolute value when already positive" do
425
+ expect((money.abs).object_id).to eq(money.object_id)
426
+ end
427
+
428
+ it "does not allocate when computing floor value when already floored" do
429
+ expect((money.floor).object_id).to eq(money.object_id)
430
+ end
431
+
432
+ it "does not allocate when computing floor value when already rounded" do
433
+ expect((money.round).object_id).to eq(money.object_id)
358
434
  end
359
435
 
360
436
  describe "frozen with amount of $1" do
@@ -413,9 +489,11 @@ RSpec.describe "Money" do
413
489
  end
414
490
 
415
491
  it "<=> issues deprecation warning when comparing incompatible currency" do
416
- expect(Money).to receive(:deprecate).twice
417
- expect(Money.new(1000, 'USD') <=> Money.new(2000, 'JPY')).to eq(-1)
418
- expect(Money.new(2000, 'JPY') <=> Money.new(1000, 'USD')).to eq(1)
492
+ configure(legacy_deprecations: true) do
493
+ expect(Money).to receive(:deprecate).twice
494
+ expect(Money.new(1000, 'USD') <=> Money.new(2000, 'JPY')).to eq(-1)
495
+ expect(Money.new(2000, 'JPY') <=> Money.new(1000, 'USD')).to eq(1)
496
+ end
419
497
  end
420
498
 
421
499
  describe('same values') do
@@ -437,15 +515,22 @@ RSpec.describe "Money" do
437
515
  it { expect(cad_10 < nil_10).to(eq(false)) }
438
516
  end
439
517
 
440
- describe('different currencies') do
518
+ describe('legacy different currencies') do
519
+ around(:each) do |test|
520
+ configure(legacy_deprecations: true) { test.run }
521
+ end
522
+
441
523
  it { expect(Money).to(receive(:deprecate).once); expect(cad_10 <=> usd_10).to(eq(0)) }
442
524
  it { expect(Money).to(receive(:deprecate).once); expect(cad_10 > usd_10).to(eq(false)) }
443
525
  it { expect(Money).to(receive(:deprecate).once); expect(cad_10 >= usd_10).to(eq(true)) }
444
- it { expect(cad_10 == usd_10).to(eq(false)) }
445
526
  it { expect(Money).to(receive(:deprecate).once); expect(cad_10 <= usd_10).to(eq(true)) }
446
527
  it { expect(Money).to(receive(:deprecate).once); expect(cad_10 < usd_10).to(eq(false)) }
447
528
  end
448
529
 
530
+ describe('different currencies') do
531
+ it { expect(cad_10 == usd_10).to(eq(false)) }
532
+ end
533
+
449
534
  describe('to_money types') do
450
535
  it { expect(cad_10 <=> 10.00).to(eq(0)) }
451
536
  it { expect(cad_10 > 10.00).to(eq(false)) }
@@ -710,8 +795,7 @@ RSpec.describe "Money" do
710
795
  end
711
796
 
712
797
  describe "parser dependency injection" do
713
- before(:each) { Money.parser = AccountingMoneyParser }
714
- after(:each) { Money.parser = MoneyParser }
798
+ around(:each) { |test| configure(parser: AccountingMoneyParser, default_currency: 'CAD') { test.run }}
715
799
 
716
800
  it "keeps AccountingMoneyParser class on new money objects" do
717
801
  expect(Money.new.class.parser).to eq(AccountingMoneyParser)
@@ -875,8 +959,7 @@ RSpec.describe "Money" do
875
959
  end
876
960
 
877
961
  context "with .default_currency set" do
878
- before(:each) { Money.default_currency = Money::Currency.new('EUR') }
879
- after(:each) { Money.default_currency = Money::NULL_CURRENCY }
962
+ around(:each) { |test| configure(default_currency: Money::Currency.new('EUR')) { test.run }}
880
963
 
881
964
  it "can be nested and falls back to default_currency outside of the blocks" do
882
965
  money2, money3 = nil
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_spec_helper"
4
+
5
+ RSpec.describe Money::Rails::JobArgumentSerializer do
6
+ it "roundtrip a Money argument returns the same object" do
7
+ job = MoneyTestJob.new(value: Money.new(10.21, "BRL"))
8
+
9
+ serialized_job = job.serialize
10
+ serialized_value = serialized_job["arguments"][0]["value"]
11
+ expect(serialized_value["_aj_serialized"]).to eq("Money::Rails::JobArgumentSerializer")
12
+ expect(serialized_value["value"]).to eq(BigDecimal("10.21"))
13
+ expect(serialized_value["currency"]).to eq("BRL")
14
+
15
+ job2 = MoneyTestJob.deserialize(serialized_job)
16
+ job2.send(:deserialize_arguments_if_needed)
17
+
18
+ expect(job2.arguments.first[:value]).to eq(Money.new(10.21, "BRL"))
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_job"
4
+ require_relative "spec_helper"
5
+
6
+ Money::Railtie.initializers.each(&:run)
7
+
8
+ class MoneyTestJob < ActiveJob::Base
9
+ def perform(_params)
10
+ end
11
+ end
data/spec/spec_helper.rb CHANGED
@@ -68,3 +68,18 @@ RSpec::Matchers.define :quack_like do
68
68
  expected.instance_methods - actual.instance_methods
69
69
  end
70
70
  end
71
+
72
+
73
+ def configure(default_currency: nil, legacy_json_format: nil, legacy_deprecations: nil, legacy_default_currency: nil, parser: nil)
74
+ old_config = Money.config
75
+ Money.config = Money::Config.new.tap do |config|
76
+ config.default_currency = default_currency if default_currency
77
+ config.parser = parser if parser
78
+ config.legacy_json_format! if legacy_json_format
79
+ config.legacy_deprecations! if legacy_deprecations
80
+ config.legacy_default_currency! if legacy_default_currency
81
+ end
82
+ yield
83
+ ensure
84
+ Money.config = old_config
85
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopify-money
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.0
4
+ version: 1.0.1.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-12 00:00:00.000000000 Z
11
+ date: 2022-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -110,6 +110,7 @@ files:
110
110
  - LICENSE.txt
111
111
  - README.md
112
112
  - Rakefile
113
+ - UPGRADING.md
113
114
  - bin/console
114
115
  - config/currency_historic.yml
115
116
  - config/currency_iso.yml
@@ -118,6 +119,7 @@ files:
118
119
  - lib/money.rb
119
120
  - lib/money/accounting_money_parser.rb
120
121
  - lib/money/allocator.rb
122
+ - lib/money/config.rb
121
123
  - lib/money/core_extensions.rb
122
124
  - lib/money/currency.rb
123
125
  - lib/money/currency/loader.rb
@@ -127,8 +129,9 @@ files:
127
129
  - lib/money/money.rb
128
130
  - lib/money/money_parser.rb
129
131
  - lib/money/null_currency.rb
132
+ - lib/money/rails/job_argument_serializer.rb
133
+ - lib/money/railtie.rb
130
134
  - lib/money/version.rb
131
- - lib/money_accessor.rb
132
135
  - lib/money_column.rb
133
136
  - lib/money_column/active_record_hooks.rb
134
137
  - lib/money_column/active_record_type.rb
@@ -140,15 +143,17 @@ files:
140
143
  - money.gemspec
141
144
  - spec/accounting_money_parser_spec.rb
142
145
  - spec/allocator_spec.rb
146
+ - spec/config_spec.rb
143
147
  - spec/core_extensions_spec.rb
144
148
  - spec/currency/loader_spec.rb
145
149
  - spec/currency_spec.rb
146
150
  - spec/helpers_spec.rb
147
- - spec/money_accessor_spec.rb
148
151
  - spec/money_column_spec.rb
149
152
  - spec/money_parser_spec.rb
150
153
  - spec/money_spec.rb
151
154
  - spec/null_currency_spec.rb
155
+ - spec/rails/job_argument_serializer_spec.rb
156
+ - spec/rails_spec_helper.rb
152
157
  - spec/rubocop/cop/money/missing_currency_spec.rb
153
158
  - spec/rubocop/cop/money/zero_money_spec.rb
154
159
  - spec/rubocop_helper.rb
@@ -170,26 +175,28 @@ required_ruby_version: !ruby/object:Gem::Requirement
170
175
  version: '2.6'
171
176
  required_rubygems_version: !ruby/object:Gem::Requirement
172
177
  requirements:
173
- - - ">="
178
+ - - ">"
174
179
  - !ruby/object:Gem::Version
175
- version: '0'
180
+ version: 1.3.1
176
181
  requirements: []
177
- rubygems_version: 3.0.3
182
+ rubygems_version: 3.2.20
178
183
  signing_key:
179
184
  specification_version: 4
180
185
  summary: Shopify's money gem
181
186
  test_files:
182
187
  - spec/accounting_money_parser_spec.rb
183
188
  - spec/allocator_spec.rb
189
+ - spec/config_spec.rb
184
190
  - spec/core_extensions_spec.rb
185
191
  - spec/currency/loader_spec.rb
186
192
  - spec/currency_spec.rb
187
193
  - spec/helpers_spec.rb
188
- - spec/money_accessor_spec.rb
189
194
  - spec/money_column_spec.rb
190
195
  - spec/money_parser_spec.rb
191
196
  - spec/money_spec.rb
192
197
  - spec/null_currency_spec.rb
198
+ - spec/rails/job_argument_serializer_spec.rb
199
+ - spec/rails_spec_helper.rb
193
200
  - spec/rubocop/cop/money/missing_currency_spec.rb
194
201
  - spec/rubocop/cop/money/zero_money_spec.rb
195
202
  - spec/rubocop_helper.rb