money-rails 1.13.0 → 1.13.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17a8dc1cf4b600e6893a143ea21e6a9bef017371
4
- data.tar.gz: 21aa33f3cace05847f66f822b88b46059752ed99
3
+ metadata.gz: a4aaf2a0c37bb46472539f30ec9c60f868ffd3ec
4
+ data.tar.gz: cdc46c11f65bf4d28afe78dee2de672e66246c2b
5
5
  SHA512:
6
- metadata.gz: 3d8d1a12dccd30071f5e269b679b55a733fb2eaedf9ed694ed8dcd9c02ff95315293fe97387559e5d891240489f79c9acd57b79861a49c68cf6eeab387f5a032
7
- data.tar.gz: 87e4fd7469311f179282531901437773316083fb695dc12003f5767d9d61c9a40cb06d553f7cdc17e4246dbbbb5b0d10973a64417bd602ee8060ddb7c42d9fa3
6
+ metadata.gz: e79213ea0c3c9aab5563ae75984e6639045ae87fc6c9a63728482a93f8de6988a1f349a06dc36c71dcd5559dea9a718ba3ac579fd790a429991aca9b49bd4007
7
+ data.tar.gz: 92021999f49f2e24efa5cfba40ed18b0d9714dc2c2bb0c8df171aeec295b4cb31a5f94668de55a4a504fac88707aff31155e239c562ccda420177d552628a886
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.13.1
4
+
5
+ Add a guard clause for "blank form" input (Mongoid)
6
+ Do not add extra errors in case attribute is not a number
7
+ Use Money.locale_backend instead of Money.use_i18n
8
+ Add money_only_cents helper method
9
+
3
10
  ## 1.13.0
4
11
 
5
12
  - Bump money version to ~> 6.13.0
data/README.md CHANGED
@@ -326,7 +326,7 @@ end
326
326
  ```
327
327
 
328
328
  In this case ```product.bonus``` will return a Money object with GBP as its
329
- currency, whereas ```product.discount.currency_as_string # => EUR ```
329
+ currency, whereas ```product.discount.currency.to_s # => EUR ```
330
330
 
331
331
  As mentioned earlier you can use an object that responds to the method ```call``` and accepts the model instance as a parameter. That means you can use a ```Proc``` or ```lambda``` (we would recommend ```lambda``` over ```Proc``` because of their [different control flow characteristics](https://stackoverflow.com/questions/1740046/whats-the-difference-between-a-proc-and-a-lambda-in-ruby)) or even define a separate ```class``` with an instance or class method (maybe even a ```module```) to return the currency code:
332
332
 
@@ -502,6 +502,7 @@ _For examples below, `@money_object == <Money fractional:650 currency:USD>`_
502
502
  | `humanized_money_with_symbol @money_object` | $6.50 |
503
503
  | `money_without_cents @money_object` | 6 |
504
504
  | `money_without_cents_and_with_symbol @money_object` | $6 |
505
+ | `money_only_cents @money_object` | 50 |
505
506
 
506
507
  #### `no_cents_if_whole`
507
508
 
@@ -552,7 +553,7 @@ For examples on using the test_helpers look at
552
553
  ## Supported ORMs/ODMs
553
554
 
554
555
  * ActiveRecord (>= 3.x)
555
- * Mongoid (2.x, 3.x)
556
+ * Mongoid (>= 2.x)
556
557
 
557
558
  ## Supported Ruby interpreters
558
559
 
@@ -44,8 +44,7 @@ module MoneyRails
44
44
  private
45
45
 
46
46
  def record_does_not_have_error?
47
- return true unless @record.errors.has_key?(@attr)
48
- !@record.errors.added?(@attr, :not_a_number)
47
+ !@record.errors.added?(@attr, :not_a_number, value: @raw_value)
49
48
  end
50
49
 
51
50
  def reset_memoized_variables!
@@ -62,16 +61,20 @@ module MoneyRails
62
61
 
63
62
  def decimal_mark
64
63
  character = currency.decimal_mark || '.'
65
- @_decimal_mark ||= Money.use_i18n ? I18n.t('number.currency.format.separator', default: character) : character
64
+ @_decimal_mark ||= use_backend_locale? ? I18n.t('number.currency.format.separator', default: character) : character
66
65
  end
67
66
 
68
67
  def thousands_separator
69
68
  character = currency.thousands_separator || ','
70
- @_thousands_separator ||= Money.use_i18n ? I18n.t('number.currency.format.delimiter', default: character) : character
69
+ @_thousands_separator ||= use_backend_locale? ? I18n.t('number.currency.format.delimiter', default: character) : character
71
70
  end
72
71
 
73
72
  def symbol
74
- @_symbol ||= Money.use_i18n ? I18n.t('number.currency.format.unit', default: currency.symbol) : currency.symbol
73
+ @_symbol ||= use_backend_locale? ? I18n.t('number.currency.format.unit', default: currency.symbol) : currency.symbol
74
+ end
75
+
76
+ def use_backend_locale?
77
+ Money.locale_backend.present? || Money.use_i18n
75
78
  end
76
79
 
77
80
  def abs_raw_value
@@ -48,5 +48,13 @@ module MoneyRails
48
48
  def money_without_cents_and_with_symbol(value)
49
49
  money_without_cents(value, symbol: true)
50
50
  end
51
+
52
+ def money_only_cents(value)
53
+ return '00' unless value.respond_to?(:to_money)
54
+
55
+ value = value.to_money
56
+
57
+ format "%0#{value.currency.exponent}d", (value % value.currency.subunit_to_unit).cents
58
+ end
51
59
  end
52
60
  end
@@ -28,25 +28,12 @@ class Money
28
28
  # Takes any possible object and converts it to how it would be
29
29
  # stored in the database.
30
30
  def mongoize(object)
31
- case
32
- when object.is_a?(Money) then object.mongoize
33
- when object.is_a?(Hash) then
34
- if object.respond_to?(:deep_symbolize_keys!)
35
- object.deep_symbolize_keys!
36
- elsif object.respond_to?(:symbolize_keys!)
37
- object.symbolize_keys!
38
- end
39
- ::Money.new(object[:cents], object[:currency_iso]).mongoize
40
- when object.nil? then nil
41
- when object.respond_to?(:to_money) then
42
- begin
43
- object.to_money.mongoize
44
- rescue Money::Currency::UnknownCurrency, Monetize::ParseError => e
45
- raise MoneyRails::Error, e.message if MoneyRails.raise_error_on_money_parsing
46
- nil
47
- end
48
- else object
49
- end
31
+ return object.mongoize if object.is_a?(Money)
32
+ return mongoize_hash(object) if object.is_a?(Hash)
33
+ return nil if object.nil?
34
+ return mongoize_castable(object) if object.respond_to?(:to_money)
35
+
36
+ object
50
37
  end
51
38
 
52
39
  # Converts the object that was supplied to a criteria and converts it
@@ -57,5 +44,27 @@ class Money
57
44
  else object
58
45
  end
59
46
  end
47
+
48
+ private
49
+
50
+ def mongoize_hash(hash)
51
+ if hash.respond_to?(:deep_symbolize_keys!)
52
+ hash.deep_symbolize_keys!
53
+ elsif hash.respond_to?(:symbolize_keys!)
54
+ hash.symbolize_keys!
55
+ end
56
+
57
+ # Guard for a blank form
58
+ return nil if hash[:cents] == '' && hash[:currency_iso] == ''
59
+
60
+ ::Money.new(hash[:cents], hash[:currency_iso]).mongoize
61
+ end
62
+
63
+ def mongoize_castable(object)
64
+ object.to_money.mongoize
65
+ rescue Money::Currency::UnknownCurrency, Monetize::ParseError => e
66
+ return nil unless MoneyRails.raise_error_on_money_parsing
67
+ raise MoneyRails::Error, e.message
68
+ end
60
69
  end
61
70
  end
@@ -1,3 +1,3 @@
1
1
  module MoneyRails
2
- VERSION = '1.13.0'
2
+ VERSION = '1.13.1'
3
3
  end
@@ -494,31 +494,31 @@ if defined? ActiveRecord
494
494
  product.price = Money.new(2500, :USD)
495
495
  expect(product.save).to be_truthy
496
496
  expect(product.price.cents).to eq(2500)
497
- expect(product.price.currency_as_string).to eq("USD")
497
+ expect(product.price.currency.to_s).to eq("USD")
498
498
  end
499
499
 
500
500
  it "correctly assigns Fixnum objects to the attribute" do
501
501
  product.price = 25
502
502
  expect(product.save).to be_truthy
503
503
  expect(product.price.cents).to eq(2500)
504
- expect(product.price.currency_as_string).to eq("USD")
504
+ expect(product.price.currency.to_s).to eq("USD")
505
505
 
506
506
  service.discount = 2
507
507
  expect(service.save).to be_truthy
508
508
  expect(service.discount.cents).to eq(200)
509
- expect(service.discount.currency_as_string).to eq("EUR")
509
+ expect(service.discount.currency.to_s).to eq("EUR")
510
510
  end
511
511
 
512
512
  it "correctly assigns String objects to the attribute" do
513
513
  product.price = "25"
514
514
  expect(product.save).to be_truthy
515
515
  expect(product.price.cents).to eq(2500)
516
- expect(product.price.currency_as_string).to eq("USD")
516
+ expect(product.price.currency.to_s).to eq("USD")
517
517
 
518
518
  service.discount = "2"
519
519
  expect(service.save).to be_truthy
520
520
  expect(service.discount.cents).to eq(200)
521
- expect(service.discount.currency_as_string).to eq("EUR")
521
+ expect(service.discount.currency.to_s).to eq("EUR")
522
522
  end
523
523
 
524
524
  it "correctly assigns objects to a accessor attribute" do
@@ -532,57 +532,57 @@ if defined? ActiveRecord
532
532
  product.bonus = 25
533
533
  expect(product.save).to be_truthy
534
534
  expect(product.bonus.cents).to eq(2500)
535
- expect(product.bonus.currency_as_string).to eq("GBP")
535
+ expect(product.bonus.currency.to_s).to eq("GBP")
536
536
 
537
537
  service.charge = 2
538
538
  expect(service.save).to be_truthy
539
539
  expect(service.charge.cents).to eq(200)
540
- expect(service.charge.currency_as_string).to eq("USD")
540
+ expect(service.charge.currency.to_s).to eq("USD")
541
541
  end
542
542
 
543
543
  it "overrides default, model currency with the value of :with_currency in string assignments" do
544
544
  product.bonus = "25"
545
545
  expect(product.save).to be_truthy
546
546
  expect(product.bonus.cents).to eq(2500)
547
- expect(product.bonus.currency_as_string).to eq("GBP")
547
+ expect(product.bonus.currency.to_s).to eq("GBP")
548
548
 
549
549
  service.charge = "2"
550
550
  expect(service.save).to be_truthy
551
551
  expect(service.charge.cents).to eq(200)
552
- expect(service.charge.currency_as_string).to eq("USD")
552
+ expect(service.charge.currency.to_s).to eq("USD")
553
553
 
554
554
  product.lambda_price = "32"
555
555
  expect(product.save).to be_truthy
556
556
  expect(product.lambda_price.cents).to eq(3200)
557
- expect(product.lambda_price.currency_as_string).to eq("CAD")
557
+ expect(product.lambda_price.currency.to_s).to eq("CAD")
558
558
  end
559
559
 
560
560
  it "overrides default currency with model currency, in fixnum assignments" do
561
561
  product.discount_value = 5
562
562
  expect(product.save).to be_truthy
563
563
  expect(product.discount_value.cents).to eq(500)
564
- expect(product.discount_value.currency_as_string).to eq("USD")
564
+ expect(product.discount_value.currency.to_s).to eq("USD")
565
565
  end
566
566
 
567
567
  it "overrides default currency with model currency, in string assignments" do
568
568
  product.discount_value = "5"
569
569
  expect(product.save).to be_truthy
570
570
  expect(product.discount_value.cents).to eq(500)
571
- expect(product.discount_value.currency_as_string).to eq("USD")
571
+ expect(product.discount_value.currency.to_s).to eq("USD")
572
572
  end
573
573
 
574
574
  it "falls back to default currency, in fixnum assignments" do
575
575
  service.discount = 5
576
576
  expect(service.save).to be_truthy
577
577
  expect(service.discount.cents).to eq(500)
578
- expect(service.discount.currency_as_string).to eq("EUR")
578
+ expect(service.discount.currency.to_s).to eq("EUR")
579
579
  end
580
580
 
581
581
  it "falls back to default currency, in string assignments" do
582
582
  service.discount = "5"
583
583
  expect(service.save).to be_truthy
584
584
  expect(service.discount.cents).to eq(500)
585
- expect(service.discount.currency_as_string).to eq("EUR")
585
+ expect(service.discount.currency.to_s).to eq("EUR")
586
586
  end
587
587
 
588
588
  it "sets field to nil, in nil assignments if allow_nil is set" do
@@ -616,13 +616,13 @@ if defined? ActiveRecord
616
616
  context "for column with model currency:" do
617
617
  it "has default currency if not specified" do
618
618
  product = Product.create(sale_price_amount: 1234)
619
- product.sale_price.currency_as_string == 'USD'
619
+ product.sale_price.currency.to_s == 'USD'
620
620
  end
621
621
 
622
622
  it "is overridden by instance currency column" do
623
623
  product = Product.create(sale_price_amount: 1234,
624
624
  sale_price_currency_code: 'CAD')
625
- expect(product.sale_price.currency_as_string).to eq('CAD')
625
+ expect(product.sale_price.currency.to_s).to eq('CAD')
626
626
  end
627
627
 
628
628
  it 'can change currency of custom column' do
@@ -634,14 +634,14 @@ if defined? ActiveRecord
634
634
  sale_price_currency_code: 'USD'
635
635
  )
636
636
 
637
- expect(product.sale_price.currency_as_string).to eq('USD')
637
+ expect(product.sale_price.currency.to_s).to eq('USD')
638
638
 
639
639
  product.sale_price = Money.new 456, 'CAD'
640
640
  product.save
641
641
  product.reload
642
642
 
643
- expect(product.sale_price.currency_as_string).to eq('CAD')
644
- expect(product.discount_value.currency_as_string).to eq('USD')
643
+ expect(product.sale_price.currency.to_s).to eq('CAD')
644
+ expect(product.discount_value.currency.to_s).to eq('USD')
645
645
  end
646
646
  end
647
647
 
@@ -705,12 +705,12 @@ if defined? ActiveRecord
705
705
  transaction.amount = Money.new(2500, :eur)
706
706
  expect(transaction.save).to be_truthy
707
707
  expect(transaction.amount.cents).to eq(Money.new(2500, :eur).cents)
708
- expect(transaction.amount.currency_as_string).to eq("EUR")
708
+ expect(transaction.amount.currency.to_s).to eq("EUR")
709
709
  end
710
710
 
711
711
  it "uses default currency if a non Money object is assigned to the attribute" do
712
712
  transaction.amount = 234
713
- expect(transaction.amount.currency_as_string).to eq("USD")
713
+ expect(transaction.amount.currency.to_s).to eq("USD")
714
714
  end
715
715
 
716
716
  it "constructs the money object from the mapped method value" do
@@ -767,9 +767,42 @@ if defined? ActiveRecord
767
767
  end
768
768
  end
769
769
 
770
+ context "when locale_backend is true" do
771
+ around(:each) do |example|
772
+ begin
773
+ Money.locale_backend = :i18n
774
+ Money.use_i18n = false
775
+ example.run
776
+ ensure
777
+ Money.locale_backend = nil
778
+ Money.use_i18n = true
779
+ end
780
+ end
781
+ it "validates with the locale's decimal mark" do
782
+ transaction.amount = "123,45"
783
+ expect(transaction.valid?).to be_truthy
784
+ end
785
+
786
+ it "does not validate with the currency's decimal mark" do
787
+ transaction.amount = "123.45"
788
+ expect(transaction.valid?).to be_falsey
789
+ end
790
+
791
+ it "validates with the locale's currency symbol" do
792
+ transaction.amount = "€123"
793
+ expect(transaction.valid?).to be_truthy
794
+ end
795
+
796
+ it "does not validate with the transaction's currency symbol" do
797
+ transaction.amount = "$123.45"
798
+ expect(transaction.valid?).to be_falsey
799
+ end
800
+ end
801
+
770
802
  context "when use_i18n is false" do
771
803
  around(:each) do |example|
772
804
  begin
805
+ Money.locale_backend = nil
773
806
  Money.use_i18n = false
774
807
  example.run
775
808
  ensure
@@ -18,8 +18,8 @@ describe "configuration" do
18
18
  end
19
19
 
20
20
  it "sets no_cents_if_whole value for formatted output globally" do
21
- # Disable the usage of I18n (to avoid default symbols for :en)
22
- Money.use_i18n = false
21
+ # Enable formatting to depend only on currency (to avoid default symbols for :en)
22
+ Money.locale_backend = :currency
23
23
 
24
24
  value = Money.new(12345600, "EUR")
25
25
  mark = Money::Currency.find(:eur).decimal_mark
@@ -35,7 +35,7 @@ describe "configuration" do
35
35
 
36
36
  # Reset global settings
37
37
  MoneyRails.no_cents_if_whole = nil
38
- Money.use_i18n = true
38
+ Money.locale_backend = :i18n
39
39
  end
40
40
 
41
41
  it "sets symbol for formatted output globally" do
@@ -74,6 +74,27 @@ describe 'MoneyRails::ActionViewExtension', type: :helper do
74
74
  it { is_expected.not_to include "00" }
75
75
  end
76
76
 
77
+ describe '#money_only_cents' do
78
+ let(:monetizable_object){ Money.new(125_00) }
79
+ subject { helper.money_only_cents monetizable_object }
80
+ it { is_expected.to eq "00" }
81
+
82
+ context 'with a non-money object' do
83
+ let(:monetizable_object){ 125 }
84
+ it { is_expected.to eq "00" }
85
+ end
86
+
87
+ context 'with less than 10 cents' do
88
+ let(:monetizable_object){ Money.new(8) }
89
+ it { is_expected.to eq "08" }
90
+ end
91
+
92
+ context 'with a non monetizable object' do
93
+ let(:monetizable_object){ false }
94
+ it { is_expected.to eq "00" }
95
+ end
96
+ end
97
+
77
98
  context 'respects MoneyRails::Configuration settings' do
78
99
  context 'with no_cents_if_whole: false' do
79
100
 
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- if defined?(Mongoid) && ::Mongoid::VERSION =~ /^4(.*)/
3
+ if defined?(Mongoid) && ::Mongoid::VERSION.split('.').first.to_i > 2
4
4
 
5
5
  describe Money do
6
6
  let!(:priceable) { Priceable.create(price: Money.new(100, 'EUR')) }
@@ -8,6 +8,9 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^4(.*)/
8
8
  let(:priceable_from_num) { Priceable.create(price: 1) }
9
9
  let(:priceable_from_string) { Priceable.create(price: '1 EUR' )}
10
10
  let(:priceable_from_hash) { Priceable.create(price: {cents: 100, currency_iso: "EUR"} )}
11
+ let(:priceable_from_blank_strings_hash) {
12
+ Priceable.create(price: {cents: '', currency_iso: ''})
13
+ }
11
14
  let(:priceable_from_hash_with_indifferent_access) {
12
15
  Priceable.create(price: {cents: 100, currency_iso: "EUR"}.with_indifferent_access)
13
16
  }
@@ -69,6 +72,10 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^4(.*)/
69
72
  expect(priceable_from_hash.price.currency).to eq(Money::Currency.find('EUR'))
70
73
  end
71
74
 
75
+ it "mongoizes a hash of blank strings for cents and currency to nil" do
76
+ expect(priceable_from_blank_strings_hash.price).to eq(nil)
77
+ end
78
+
72
79
  it "correctly mongoizes a HashWithIndifferentAccess of cents and currency" do
73
80
  expect(priceable_from_hash_with_indifferent_access.price.cents).to eq(100)
74
81
  expect(priceable_from_hash_with_indifferent_access.price.currency).to eq(Money::Currency.find('EUR'))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.13.0
4
+ version: 1.13.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Loupasakis
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-10-23 00:00:00.000000000 Z
13
+ date: 2019-01-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: money
@@ -221,9 +221,7 @@ files:
221
221
  - spec/dummy/script/rails
222
222
  - spec/helpers/action_view_extension_spec.rb
223
223
  - spec/helpers/form_helper_spec.rb
224
- - spec/mongoid/five_spec.rb
225
- - spec/mongoid/four_spec.rb
226
- - spec/mongoid/three_spec.rb
224
+ - spec/mongoid/mongoid_spec.rb
227
225
  - spec/mongoid/two_spec.rb
228
226
  - spec/spec_helper.rb
229
227
  - spec/support/database_cleaner.rb
@@ -315,9 +313,7 @@ test_files:
315
313
  - spec/dummy/script/rails
316
314
  - spec/helpers/action_view_extension_spec.rb
317
315
  - spec/helpers/form_helper_spec.rb
318
- - spec/mongoid/five_spec.rb
319
- - spec/mongoid/four_spec.rb
320
- - spec/mongoid/three_spec.rb
316
+ - spec/mongoid/mongoid_spec.rb
321
317
  - spec/mongoid/two_spec.rb
322
318
  - spec/spec_helper.rb
323
319
  - spec/support/database_cleaner.rb
@@ -1,117 +0,0 @@
1
- require 'spec_helper'
2
-
3
- if defined?(Mongoid) && ::Mongoid::VERSION =~ /^5(.*)/
4
-
5
- describe Money do
6
- let!(:priceable) { Priceable.create(price: Money.new(100, 'EUR')) }
7
- let!(:priceable_from_num) { Priceable.create(price: 1) }
8
- let!(:priceable_from_string) { Priceable.create(price: '1 EUR' )}
9
- let!(:priceable_from_hash) { Priceable.create(price: {cents: 100, currency_iso: "EUR"} )}
10
- let!(:priceable_from_hash_with_indifferent_access) {
11
- Priceable.create(price: {cents: 100, currency_iso: "EUR"}.with_indifferent_access)
12
- }
13
- let(:priceable_from_string_with_hyphen) { Priceable.create(price: '1-2 EUR' )}
14
- let(:priceable_from_string_with_unknown_currency) { Priceable.create(price: '1 TLDR') }
15
- let(:priceable_with_infinite_precision) { Priceable.create(price: Money.new(BigDecimal.new('100.1'), 'EUR')) }
16
- let(:priceable_with_hash_field) {
17
- Priceable.create(price_hash: {
18
- key1: Money.new(100, "EUR"),
19
- key2: Money.new(200, "USD")
20
- })
21
- }
22
-
23
- context "mongoize" do
24
- it "correctly mongoizes a Money object to a hash of cents and currency" do
25
- expect(priceable.price.cents).to eq(100)
26
- expect(priceable.price.currency).to eq(Money::Currency.find('EUR'))
27
- end
28
-
29
- it "correctly mongoizes a Numeric object to a hash of cents and currency" do
30
- expect(priceable_from_num.price.cents).to eq(100)
31
- expect(priceable_from_num.price.currency).to eq(Money.default_currency)
32
- end
33
-
34
- it "correctly mongoizes a String object to a hash of cents and currency" do
35
- expect(priceable_from_string.price.cents).to eq(100)
36
- expect(priceable_from_string.price.currency).to eq(Money::Currency.find('EUR'))
37
- end
38
-
39
- context "when MoneyRails.raise_error_on_money_parsing is true" do
40
- before { MoneyRails.raise_error_on_money_parsing = true }
41
- after { MoneyRails.raise_error_on_money_parsing = false }
42
-
43
- it "raises exception if the mongoized value is a String with a hyphen" do
44
- expect { priceable_from_string_with_hyphen }.to raise_error MoneyRails::Error
45
- end
46
-
47
- it "raises exception if the mongoized value is a String with an unknown currency" do
48
- expect { priceable_from_string_with_unknown_currency }.to raise_error MoneyRails::Error
49
- end
50
- end
51
-
52
- context "when MoneyRails.raise_error_on_money_parsing is false" do
53
- it "does not correctly mongoize a String with a hyphen in its middle" do
54
- expect(priceable_from_string_with_hyphen.price).to eq(nil)
55
- end
56
-
57
- it "does not correctly mongoize a String with an unknown currency" do
58
- expect(priceable_from_string_with_unknown_currency.price).to eq(nil)
59
- end
60
- end
61
-
62
- it "correctly mongoizes a hash of cents and currency" do
63
- expect(priceable_from_hash.price.cents).to eq(100)
64
- expect(priceable_from_hash.price.currency).to eq(Money::Currency.find('EUR'))
65
- end
66
-
67
- it "correctly mongoizes a HashWithIndifferentAccess of cents and currency" do
68
- expect(priceable_from_hash_with_indifferent_access.price.cents).to eq(100)
69
- expect(priceable_from_hash_with_indifferent_access.price.currency).to eq(Money::Currency.find('EUR'))
70
- end
71
-
72
- context "infinite_precision = true" do
73
- before do
74
- Money.infinite_precision = true
75
- end
76
-
77
- after do
78
- Money.infinite_precision = false
79
- end
80
-
81
- it "correctly mongoizes a Money object to a hash of cents and currency" do
82
- expect(priceable_with_infinite_precision.price.cents).to eq(BigDecimal.new('100.1'))
83
- expect(priceable_with_infinite_precision.price.currency).to eq(Money::Currency.find('EUR'))
84
- end
85
- end
86
- end
87
-
88
- it "correctly serializes a Hash field containing Money objects" do
89
- expect(priceable_with_hash_field.price_hash[:key1][:cents]).to eq(100)
90
- expect(priceable_with_hash_field.price_hash[:key2][:cents]).to eq(200)
91
- expect(priceable_with_hash_field.price_hash[:key1][:currency_iso]).to eq('EUR')
92
- expect(priceable_with_hash_field.price_hash[:key2][:currency_iso]).to eq('USD')
93
- end
94
-
95
- context "demongoize" do
96
- subject { Priceable.first.price }
97
- it { is_expected.to be_an_instance_of(Money) }
98
- it { is_expected.to eq(Money.new(100, 'EUR')) }
99
-
100
- it "returns 0 cents in default_currency if a nil value was stored" do
101
- nil_priceable = Priceable.create(price: nil)
102
- expect(nil_priceable.price.cents).to eq(0)
103
- expect(nil_priceable.price.currency).to eq(Money.default_currency)
104
- end
105
- it 'returns nil if an unknown value was stored' do
106
- zero_priceable = Priceable.create(price: [])
107
- expect(zero_priceable.price).to be_nil
108
- end
109
- end
110
-
111
- context "evolve" do
112
- it "correctly transforms a Money object into a Mongo friendly value" do
113
- expect(Priceable.where(price: Money.new(100, 'EUR')).first).to eq(priceable)
114
- end
115
- end
116
- end
117
- end
@@ -1,122 +0,0 @@
1
- require 'spec_helper'
2
-
3
- if defined?(Mongoid) && ::Mongoid::VERSION =~ /^3(.*)/
4
-
5
- describe Money do
6
- let!(:priceable) { Priceable.create(price: Money.new(100, 'EUR')) }
7
- let(:priceable_from_nil) { Priceable.create(price: nil) }
8
- let(:priceable_from_num) { Priceable.create(price: 1) }
9
- let(:priceable_from_string) { Priceable.create(price: '1 EUR' )}
10
- let(:priceable_from_hash) { Priceable.create(price: {cents: 100, currency_iso: "EUR"} )}
11
- let(:priceable_from_hash_with_indifferent_access) {
12
- Priceable.create(price: {cents: 100, currency_iso: "EUR"}.with_indifferent_access)
13
- }
14
- let(:priceable_from_string_with_hyphen) { Priceable.create(price: '1-2 EUR' )}
15
- let(:priceable_from_string_with_unknown_currency) { Priceable.create(price: '1 TLDR') }
16
- let(:priceable_with_infinite_precision) { Priceable.create(price: Money.new(BigDecimal.new('100.1'), 'EUR')) }
17
- let(:priceable_with_hash_field) {
18
- Priceable.create(price_hash: {
19
- key1: Money.new(100, "EUR"),
20
- key2: Money.new(200, "USD")
21
- })
22
- }
23
-
24
- context "mongoize" do
25
- it "mongoizes correctly nil to nil" do
26
- expect(priceable_from_nil.price).to be_nil
27
- end
28
-
29
- it "mongoizes correctly a Money object to a hash of cents and currency" do
30
- expect(priceable.price.cents).to eq(100)
31
- expect(priceable.price.currency).to eq(Money::Currency.find('EUR'))
32
- end
33
-
34
- it "mongoizes correctly a Numeric object to a hash of cents and currency" do
35
- expect(priceable_from_num.price.cents).to eq(100)
36
- expect(priceable_from_num.price.currency).to eq(Money.default_currency)
37
- end
38
-
39
- it "mongoizes correctly a String object to a hash of cents and currency" do
40
- expect(priceable_from_string.price.cents).to eq(100)
41
- expect(priceable_from_string.price.currency).to eq(Money::Currency.find('EUR'))
42
- end
43
-
44
- context "when MoneyRails.raise_error_on_money_parsing is true" do
45
- before { MoneyRails.raise_error_on_money_parsing = true }
46
- after { MoneyRails.raise_error_on_money_parsing = false }
47
-
48
- it "raises exception if the mongoized value is a String with a hyphen" do
49
- expect { priceable_from_string_with_hyphen }.to raise_error MoneyRails::Error
50
- end
51
-
52
- it "raises exception if the mongoized value is a String with an unknown currency" do
53
- expect { priceable_from_string_with_unknown_currency }.to raise_error MoneyRails::Error
54
- end
55
- end
56
-
57
- context "when MoneyRails.raise_error_on_money_parsing is false" do
58
- it "does not mongoizes correctly a String with hyphen in its middle" do
59
- expect(priceable_from_string_with_hyphen.price).to eq(nil)
60
- end
61
-
62
- it "does not mongoize correctly a String with an unknown currency" do
63
- expect(priceable_from_string_with_unknown_currency.price).to eq(nil)
64
- end
65
- end
66
-
67
- it "mongoizes correctly a hash of cents and currency" do
68
- expect(priceable_from_hash.price.cents).to eq(100)
69
- expect(priceable_from_hash.price.currency).to eq(Money::Currency.find('EUR'))
70
- end
71
-
72
- it "mongoizes correctly a HashWithIndifferentAccess of cents and currency" do
73
- expect(priceable_from_hash_with_indifferent_access.price.cents).to eq(100)
74
- expect(priceable_from_hash_with_indifferent_access.price.currency).to eq(Money::Currency.find('EUR'))
75
- end
76
-
77
- context "infinite_precision = true" do
78
- before do
79
- Money.infinite_precision = true
80
- end
81
-
82
- after do
83
- Money.infinite_precision = false
84
- end
85
-
86
- it "mongoizes correctly a Money object to a hash of cents and currency" do
87
- expect(priceable_with_infinite_precision.price.cents).to eq(BigDecimal.new('100.1'))
88
- expect(priceable_with_infinite_precision.price.currency).to eq(Money::Currency.find('EUR'))
89
- end
90
- end
91
- end
92
-
93
- it "serializes correctly a Hash field containing Money objects" do
94
- expect(priceable_with_hash_field.price_hash[:key1][:cents]).to eq(100)
95
- expect(priceable_with_hash_field.price_hash[:key2][:cents]).to eq(200)
96
- expect(priceable_with_hash_field.price_hash[:key1][:currency_iso]).to eq('EUR')
97
- expect(priceable_with_hash_field.price_hash[:key2][:currency_iso]).to eq('USD')
98
- end
99
-
100
- context "demongoize" do
101
- subject { Priceable.first.price }
102
- it { is_expected.to be_an_instance_of(Money) }
103
- it { is_expected.to eq(Money.new(100, 'EUR')) }
104
-
105
- it "returns nil if a nil value was stored" do
106
- nil_priceable = Priceable.create(price: nil)
107
- expect(nil_priceable.price).to be_nil
108
- end
109
-
110
- it 'returns nil if an unknown value was stored' do
111
- zero_priceable = Priceable.create(price: [])
112
- expect(zero_priceable.price).to be_nil
113
- end
114
- end
115
-
116
- context "evolve" do
117
- it "transforms correctly a Money object to a Mongo friendly value" do
118
- expect(Priceable.where(price: Money.new(100, 'EUR')).first).to eq(priceable)
119
- end
120
- end
121
- end
122
- end