money-rails 1.13.0 → 1.13.3

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
- SHA1:
3
- metadata.gz: 17a8dc1cf4b600e6893a143ea21e6a9bef017371
4
- data.tar.gz: 21aa33f3cace05847f66f822b88b46059752ed99
2
+ SHA256:
3
+ metadata.gz: 94253df4c6ac9ec75bfc1b569d6d05a96082846df1c7e2ab61d65cc56cf10183
4
+ data.tar.gz: 8fbcf7e06595b836c03d12d77bba6db42fbf40abc5b1de44e7c6311ffca4f01f
5
5
  SHA512:
6
- metadata.gz: 3d8d1a12dccd30071f5e269b679b55a733fb2eaedf9ed694ed8dcd9c02ff95315293fe97387559e5d891240489f79c9acd57b79861a49c68cf6eeab387f5a032
7
- data.tar.gz: 87e4fd7469311f179282531901437773316083fb695dc12003f5767d9d61c9a40cb06d553f7cdc17e4246dbbbb5b0d10973a64417bd602ee8060ddb7c42d9fa3
6
+ metadata.gz: a3f73a3833288cc529da04343d58ce82dabaa3563c61281712a8e5d975f3c60cacce5ff37b9b5422bacbf6f6c0606468643145376521925b9db26281748150fb
7
+ data.tar.gz: 035af0f28d1aae1e28a970938f402a32c5de2eb1abd62bc2d36808f366dfe60f92f06aa4c5dddf9f940f85b20776deb9707def03b65544aedb6c9883dd8c67ac
@@ -1,5 +1,23 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.13.3
4
+
5
+ - Add Money#to_hash for JSON serialization
6
+ - Update initializer template with #locale_backend config
7
+ - Rollback support for remove_monetize / remove_money DB helpers
8
+ - Rails 6 support
9
+
10
+ ## 1.13.2
11
+
12
+ - Make validation compatible with Money.locale_backend
13
+
14
+ ## 1.13.1
15
+
16
+ - Add a guard clause for "blank form" input (Mongoid)
17
+ - Do not add extra errors in case attribute is not a number
18
+ - Use Money.locale_backend instead of Money.use_i18n
19
+ - Add money_only_cents helper method
20
+
3
21
  ## 1.13.0
4
22
 
5
23
  - 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
 
@@ -83,6 +83,29 @@ MoneyRails.configure do |config|
83
83
  # sign_before_symbol: nil
84
84
  # }
85
85
 
86
+ # If you would like to use I18n localization (formatting depends on the
87
+ # locale):
88
+ # config.locale_backend = :i18n
89
+ #
90
+ # Example (using default localization from rails-i18n):
91
+ #
92
+ # I18n.locale = :en
93
+ # Money.new(10_000_00, 'USD').format # => $10,000.00
94
+ # I18n.locale = :es
95
+ # Money.new(10_000_00, 'USD').format # => $10.000,00
96
+ #
97
+ # For the legacy behaviour of "per currency" localization (formatting depends
98
+ # only on currency):
99
+ # config.locale_backend = :currency
100
+ #
101
+ # Example:
102
+ # Money.new(10_000_00, 'USD').format # => $10,000.00
103
+ # Money.new(10_000_00, 'EUR').format # => €10.000,00
104
+ #
105
+ # In case you don't need localization and would like to use default values
106
+ # (can be redefined using config.default_format):
107
+ # config.locale_backend = nil
108
+
86
109
  # Set default raise_error_on_money_parsing option
87
110
  # It will be raise error if assigned different currency
88
111
  # The default value is false
@@ -43,9 +43,13 @@ module MoneyRails
43
43
 
44
44
  private
45
45
 
46
+ DEFAULTS = {
47
+ decimal_mark: '.',
48
+ thousands_separator: ','
49
+ }.freeze
50
+
46
51
  def record_does_not_have_error?
47
- return true unless @record.errors.has_key?(@attr)
48
- !@record.errors.added?(@attr, :not_a_number)
52
+ !@record.errors.added?(@attr, :not_a_number, value: @raw_value)
49
53
  end
50
54
 
51
55
  def reset_memoized_variables!
@@ -61,17 +65,17 @@ module MoneyRails
61
65
  end
62
66
 
63
67
  def decimal_mark
64
- character = currency.decimal_mark || '.'
65
- @_decimal_mark ||= Money.use_i18n ? I18n.t('number.currency.format.separator', default: character) : character
68
+ @_decimal_mark ||= lookup(:decimal_mark)
66
69
  end
67
70
 
68
71
  def thousands_separator
69
- character = currency.thousands_separator || ','
70
- @_thousands_separator ||= Money.use_i18n ? I18n.t('number.currency.format.delimiter', default: character) : character
72
+ @_thousands_separator ||= lookup(:thousands_separator)
71
73
  end
72
74
 
75
+ # TODO: This is supporting legacy behaviour where a symbol can come from a i18n locale,
76
+ # however practical implications of that are most likely non-existent
73
77
  def symbol
74
- @_symbol ||= Money.use_i18n ? I18n.t('number.currency.format.unit', default: currency.symbol) : currency.symbol
78
+ @_symbol ||= lookup(:symbol) || currency.symbol
75
79
  end
76
80
 
77
81
  def abs_raw_value
@@ -125,6 +129,18 @@ module MoneyRails
125
129
  .gsub(decimal_mark, '.')
126
130
  .gsub(/[\s_]/, '')
127
131
  end
132
+
133
+ def lookup(key)
134
+ if locale_backend
135
+ locale_backend.lookup(key, currency) || DEFAULTS[key]
136
+ else
137
+ DEFAULTS[key]
138
+ end
139
+ end
140
+
141
+ def locale_backend
142
+ Money.locale_backend
143
+ end
128
144
  end
129
145
  end
130
146
  end
@@ -11,8 +11,8 @@ module MoneyRails
11
11
 
12
12
  def remove_monetize(table_name, accessor, options={})
13
13
  [:amount, :currency].each do |attribute|
14
- column_present, table_name, column_name, _, _ = OptionsExtractor.extract attribute, table_name, accessor, options
15
- remove_column table_name, column_name if column_present
14
+ column_present, table_name, column_name, type, _ = OptionsExtractor.extract attribute, table_name, accessor, options
15
+ remove_column table_name, column_name, type if column_present
16
16
  end
17
17
  end
18
18
  end
@@ -7,7 +7,6 @@ module MoneyRails
7
7
  # MoneyRails configuration module.
8
8
  # This is extended by MoneyRails to provide configuration settings.
9
9
  module Configuration
10
-
11
10
  # Start a MoneyRails configuration block in an initializer.
12
11
  #
13
12
  # example: Provide a default currency for the application
@@ -59,7 +58,7 @@ module MoneyRails
59
58
  # MoneyRails.configure do |config|
60
59
  # config.default_bank = EuCentralBank.new
61
60
  # end
62
- delegate :default_bank=, :default_bank, to: :Money
61
+ delegate :default_bank=, :default_bank, :locale_backend, :locale_backend=, to: :Money
63
62
 
64
63
  # Provide exchange rates
65
64
  delegate :add_rate, to: :Money
@@ -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
@@ -2,7 +2,7 @@ require "active_support/core_ext/module/aliasing.rb"
2
2
  require "active_support/core_ext/hash/reverse_merge.rb"
3
3
 
4
4
  class Money
5
- class <<self
5
+ class << self
6
6
  alias_method :orig_default_formatting_rules, :default_formatting_rules
7
7
 
8
8
  def default_formatting_rules
@@ -21,4 +21,9 @@ class Money
21
21
  rules
22
22
  end
23
23
  end
24
+
25
+ # This is expected to be called by ActiveSupport when calling as_json an Money object
26
+ def to_hash
27
+ { cents: cents, currency_iso: currency.iso_code.to_s }
28
+ end
24
29
  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.3'
3
3
  end
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
26
26
 
27
27
  s.require_path = "lib"
28
28
 
29
- s.add_dependency "money", "~> 6.13.0"
29
+ s.add_dependency "money", "~> 6.13.2"
30
30
  s.add_dependency "monetize", "~> 1.9.0"
31
31
  s.add_dependency "activesupport", ">= 3.0"
32
32
  s.add_dependency "railties", ">= 3.0"
@@ -36,4 +36,10 @@ Gem::Specification.new do |s|
36
36
  s.add_development_dependency "rspec-rails", "~> 3.0"
37
37
  s.add_development_dependency 'database_cleaner', '~> 1.6.1'
38
38
  s.add_development_dependency 'test-unit', '~> 3.0' if RUBY_VERSION >= '2.2'
39
+
40
+ if s.respond_to?(:metadata)
41
+ s.metadata['changelog_uri'] = 'https://github.com/RubyMoney/money-rails/blob/master/CHANGELOG.md'
42
+ s.metadata['source_code_uri'] = 'https://github.com/RubyMoney/money-rails/'
43
+ s.metadata['bug_tracker_uri'] = 'https://github.com/RubyMoney/money-rails/issues'
44
+ end
39
45
  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
@@ -738,6 +738,8 @@ if defined? ActiveRecord
738
738
  end
739
739
  end
740
740
 
741
+ # TODO: these specs should mock locale_backend with expected values
742
+ # instead of manipulating it directly
741
743
  context "and an Italian locale" do
742
744
  around(:each) do |example|
743
745
  I18n.with_locale(:it) do
@@ -745,7 +747,7 @@ if defined? ActiveRecord
745
747
  end
746
748
  end
747
749
 
748
- context "when use_i18n is true" do
750
+ context "when using :i18n locale backend" do
749
751
  it "validates with the locale's decimal mark" do
750
752
  transaction.amount = "123,45"
751
753
  expect(transaction.valid?).to be_truthy
@@ -767,13 +769,13 @@ if defined? ActiveRecord
767
769
  end
768
770
  end
769
771
 
770
- context "when use_i18n is false" do
772
+ context "when using :currency locale backend" do
771
773
  around(:each) do |example|
772
774
  begin
773
- Money.use_i18n = false
775
+ Money.locale_backend = :currency
774
776
  example.run
775
777
  ensure
776
- Money.use_i18n = true
778
+ Money.locale_backend = :i18n
777
779
  end
778
780
  end
779
781
 
@@ -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
 
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Money overrides' do
6
+ describe '.default_formatting_rules' do
7
+ it 'uses defauts set as individual options' do
8
+ allow(MoneyRails::Configuration).to receive(:symbol).and_return('£')
9
+
10
+ expect(Money.default_formatting_rules).to include(symbol: '£')
11
+ end
12
+
13
+ it 'ignores individual options that are nil' do
14
+ allow(MoneyRails::Configuration).to receive(:symbol).and_return(nil)
15
+
16
+ expect(Money.default_formatting_rules.keys).not_to include(:symbol)
17
+ end
18
+
19
+ it 'includes default_format options' do
20
+ allow(MoneyRails::Configuration).to receive(:default_format).and_return(symbol: '£')
21
+
22
+ expect(Money.default_formatting_rules).to include(symbol: '£')
23
+ end
24
+
25
+ it 'gives priority to original defaults' do
26
+ allow(Money).to receive(:orig_default_formatting_rules).and_return(symbol: '£')
27
+ allow(MoneyRails::Configuration).to receive(:symbol).and_return('€')
28
+ allow(MoneyRails::Configuration).to receive(:default_format).and_return(symbol: '€')
29
+
30
+ expect(Money.default_formatting_rules).to include(symbol: '£')
31
+ end
32
+ end
33
+
34
+ describe '#to_hash' do
35
+ it 'returns a hash with JSON representation' do
36
+ expect(Money.new(9_99, 'EUR').to_hash).to eq(cents: 9_99, currency_iso: 'EUR')
37
+ expect(Money.zero('USD').to_hash).to eq(cents: 0, currency_iso: 'USD')
38
+ end
39
+ end
40
+ end
@@ -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,12 +8,15 @@ 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
  }
14
17
  let(:priceable_from_string_with_hyphen) { Priceable.create(price: '1-2 EUR' )}
15
18
  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')) }
19
+ let(:priceable_with_infinite_precision) { Priceable.create(price: Money.new(BigDecimal('100.1'), 'EUR')) }
17
20
  let(:priceable_with_hash_field) {
18
21
  Priceable.create(price_hash: {
19
22
  key1: Money.new(100, "EUR"),
@@ -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'))
@@ -84,7 +91,7 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^4(.*)/
84
91
  end
85
92
 
86
93
  it "correctly mongoizes 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'))
94
+ expect(priceable_with_infinite_precision.price.cents).to eq(BigDecimal('100.1'))
88
95
  expect(priceable_with_infinite_precision.price.currency).to eq(Money::Currency.find('EUR'))
89
96
  end
90
97
  end
@@ -7,7 +7,7 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^2(.*)/
7
7
  let(:priceable_from_nil) { Priceable.create(price: nil) }
8
8
  let(:priceable_from_num) { Priceable.create(price: 1) }
9
9
  let(:priceable_from_string) { Priceable.create(price: '1 EUR' )}
10
- let(:priceable_with_infinite_precision) { Priceable.create(price: Money.new(BigDecimal.new('100.1'), 'EUR')) }
10
+ let(:priceable_with_infinite_precision) { Priceable.create(price: Money.new(BigDecimal('100.1'), 'EUR')) }
11
11
  let(:priceable_from_string_with_hyphen) { Priceable.create(price: '1-2 EUR' )}
12
12
 
13
13
  context "serialize" do
@@ -40,7 +40,7 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^2(.*)/
40
40
  end
41
41
 
42
42
  it "mongoizes correctly a Money object to a hash of cents and currency" do
43
- expect(priceable_with_infinite_precision.price.cents).to eq(BigDecimal.new('100.1'))
43
+ expect(priceable_with_infinite_precision.price.cents).to eq(BigDecimal('100.1'))
44
44
  expect(priceable_with_infinite_precision.price.currency).to eq(Money::Currency.find('EUR'))
45
45
  end
46
46
  end
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.3
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-10-16 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: money
@@ -18,14 +18,14 @@ dependencies:
18
18
  requirements:
19
19
  - - "~>"
20
20
  - !ruby/object:Gem::Version
21
- version: 6.13.0
21
+ version: 6.13.2
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - "~>"
27
27
  - !ruby/object:Gem::Version
28
- version: 6.13.0
28
+ version: 6.13.2
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: monetize
31
31
  requirement: !ruby/object:Gem::Requirement
@@ -165,6 +165,7 @@ files:
165
165
  - spec/configuration_spec.rb
166
166
  - spec/dummy/README.rdoc
167
167
  - spec/dummy/Rakefile
168
+ - spec/dummy/app/assets/config/manifest.js
168
169
  - spec/dummy/app/assets/javascripts/application.js
169
170
  - spec/dummy/app/assets/stylesheets/application.css
170
171
  - spec/dummy/app/controllers/application_controller.rb
@@ -221,9 +222,8 @@ files:
221
222
  - spec/dummy/script/rails
222
223
  - spec/helpers/action_view_extension_spec.rb
223
224
  - spec/helpers/form_helper_spec.rb
224
- - spec/mongoid/five_spec.rb
225
- - spec/mongoid/four_spec.rb
226
- - spec/mongoid/three_spec.rb
225
+ - spec/money_spec.rb
226
+ - spec/mongoid/mongoid_spec.rb
227
227
  - spec/mongoid/two_spec.rb
228
228
  - spec/spec_helper.rb
229
229
  - spec/support/database_cleaner.rb
@@ -231,7 +231,10 @@ files:
231
231
  homepage: https://github.com/RubyMoney/money-rails
232
232
  licenses:
233
233
  - MIT
234
- metadata: {}
234
+ metadata:
235
+ changelog_uri: https://github.com/RubyMoney/money-rails/blob/master/CHANGELOG.md
236
+ source_code_uri: https://github.com/RubyMoney/money-rails/
237
+ bug_tracker_uri: https://github.com/RubyMoney/money-rails/issues
235
238
  post_install_message:
236
239
  rdoc_options: []
237
240
  require_paths:
@@ -247,8 +250,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
247
250
  - !ruby/object:Gem::Version
248
251
  version: '0'
249
252
  requirements: []
250
- rubyforge_project:
251
- rubygems_version: 2.6.8
253
+ rubygems_version: 3.0.3
252
254
  signing_key:
253
255
  specification_version: 4
254
256
  summary: Money gem integration with Rails
@@ -259,6 +261,7 @@ test_files:
259
261
  - spec/configuration_spec.rb
260
262
  - spec/dummy/README.rdoc
261
263
  - spec/dummy/Rakefile
264
+ - spec/dummy/app/assets/config/manifest.js
262
265
  - spec/dummy/app/assets/javascripts/application.js
263
266
  - spec/dummy/app/assets/stylesheets/application.css
264
267
  - spec/dummy/app/controllers/application_controller.rb
@@ -315,9 +318,8 @@ test_files:
315
318
  - spec/dummy/script/rails
316
319
  - spec/helpers/action_view_extension_spec.rb
317
320
  - spec/helpers/form_helper_spec.rb
318
- - spec/mongoid/five_spec.rb
319
- - spec/mongoid/four_spec.rb
320
- - spec/mongoid/three_spec.rb
321
+ - spec/money_spec.rb
322
+ - spec/mongoid/mongoid_spec.rb
321
323
  - spec/mongoid/two_spec.rb
322
324
  - spec/spec_helper.rb
323
325
  - 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