-
Notifications
You must be signed in to change notification settings - Fork 388
/
Copy pathmonetizable_spec.rb
1085 lines (872 loc) · 40.8 KB
/
monetizable_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: utf-8
require 'spec_helper'
class Sub < Product; end
if defined? ActiveRecord
describe MoneyRails::ActiveRecord::Monetizable do
let(:product) do
Product.create(price_cents: 3000, discount: 150,
bonus_cents: 200, optional_price: 100,
sale_price_amount: 1200, delivery_fee_cents: 100,
restock_fee_cents: 2000,
reduced_price_cents: 1500, reduced_price_currency: :lvl,
lambda_price_cents: 4000)
end
describe ".monetize" do
let(:service) do
Service.create(charge_cents: 2000, discount_cents: 120)
end
def update_product(*attributes)
if defined?(::ActiveRecord::VERSION) && ::ActiveRecord::VERSION::MAJOR >= 5
product.update(*attributes)
else
product.update_attributes(*attributes)
end
end
context ".monetized_attributes" do
class InheritedMonetizeProduct < Product
monetize :special_price_cents
end
it "should be inherited by subclasses" do
assert_monetized_attributes(Sub.monetized_attributes, Product.monetized_attributes)
end
it "should be inherited by subclasses with new monetized attribute" do
assert_monetized_attributes(InheritedMonetizeProduct.monetized_attributes, Product.monetized_attributes.merge(special_price: "special_price_cents"))
end
def assert_monetized_attributes(monetized_attributes, expected_attributes)
expect(monetized_attributes).to include expected_attributes
expect(expected_attributes).to include monetized_attributes
expect(monetized_attributes.size).to eql expected_attributes.size
monetized_attributes.keys.each do |key|
expect(key.is_a? String).to be_truthy
end
end
end
it "attaches a Money object to model field" do
expect(product.price).to be_an_instance_of(Money)
expect(product.discount_value).to be_an_instance_of(Money)
expect(product.bonus).to be_an_instance_of(Money)
end
it "attaches Money objects to multiple model fields" do
expect(product.delivery_fee).to be_an_instance_of(Money)
expect(product.restock_fee).to be_an_instance_of(Money)
end
it "returns the expected money amount as a Money object" do
expect(product.price).to eq(Money.new(3000, "USD"))
end
it "assigns the correct value from a Money object" do
product.price = Money.new(3210, "USD")
expect(product.save).to be_truthy
expect(product.price_cents).to eq(3210)
end
it "assigns the correct value from a Money object using create" do
product = Product.create(price: Money.new(3210, "USD"), discount: 150,
bonus_cents: 200, optional_price: 100)
expect(product.valid?).to be_truthy
expect(product.price_cents).to eq(3210)
end
it "correctly updates from a Money object using update_attributes" do
expect(update_product(price: Money.new(215, "USD"))).to be_truthy
expect(product.price_cents).to eq(215)
end
it "assigns the correct value from params" do
params_clp = { amount: '20000', tax: '1000', currency: 'CLP' }
product = Transaction.create(params_clp)
expect(product.valid?).to be_truthy
expect(product.amount.currency.subunit_to_unit).to eq(1)
expect(product.amount_cents).to eq(20000)
end
# TODO: This is a slightly controversial example, btu it reflects the current behaviour
it "re-assigns cents amount when subunit/unit ratio changes preserving amount in units" do
transaction = Transaction.create(amount: '20000', tax: '1000', currency: 'USD')
expect(transaction.amount).to eq(Money.new(20000_00, 'USD'))
transaction.currency = 'CLP'
expect(transaction.amount).to eq(Money.new(20000, 'CLP'))
expect(transaction.amount_cents).to eq(20000)
end
it "raises an error if trying to create two attributes with the same name" do
expect do
class Product
monetize :discount, as: :price
end
end.to raise_error ArgumentError
end
it "raises an error if Money object has the same attribute name as the monetizable attribute" do
expect do
class AnotherProduct < Product
monetize :price_cents, as: :price_cents
end
end.to raise_error ArgumentError
end
it "raises an error when unable to infer attribute name" do
old_postfix = MoneyRails::Configuration.amount_column[:postfix]
MoneyRails::Configuration.amount_column[:postfix] = '_pennies'
expect do
class AnotherProduct < Product
monetize :price_cents
end
end.to raise_error ArgumentError
MoneyRails::Configuration.amount_column[:postfix] = old_postfix
end
it "allows subclass to redefine attribute with the same name" do
class SubProduct < Product
monetize :discount, as: :discount_price, with_currency: :gbp
end
sub_product = SubProduct.new(discount: 100)
expect(sub_product.discount_price).to be_an_instance_of(Money)
expect(sub_product.discount_price.currency.id).to equal :gbp
end
it "respects :as argument" do
expect(product.discount_value).to eq(Money.new(150, "USD"))
end
it "uses numericality validation" do
product.price_cents = "foo"
expect(product.save).to be_falsey
product.price_cents = 2000
expect(product.save).to be_truthy
end
it "skips numericality validation when disabled" do
product.accessor_price_cents = 'not_valid'
expect(product.save).to be_truthy
end
it "passes validation after updating fractional attribute which was previously invalid" do
product.price_in_a_range = -5
expect(product).not_to be_valid
product.price_in_a_range_cents = 500
expect(product).to be_valid
end
context "when MoneyRails.raise_error_on_money_parsing is true" do
before { MoneyRails.raise_error_on_money_parsing = true }
after { MoneyRails.raise_error_on_money_parsing = false }
it "raises exception when a String value with hyphen is assigned" do
expect { product.accessor_price = "10-235" }.to raise_error MoneyRails::Error
end
it "raises an exception if it can't change currency" do
expect {
Product.new.price = Money.new(10, "RUB")
}.to raise_error(MoneyRails::ActiveRecord::Monetizable::ReadOnlyCurrencyException, "Can't change readonly currency 'USD' to 'RUB' for field 'price'")
end
end
context "when MoneyRails.raise_error_on_money_parsing is false (default)" do
it "does not raise exception when a String value with hyphen is assigned" do
expect { product.accessor_price = "10-235" }.not_to raise_error
end
it "does not raise exception if it can't change currency" do
expect { Product.new.price = Money.new(10, "RUB") }.not_to raise_error
end
end
it "respects numericality validation when using update_attributes" do
expect(update_product(price_cents: "some text")).to be_falsey
expect(update_product(price_cents: 2000)).to be_truthy
end
it "uses numericality validation on money attribute" do
product.price = "some text"
expect(product.save).to be_falsey
product.price = Money.new(320, "USD")
expect(product.save).to be_truthy
product.sale_price = "12.34"
product.sale_price_currency_code = 'EUR'
expect(product.valid?).to be_truthy
end
it "separately skips price validations" do
product.skip_validation_price = 'hundred thousands'
expect(product.save).to be_truthy
end
it "separately skips subunit validations" do
product.skip_validation_price_cents = 'ten million'
expect(product.save).to be_truthy
end
it "shouldn't init empty key in errors" do
product.price = Money.new(320, "USD")
product.valid?
expect(product.errors.has_key?(:price)).to be_falsey
end
it "fails validation with the proper error message if money value is invalid decimal" do
product.price = "12.23.24"
expect(product.save).to be_falsey
expect(product.errors[:price].size).to eq(1)
expect(product.errors[:price].first).to match(/not a number/)
end
it "fails validation with the proper error message if money value is nothing but periods" do
product.price = "..."
expect(product.save).to be_falsey
expect(product.errors[:price].size).to eq(1)
expect(product.errors[:price].first).to match(/not a number/)
end
it "fails validation with the proper error message if money value has invalid thousands part" do
product.price = "12,23.24"
expect(product.save).to be_falsey
expect(product.errors[:price].size).to eq(1)
expect(product.errors[:price].first).to match(/has invalid format/)
expect(product.errors[:price].first).to match(/Got 12,23.24/)
end
it "fails validation with the proper error message if money value has thousand char after decimal mark" do
product.price = "1.234,56"
expect(product.save).to be_falsey
expect(product.errors[:price].size).to eq(1)
expect(product.errors[:price].first).to match(/has invalid format/)
expect(product.errors[:price].first).to match(/Got 1.234,56/)
end
it "allows an empty string as the thousands separator" do
begin
I18n.locale = 'en-US'
product.price = '10.00'
expect(product).to be_valid
ensure
I18n.locale = I18n.default_locale
end
end
it "passes validation if money value is a Float and the currency decimal mark is not period" do
# The corresponding String would be "12,34" euros
service.discount = 12.34
expect(service.save).to be_truthy
end
it "passes validation if money value is a Float" do
product.price = 12.34
expect(product.save).to be_truthy
end
it "passes validation if money value is an Integer" do
product.price = 12
expect(product.save).to be_truthy
end
it "fails validation with the proper error message using numericality validations" do
product.price_in_a_range = "-12"
expect(product.valid?).to be_falsey
expect(product.errors[:price_in_a_range].size).to eq(1)
expect(product.errors[:price_in_a_range].first).to match(/must be greater than zero and less than \$100/)
product.price_in_a_range = Money.new(-1200, "USD")
expect(product.valid?).to be_falsey
expect(product.errors[:price_in_a_range].size).to eq(1)
expect(product.errors[:price_in_a_range].first).to match(/must be greater than zero and less than \$100/)
product.price_in_a_range = "0"
expect(product.valid?).to be_falsey
expect(product.errors[:price_in_a_range].size).to eq(1)
expect(product.errors[:price_in_a_range].first).to match(/must be greater than zero and less than \$100/)
product.price_in_a_range = "12"
expect(product.valid?).to be_truthy
product.price_in_a_range = Money.new(1200, "USD")
expect(product.valid?).to be_truthy
product.price_in_a_range = "101"
expect(product.valid?).to be_falsey
expect(product.errors[:price_in_a_range].size).to eq(1)
expect(product.errors[:price_in_a_range].first).to match(/must be greater than zero and less than \$100/)
product.price_in_a_range = Money.new(10100, "USD")
expect(product.valid?).to be_falsey
expect(product.errors[:price_in_a_range].size).to eq(1)
expect(product.errors[:price_in_a_range].first).to match(/must be greater than zero and less than \$100/)
end
it "fails validation if linked attribute changed" do
product = Product.create(price: Money.new(3210, "USD"), discount: 150,
validates_method_amount: 100,
bonus_cents: 200, optional_price: 100)
expect(product.valid?).to be_truthy
product.optional_price = 50
expect(product.valid?).to be_falsey
end
it "fails validation with the proper error message using validates :money" do
product.validates_method_amount = "-12"
expect(product.valid?).to be_falsey
expect(product.errors[:validates_method_amount].size).to eq(1)
expect(product.errors[:validates_method_amount].first).to match(/must be greater than zero and less than \$100/)
product.validates_method_amount = Money.new(-1200, "USD")
expect(product.valid?).to be_falsey
expect(product.errors[:validates_method_amount].size).to eq(1)
expect(product.errors[:validates_method_amount].first).to match(/must be greater than zero and less than \$100/)
product.validates_method_amount = "0"
expect(product.valid?).to be_falsey
expect(product.errors[:validates_method_amount].size).to eq(1)
expect(product.errors[:validates_method_amount].first).to match(/must be greater than zero and less than \$100/)
product.validates_method_amount = "12"
expect(product.valid?).to be_truthy
product.validates_method_amount = Money.new(1200, "USD")
expect(product.valid?).to be_truthy
product.validates_method_amount = "101"
expect(product.valid?).to be_falsey
expect(product.errors[:validates_method_amount].size).to eq(1)
expect(product.errors[:validates_method_amount].first).to match(/must be greater than zero and less than \$100/)
product.validates_method_amount = Money.new(10100, "USD")
expect(product.valid?).to be_falsey
expect(product.errors[:validates_method_amount].size).to eq(1)
expect(product.errors[:validates_method_amount].first).to match(/must be greater than zero and less than \$100/)
end
it "fails validation with the proper error message on the cents field " do
product.price_in_a_range = "-12"
expect(product.valid?).to be_falsey
expect(product.errors[:price_in_a_range_cents].size).to eq(1)
expect(product.errors[:price_in_a_range_cents].first).to match(/greater than 0/)
product.price_in_a_range = "0"
expect(product.valid?).to be_falsey
expect(product.errors[:price_in_a_range_cents].size).to eq(1)
expect(product.errors[:price_in_a_range_cents].first).to match(/greater than 0/)
product.price_in_a_range = "12"
expect(product.valid?).to be_truthy
product.price_in_a_range = "101"
expect(product.valid?).to be_falsey
expect(product.errors[:price_in_a_range_cents].size).to eq(1)
expect(product.errors[:price_in_a_range_cents].first).to match(/less than or equal to 10000/)
end
it "fails validation when a non number string is given" do
product = Product.create(price_in_a_range: "asd")
expect(product.valid?).to be_falsey
expect(product.errors[:price_in_a_range].size).to eq(1)
expect(product.errors[:price_in_a_range].first).to match(/greater than zero/)
product = Product.create(price_in_a_range: "asd23")
expect(product.valid?).to be_falsey
expect(product.errors[:price_in_a_range].size).to eq(1)
expect(product.errors[:price_in_a_range].first).to match(/greater than zero/)
product = Product.create(price: "asd")
expect(product.valid?).to be_falsey
expect(product.errors[:price].size).to eq(1)
expect(product.errors[:price].first).to match(/is not a number/)
product = Product.create(price: "asd23")
expect(product.valid?).to be_falsey
expect(product.errors[:price].size).to eq(1)
expect(product.errors[:price].first).to match(/is not a number/)
end
it "passes validation when amount contains spaces (999 999.99)" do
product.price = "999 999.99"
expect(product).to be_valid
expect(product.price_cents).to eq(99999999)
end
it "passes validation when amount contains underscores (999_999.99)" do
product.price = "999_999.99"
expect(product).to be_valid
expect(product.price_cents).to eq(99999999)
end
it "passes validation if money value has correct format" do
product.price = "12,230.24"
expect(product.save).to be_truthy
end
it "passes validation if there is a whitespace between the currency symbol and amount" do
product.price = "$ 123,456.78"
expect(product.save).to be_truthy
end
it "respects numericality validation when using update_attributes on money attribute" do
expect(update_product(price: "some text")).to be_falsey
expect(update_product(price: Money.new(320, 'USD'))).to be_truthy
end
it "uses i18n currency format when validating" do
old_locale = I18n.locale
I18n.locale = "en-GB"
Money.default_currency = Money::Currency.find('EUR')
expect("12.00".to_money).to eq(Money.new(1200, :eur))
transaction = Transaction.new(amount: "12.00", tax: "13.00")
expect(transaction.amount_cents).to eq(1200)
expect(transaction.valid?).to be_truthy
# reset locale setting
I18n.locale = old_locale
end
it "doesn't allow nil by default" do
product.price_cents = nil
expect(product.save).to be_falsey
end
it "allows nil if optioned" do
product.optional_price = nil
expect(product.save).to be_truthy
expect(product.optional_price).to be_nil
end
it "doesn't raise exception if validation is used and nil is not allowed" do
expect { product.price = nil }.to_not raise_error
end
it "doesn't save nil values if validation is used and nil is not allowed" do
product.price = nil
product.save
expect(product.price_cents).not_to be_nil
end
it "resets money_before_type_cast attr every time a save operation occurs" do
v = Money.new(100, :usd)
product.price = v
expect(product.price_money_before_type_cast).to eq(v)
product.save
expect(product.price_money_before_type_cast).to be_nil
product.price = 10
expect(product.price_money_before_type_cast).to eq(10)
product.save
expect(product.price_money_before_type_cast).to be_nil
end
it "does not reset money_before_type_cast attr if save operation fails" do
product.bonus = ""
expect(product.bonus_money_before_type_cast).to eq("")
expect(product.save).to be_falsey
expect(product.bonus_money_before_type_cast).to eq("")
end
it "uses Money default currency if :with_currency has not been used" do
expect(service.discount.currency).to eq(Money::Currency.find(:eur))
end
it "overrides default currency with the currency registered for the model" do
expect(product.price.currency).to eq(Money::Currency.find(:usd))
end
it "overrides default currency with the value of :with_currency argument" do
expect(service.charge.currency).to eq(Money::Currency.find(:usd))
expect(product.bonus.currency).to eq(Money::Currency.find(:gbp))
end
it "uses currency postfix to determine attribute that stores currency" do
expect(product.reduced_price.currency).to eq(Money::Currency.find(:lvl))
end
it "correctly assigns Money objects to the attribute" do
product.price = Money.new(2500, :USD)
expect(product.save).to be_truthy
expect(product.price.cents).to eq(2500)
expect(product.price.currency.to_s).to eq("USD")
end
it "correctly assigns Fixnum objects to the attribute" do
product.price = 25
expect(product.save).to be_truthy
expect(product.price.cents).to eq(2500)
expect(product.price.currency.to_s).to eq("USD")
service.discount = 2
expect(service.save).to be_truthy
expect(service.discount.cents).to eq(200)
expect(service.discount.currency.to_s).to eq("EUR")
end
it "correctly assigns String objects to the attribute" do
product.price = "25"
expect(product.save).to be_truthy
expect(product.price.cents).to eq(2500)
expect(product.price.currency.to_s).to eq("USD")
service.discount = "2"
expect(service.save).to be_truthy
expect(service.discount.cents).to eq(200)
expect(service.discount.currency.to_s).to eq("EUR")
end
it "correctly assigns objects to a accessor attribute" do
product.accessor_price = 1.23
expect(product.save).to be_truthy
expect(product.accessor_price.cents).to eq(123)
expect(product.accessor_price_cents).to eq(123)
end
it "overrides default, model currency with the value of :with_currency in fixnum assignments" do
product.bonus = 25
expect(product.save).to be_truthy
expect(product.bonus.cents).to eq(2500)
expect(product.bonus.currency.to_s).to eq("GBP")
service.charge = 2
expect(service.save).to be_truthy
expect(service.charge.cents).to eq(200)
expect(service.charge.currency.to_s).to eq("USD")
end
it "overrides default, model currency with the value of :with_currency in string assignments" do
product.bonus = "25"
expect(product.save).to be_truthy
expect(product.bonus.cents).to eq(2500)
expect(product.bonus.currency.to_s).to eq("GBP")
service.charge = "2"
expect(service.save).to be_truthy
expect(service.charge.cents).to eq(200)
expect(service.charge.currency.to_s).to eq("USD")
product.lambda_price = "32"
expect(product.save).to be_truthy
expect(product.lambda_price.cents).to eq(3200)
expect(product.lambda_price.currency.to_s).to eq("CAD")
end
it "overrides default currency with model currency, in fixnum assignments" do
product.discount_value = 5
expect(product.save).to be_truthy
expect(product.discount_value.cents).to eq(500)
expect(product.discount_value.currency.to_s).to eq("USD")
end
it "overrides default currency with model currency, in string assignments" do
product.discount_value = "5"
expect(product.save).to be_truthy
expect(product.discount_value.cents).to eq(500)
expect(product.discount_value.currency.to_s).to eq("USD")
end
it "falls back to default currency, in fixnum assignments" do
service.discount = 5
expect(service.save).to be_truthy
expect(service.discount.cents).to eq(500)
expect(service.discount.currency.to_s).to eq("EUR")
end
it "falls back to default currency, in string assignments" do
service.discount = "5"
expect(service.save).to be_truthy
expect(service.discount.cents).to eq(500)
expect(service.discount.currency.to_s).to eq("EUR")
end
it "sets field to nil, in nil assignments if allow_nil is set" do
product.optional_price = nil
expect(product.save).to be_truthy
expect(product.optional_price).to be_nil
end
it "sets field to nil, in instantiation if allow_nil is set" do
pr = Product.new(optional_price: nil, price_cents: 5320,
discount: 350, bonus_cents: 320)
expect(pr.optional_price).to be_nil
expect(pr.save).to be_truthy
expect(pr.optional_price).to be_nil
end
it "sets field to nil, in blank assignments if allow_nil is set" do
product.optional_price = ""
expect(product.save).to be_truthy
expect(product.optional_price).to be_nil
end
context "when the monetized field is an aliased attribute" do
it "writes the subunits to the original (unaliased) column" do
pending if Rails::VERSION::MAJOR < 4
product.renamed = "$10.00"
expect(product.aliased_cents).to eq 10_00
end
end
context "for column with model currency:" do
it "has default currency if not specified" do
product = Product.create(sale_price_amount: 1234)
product.sale_price.currency.to_s == 'USD'
end
it "is overridden by instance currency column" do
product = Product.create(sale_price_amount: 1234,
sale_price_currency_code: 'CAD')
expect(product.sale_price.currency.to_s).to eq('CAD')
end
it 'can change currency of custom column' do
product = Product.create!(
price: Money.new(10,'USD'),
bonus: Money.new(10,'GBP'),
discount: 10,
sale_price_amount: 1234,
sale_price_currency_code: 'USD'
)
expect(product.sale_price.currency.to_s).to eq('USD')
product.sale_price = Money.new 456, 'CAD'
product.save
product.reload
expect(product.sale_price.currency.to_s).to eq('CAD')
expect(product.discount_value.currency.to_s).to eq('USD')
end
end
context "for model with currency column:" do
let(:transaction) do
Transaction.create(amount_cents: 2400, tax_cents: 600,
currency: :usd)
end
let(:dummy_product) do
DummyProduct.create(price_cents: 2400, currency: :usd)
end
let(:dummy_product_with_nil_currency) do
DummyProduct.create(price_cents: 2600) # nil currency
end
let(:dummy_product_with_invalid_currency) do
# invalid currency
DummyProduct.create(price_cents: 2600, currency: :foo)
end
it "correctly serializes the currency to a new instance of model" do
d = DummyProduct.new
d.price = Money.new(10, "EUR")
d.save!
d.reload
expect(d.currency).to eq("EUR")
end
it "overrides default currency with the value of row currency" do
expect(transaction.amount.currency).to eq(Money::Currency.find(:usd))
end
it "overrides default currency with the currency registered for the model" do
expect(dummy_product_with_nil_currency.price.currency).to eq(
Money::Currency.find(:gbp)
)
end
it "overrides default currency with the currency registered for the model if currency is invalid" do
expect(dummy_product_with_invalid_currency.price.currency).to eq(
Money::Currency.find(:gbp)
)
end
it "overrides default and model currency with the row currency" do
expect(dummy_product.price.currency).to eq(Money::Currency.find(:usd))
end
it "constructs the money attribute from the stored mapped attribute values" do
expect(transaction.amount).to eq(Money.new(2400, :usd))
end
it "correctly instantiates Money objects from the mapped attributes" do
t = Transaction.new(amount_cents: 2500, currency: "CAD")
expect(t.amount).to eq(Money.new(2500, "CAD"))
end
it "correctly assigns Money objects to the attribute" do
transaction.amount = Money.new(2500, :eur)
expect(transaction.save).to be_truthy
expect(transaction.amount.cents).to eq(Money.new(2500, :eur).cents)
expect(transaction.amount.currency.to_s).to eq("EUR")
end
it "uses default currency if a non Money object is assigned to the attribute" do
transaction.amount = 234
expect(transaction.amount.currency.to_s).to eq("USD")
end
it "constructs the money object from the mapped method value" do
expect(transaction.total).to eq(Money.new(3000, :usd))
end
it "constructs the money object from the mapped method value with arguments" do
expect(transaction.total(1, bar: 2)).to eq(Money.new(3003, :usd))
end
it "allows currency column postfix to be blank" do
allow(MoneyRails::Configuration).to receive(:currency_column) { { postfix: nil, column_name: 'currency' } }
expect(dummy_product_with_nil_currency.price.currency).to eq(Money::Currency.find(:gbp))
end
it "updates inferred currency column based on currency column postfix" do
product.reduced_price = Money.new(999_00, 'CAD')
product.save
expect(product.reduced_price_cents).to eq(999_00)
expect(product.reduced_price_currency).to eq('CAD')
end
context "and field with allow_nil: true" do
it "doesn't set currency to nil when setting the field to nil" do
t = Transaction.new(amount_cents: 2500, currency: "CAD")
t.optional_amount = nil
expect(t.currency).to eq("CAD")
end
end
# TODO: these specs should mock locale_backend with expected values
# instead of manipulating it directly
context "and an Italian locale" do
around(:each) do |example|
I18n.with_locale(:it) do
example.run
end
end
context "when using :i18n locale backend" do
it "validates with the locale's decimal mark" do
transaction.amount = "123,45"
expect(transaction.valid?).to be_truthy
end
it "does not validate with the currency's decimal mark" do
transaction.amount = "123.45"
expect(transaction.valid?).to be_falsey
end
it "validates with the locale's currency symbol" do
transaction.amount = "€123"
expect(transaction.valid?).to be_truthy
end
it "does not validate with the transaction's currency symbol" do
transaction.amount = "$123.45"
expect(transaction.valid?).to be_falsey
end
end
context "when using :currency locale backend" do
around(:each) do |example|
begin
Money.locale_backend = :currency
example.run
ensure
Money.locale_backend = :i18n
end
end
it "does not validate with the locale's decimal mark" do
transaction.amount = "123,45"
expect(transaction.valid?).to be_falsey
end
it "validates with the currency's decimal mark" do
transaction.amount = "123.45"
expect(transaction.valid?).to be_truthy
end
it "does not validate with the locale's currency symbol" do
transaction.amount = "€123"
expect(transaction.valid?).to be_falsey
end
it "validates with the transaction's currency symbol" do
transaction.amount = "$123"
expect(transaction.valid?).to be_truthy
end
end
end
end
end
describe ".register_currency" do
it "attaches currency at model level" do
expect(Product.currency).to eq(Money::Currency.find(:usd))
expect(DummyProduct.currency).to eq(Money::Currency.find(:gbp))
end
end
describe "#read_monetized" do
it "returns monetized attribute's value" do
reduced_price = product.read_monetized(:reduced_price, :reduced_price_cents)
expect(reduced_price).to be_an_instance_of(Money)
expect(reduced_price).to eq(Money.new(product.reduced_price_cents, product.reduced_price_currency))
end
context "memoize" do
it "memoizes monetized attribute's value" do
product.instance_variable_set '@reduced_price', nil
reduced_price = product.read_monetized(:reduced_price, :reduced_price_cents)
expect(product.instance_variable_get('@reduced_price')).to eq(reduced_price)
end
it "resets memoized attribute's value if amount has changed" do
reduced_price = product.read_monetized(:reduced_price, :reduced_price_cents)
product.reduced_price_cents = 100
expect(product.read_monetized(:reduced_price, :reduced_price_cents)).not_to eq(reduced_price)
end
it "resets memoized attribute's value if currency has changed" do
reduced_price = product.read_monetized(:reduced_price, :reduced_price_cents)
product.reduced_price_currency = 'CAD'
expect(product.read_monetized(:reduced_price, :reduced_price_cents)).not_to eq(reduced_price)
end
end
context "with preserve_user_input set" do
around(:each) do |example|
MoneyRails::Configuration.preserve_user_input = true
example.run
MoneyRails::Configuration.preserve_user_input = false
end
it "has no effect if validation passes" do
product.price = '14'
expect(product.save).to be_truthy
expect(product.read_monetized(:price, :price_cents).to_s).to eq('14.00')
end
it "preserves user input if validation fails" do
product.price = '14,0'
expect(product.save).to be_falsy
expect(product.read_monetized(:price, :price_cents).to_s).to eq('14,0')
end
end
context "with a monetized attribute that is nil" do
let(:service) do
Service.create(discount_cents: nil)
end
let(:default_currency_lambda) { double("Default Currency Fallback") }
subject { service.read_monetized(:discount, :discount_cents, options) }
around(:each) do |example|
service # Instantiate instance which relies on Money.default_currency
original_default_currency = Money.default_currency
Money.default_currency = -> { default_currency_lambda.read_currency }
example.run
Money.default_currency = original_default_currency
end
context "when allow_nil options is set" do
let(:options) { { allow_nil: true } }
it "does not attempt to read the fallback default currency" do
expect(default_currency_lambda).not_to receive(:read_currency)
subject
end
end
end
end
describe "#write_monetized" do
let(:value) { Money.new(1_000, 'LVL') }
it "sets monetized attribute's value to Money object" do
product.write_monetized :price, :price_cents, value, false, nil, {}
expect(product.price).to be_an_instance_of(Money)
expect(product.price_cents).to eq(value.cents)
# Because :price does not have a column for currency
expect(product.price.currency).to eq(Product.currency)
end
it "sets monetized attribute's value from a given Fixnum" do
product.write_monetized :price, :price_cents, 10, false, nil, {}
expect(product.price).to be_an_instance_of(Money)
expect(product.price_cents).to eq(1000)
end
it "sets monetized attribute's value from a given Float" do
product.write_monetized :price, :price_cents, 10.5, false, nil, {}
expect(product.price).to be_an_instance_of(Money)
expect(product.price_cents).to eq(1050)
end
it "resets monetized attribute when given blank input" do
product.write_monetized :price, :price_cents, nil, false, nil, { allow_nil: true }
expect(product.price).to eq(nil)
end
it "sets monetized attribute to 0 when given a blank value" do
currency = product.price.currency
product.write_monetized :price, :price_cents, nil, false, nil, {}
expect(product.price.amount).to eq(0)
expect(product.price.currency).to eq(currency)
end
it "does not memoize monetized attribute's value if currency is read-only" do
product.write_monetized :price, :price_cents, value, false, nil, {}
price = product.instance_variable_get('@price')
expect(price).to be_an_instance_of(Money)
expect(price.amount).not_to eq(value.amount)
end
context 'without a default currency' do
let(:product) { OtherProduct.new }
around do |example|
default_currency = Money.default_currency
Money.default_currency = nil
example.run
Money.default_currency = default_currency
end
it "errors a NoCurrency Error" do
expect do
product.write_monetized :price, :price_cents, 10.5, false, nil, {}
end.to raise_error(Money::Currency::NoCurrency)
end
end
describe "instance_currency_name" do
it "updates instance_currency_name attribute" do
product.write_monetized :sale_price, :sale_price_amount, value, false, :sale_price_currency_code, {}
expect(product.sale_price).to eq(value)
expect(product.sale_price_currency_code).to eq('LVL')
end
it "memoizes monetized attribute's value with currency" do
product.write_monetized :sale_price, :sale_price_amount, value, false, :sale_price_currency_code, {}
expect(product.instance_variable_get('@sale_price')).to eq(value)
end
it "ignores empty instance_currency_name" do
product.write_monetized :sale_price, :sale_price_amount, value, false, '', {}
expect(product.sale_price.amount).to eq(value.amount)
expect(product.sale_price.currency).to eq(Product.currency)
end
it "ignores instance_currency_name that model does not respond to" do
product.write_monetized :sale_price, :sale_price_amount, value, false, :non_existing_currency, {}
expect(product.sale_price.amount).to eq(value.amount)
expect(product.sale_price.currency).to eq(Product.currency)
end
end
describe "error handling" do