money-rails 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -53
- data/lib/generators/templates/money.rb +7 -4
- data/lib/money-rails/active_model/validator.rb +1 -1
- data/lib/money-rails/active_record/monetizable.rb +23 -7
- data/lib/money-rails/configuration.rb +5 -2
- data/lib/money-rails/money.rb +4 -1
- data/lib/money-rails/mongoid/money.rb +2 -2
- data/lib/money-rails/test_helpers.rb +40 -18
- data/lib/money-rails/version.rb +1 -1
- data/money-rails.gemspec +1 -1
- data/spec/active_record/migration_extensions/schema_statements_spec.rb +8 -8
- data/spec/active_record/migration_extensions/table_spec.rb +8 -8
- data/spec/active_record/monetizable_spec.rb +195 -166
- data/spec/configuration_spec.rb +39 -22
- data/spec/dummy/app/models/dummy_product.rb +1 -0
- data/spec/dummy/app/models/product.rb +4 -0
- data/spec/dummy/config/application.rb +3 -2
- data/spec/dummy/config/locales/en-US.yml +5 -0
- data/spec/dummy/db/migrate/20141005075025_add_aliased_attr_to_products.rb +5 -0
- data/spec/dummy/db/schema.rb +2 -1
- data/spec/helpers/action_view_extension_spec.rb +45 -45
- data/spec/helpers/form_helper_spec.rb +2 -2
- data/spec/mongoid/four_spec.rb +23 -23
- data/spec/mongoid/three_spec.rb +23 -23
- data/spec/mongoid/two_spec.rb +13 -13
- data/spec/spec_helper.rb +0 -2
- data/spec/test_helpers_spec.rb +34 -22
- metadata +8 -4
data/spec/configuration_spec.rb
CHANGED
@@ -5,16 +5,16 @@ describe "configuration" do
|
|
5
5
|
describe "initializer" do
|
6
6
|
|
7
7
|
it "sets default currency" do
|
8
|
-
Money.default_currency.
|
8
|
+
expect(Money.default_currency).to eq(Money::Currency.new(:eur))
|
9
9
|
end
|
10
10
|
|
11
11
|
it "registers a custom currency" do
|
12
|
-
Money::Currency.table.
|
12
|
+
expect(Money::Currency.table).to include(:eu4)
|
13
13
|
end
|
14
14
|
|
15
15
|
it "adds exchange rates given in config initializer" do
|
16
|
-
Money.us_dollar(100).exchange_to("CAD").
|
17
|
-
Money.ca_dollar(100).exchange_to("USD").
|
16
|
+
expect(Money.us_dollar(100).exchange_to("CAD")).to eq(Money.new(124, "CAD"))
|
17
|
+
expect(Money.ca_dollar(100).exchange_to("USD")).to eq(Money.new(80, "USD"))
|
18
18
|
end
|
19
19
|
|
20
20
|
it "sets no_cents_if_whole value for formatted output globally" do
|
@@ -23,15 +23,15 @@ describe "configuration" do
|
|
23
23
|
|
24
24
|
value = Money.new(12345600, "EUR")
|
25
25
|
mark = Money::Currency.find(:eur).decimal_mark
|
26
|
-
value.format.
|
26
|
+
expect(value.format).to match(/#{mark}/)
|
27
27
|
|
28
28
|
MoneyRails.no_cents_if_whole = true
|
29
|
-
value.format.
|
30
|
-
value.format(no_cents_if_whole: false).
|
29
|
+
expect(value.format).not_to match(/#{mark}/)
|
30
|
+
expect(value.format(no_cents_if_whole: false)).to match(/#{mark}/)
|
31
31
|
|
32
32
|
MoneyRails.no_cents_if_whole = false
|
33
|
-
value.format.
|
34
|
-
value.format(no_cents_if_whole: true).
|
33
|
+
expect(value.format).to match(/#{mark}/)
|
34
|
+
expect(value.format(no_cents_if_whole: true)).not_to match(/#{mark}/)
|
35
35
|
|
36
36
|
# Reset global settings
|
37
37
|
MoneyRails.no_cents_if_whole = nil
|
@@ -41,15 +41,15 @@ describe "configuration" do
|
|
41
41
|
it "sets symbol for formatted output globally" do
|
42
42
|
value = Money.new(12345600, "EUR")
|
43
43
|
symbol = Money::Currency.find(:eur).symbol
|
44
|
-
value.format.
|
44
|
+
expect(value.format).to match(/#{symbol}/)
|
45
45
|
|
46
46
|
MoneyRails.symbol = false
|
47
|
-
value.format.
|
48
|
-
value.format(symbol: true).
|
47
|
+
expect(value.format).not_to match(/#{symbol}/)
|
48
|
+
expect(value.format(symbol: true)).to match(/#{symbol}/)
|
49
49
|
|
50
50
|
MoneyRails.symbol = true
|
51
|
-
value.format.
|
52
|
-
value.format(symbol: false).
|
51
|
+
expect(value.format).to match(/#{symbol}/)
|
52
|
+
expect(value.format(symbol: false)).not_to match(/#{symbol}/)
|
53
53
|
|
54
54
|
# Reset global setting
|
55
55
|
MoneyRails.symbol = nil
|
@@ -58,26 +58,43 @@ describe "configuration" do
|
|
58
58
|
it "sets the location of the negative sign for formatted output globally" do
|
59
59
|
value = Money.new(-12345600, "EUR")
|
60
60
|
symbol = Money::Currency.find(:eur).symbol
|
61
|
-
value.format.
|
61
|
+
expect(value.format).to match(/#{symbol}-/)
|
62
62
|
|
63
63
|
MoneyRails.sign_before_symbol = false
|
64
|
-
value.format.
|
65
|
-
value.format(sign_before_symbol: false).
|
64
|
+
expect(value.format).to match(/#{symbol}-/)
|
65
|
+
expect(value.format(sign_before_symbol: false)).to match(/#{symbol}-/)
|
66
66
|
|
67
67
|
MoneyRails.sign_before_symbol = true
|
68
|
-
value.format.
|
69
|
-
value.format(sign_before_symbol: true).
|
68
|
+
expect(value.format).to match(/-#{symbol}/)
|
69
|
+
expect(value.format(sign_before_symbol: true)).to match(/-#{symbol}/)
|
70
70
|
|
71
71
|
# Reset global setting
|
72
72
|
MoneyRails.sign_before_symbol = nil
|
73
73
|
end
|
74
74
|
|
75
|
+
it "passes through arbitrary format options" do
|
76
|
+
value = Money.new(-12345600, "EUR")
|
77
|
+
symbol = Money::Currency.find(:eur).symbol
|
78
|
+
|
79
|
+
MoneyRails.default_format = {:symbol_position => :after}
|
80
|
+
expect(value.format).to match(/#{symbol}\z/)
|
81
|
+
|
82
|
+
# Override with "classic" format options for backward compatibility
|
83
|
+
MoneyRails.default_format = {:sign_before_symbol => :false}
|
84
|
+
MoneyRails.sign_before_symbol = true
|
85
|
+
expect(value.format).to match(/-#{symbol}/)
|
86
|
+
|
87
|
+
# Reset global settings
|
88
|
+
MoneyRails.sign_before_symbol = nil
|
89
|
+
MoneyRails.default_format = nil
|
90
|
+
end
|
91
|
+
|
75
92
|
it "changes the amount and currency column settings based on the default currency" do
|
76
93
|
old_currency = MoneyRails.default_currency
|
77
94
|
MoneyRails.default_currency = :inr
|
78
95
|
|
79
|
-
MoneyRails.amount_column[:postfix].
|
80
|
-
MoneyRails.currency_column[:default].
|
96
|
+
expect(MoneyRails.amount_column[:postfix]).to eq("_#{MoneyRails.default_currency.subunit.downcase.pluralize}")
|
97
|
+
expect(MoneyRails.currency_column[:default]).to eq(MoneyRails.default_currency.iso_code)
|
81
98
|
|
82
99
|
# Reset global setting
|
83
100
|
MoneyRails.default_currency = old_currency
|
@@ -90,7 +107,7 @@ describe "configuration" do
|
|
90
107
|
MoneyRails.default_currency = :jpy
|
91
108
|
}.to_not raise_error
|
92
109
|
|
93
|
-
MoneyRails.amount_column[:postfix].
|
110
|
+
expect(MoneyRails.amount_column[:postfix]).to eq("_cents")
|
94
111
|
|
95
112
|
# Reset global setting
|
96
113
|
MoneyRails.default_currency = old_currency
|
@@ -15,7 +15,9 @@ require "money-rails"
|
|
15
15
|
module Dummy
|
16
16
|
class Application < Rails::Application
|
17
17
|
|
18
|
-
I18n.enforce_available_locales
|
18
|
+
if I18n.respond_to?(:enforce_available_locales)
|
19
|
+
I18n.enforce_available_locales = false # removes deprecation warning
|
20
|
+
end
|
19
21
|
|
20
22
|
# Settings in config/environments/* take precedence over those specified here.
|
21
23
|
# Application configuration should go into files in config/initializers
|
@@ -59,4 +61,3 @@ module Dummy
|
|
59
61
|
config.assets.version = '1.0'
|
60
62
|
end
|
61
63
|
end
|
62
|
-
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
#
|
12
12
|
# It's strongly recommended that you check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(version:
|
14
|
+
ActiveRecord::Schema.define(version: 20141005075025) do
|
15
15
|
|
16
16
|
create_table "dummy_products", force: true do |t|
|
17
17
|
t.string "currency"
|
@@ -31,6 +31,7 @@ ActiveRecord::Schema.define(version: 20140110194016) do
|
|
31
31
|
t.string "sale_price_currency_code"
|
32
32
|
t.integer "price_in_a_range_cents"
|
33
33
|
t.integer "validates_method_amount_cents"
|
34
|
+
t.integer "aliased_cents"
|
34
35
|
end
|
35
36
|
|
36
37
|
create_table "services", force: true do |t|
|
@@ -1,62 +1,62 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe 'MoneyRails::ActionViewExtension' do
|
3
|
+
describe 'MoneyRails::ActionViewExtension', :type => :helper do
|
4
4
|
describe '#currency_symbol' do
|
5
5
|
subject { helper.currency_symbol }
|
6
|
-
it {
|
7
|
-
it {
|
6
|
+
it { is_expected.to be_a String }
|
7
|
+
it { is_expected.to include Money.default_currency.symbol }
|
8
8
|
end
|
9
9
|
|
10
10
|
describe '#humanized_money' do
|
11
11
|
let(:options) { {} }
|
12
12
|
subject { helper.humanized_money Money.new(12500), options }
|
13
|
-
it {
|
14
|
-
it {
|
15
|
-
it {
|
13
|
+
it { is_expected.to be_a String }
|
14
|
+
it { is_expected.not_to include Money.default_currency.symbol }
|
15
|
+
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
16
16
|
|
17
17
|
context 'with symbol options' do
|
18
18
|
let(:options) { { :symbol => true } }
|
19
|
-
it {
|
19
|
+
it { is_expected.to include Money.default_currency.symbol }
|
20
20
|
end
|
21
21
|
|
22
22
|
context 'with deprecated symbol' do
|
23
23
|
let(:options) { true }
|
24
24
|
before(:each) do
|
25
|
-
helper.
|
25
|
+
expect(helper).to receive(:warn)
|
26
26
|
end
|
27
|
-
it {
|
27
|
+
it { is_expected.to include Money.default_currency.symbol }
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
31
|
describe '#humanized_money_with_symbol' do
|
32
32
|
subject { helper.humanized_money_with_symbol Money.new(12500) }
|
33
|
-
it {
|
34
|
-
it {
|
35
|
-
it {
|
33
|
+
it { is_expected.to be_a String }
|
34
|
+
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
35
|
+
it { is_expected.to include Money.default_currency.symbol }
|
36
36
|
end
|
37
37
|
|
38
38
|
describe '#money_without_cents' do
|
39
39
|
let(:options) { {} }
|
40
40
|
subject { helper.money_without_cents Money.new(12500), options }
|
41
|
-
it {
|
42
|
-
it {
|
43
|
-
it {
|
41
|
+
it { is_expected.to be_a String }
|
42
|
+
it { is_expected.not_to include Money.default_currency.symbol }
|
43
|
+
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
44
44
|
|
45
45
|
context 'with deprecated symbol' do
|
46
46
|
let(:options) { true }
|
47
47
|
before(:each) do
|
48
|
-
helper.
|
48
|
+
expect(helper).to receive(:warn)
|
49
49
|
end
|
50
|
-
it {
|
50
|
+
it { is_expected.to include Money.default_currency.symbol }
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
54
|
describe '#money_without_cents_and_with_symbol' do
|
55
55
|
subject { helper.money_without_cents_and_with_symbol Money.new(12500) }
|
56
|
-
it {
|
57
|
-
it {
|
58
|
-
it {
|
59
|
-
it {
|
56
|
+
it { is_expected.to be_a String }
|
57
|
+
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
58
|
+
it { is_expected.to include Money.default_currency.symbol }
|
59
|
+
it { is_expected.not_to include "00" }
|
60
60
|
end
|
61
61
|
|
62
62
|
context 'respects MoneyRails::Configuration settings' do
|
@@ -70,18 +70,18 @@ describe 'MoneyRails::ActionViewExtension' do
|
|
70
70
|
|
71
71
|
describe '#humanized_money' do
|
72
72
|
subject { helper.humanized_money Money.new(12500) }
|
73
|
-
it {
|
74
|
-
it {
|
75
|
-
it {
|
76
|
-
it {
|
73
|
+
it { is_expected.to be_a String }
|
74
|
+
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
75
|
+
it { is_expected.not_to include Money.default_currency.symbol }
|
76
|
+
it { is_expected.to include "00" }
|
77
77
|
end
|
78
78
|
|
79
79
|
describe '#humanized_money_with_symbol' do
|
80
80
|
subject { helper.humanized_money_with_symbol Money.new(12500) }
|
81
|
-
it {
|
82
|
-
it {
|
83
|
-
it {
|
84
|
-
it {
|
81
|
+
it { is_expected.to be_a String }
|
82
|
+
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
83
|
+
it { is_expected.to include Money.default_currency.symbol }
|
84
|
+
it { is_expected.to include "00" }
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
@@ -95,18 +95,18 @@ describe 'MoneyRails::ActionViewExtension' do
|
|
95
95
|
|
96
96
|
describe '#humanized_money' do
|
97
97
|
subject { helper.humanized_money Money.new(12500) }
|
98
|
-
it {
|
99
|
-
it {
|
100
|
-
it {
|
101
|
-
it {
|
98
|
+
it { is_expected.to be_a String }
|
99
|
+
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
100
|
+
it { is_expected.not_to include Money.default_currency.symbol }
|
101
|
+
it { is_expected.not_to include "00" }
|
102
102
|
end
|
103
103
|
|
104
104
|
describe '#humanized_money_with_symbol' do
|
105
105
|
subject { helper.humanized_money_with_symbol Money.new(12500) }
|
106
|
-
it {
|
107
|
-
it {
|
108
|
-
it {
|
109
|
-
it {
|
106
|
+
it { is_expected.to be_a String }
|
107
|
+
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
108
|
+
it { is_expected.to include Money.default_currency.symbol }
|
109
|
+
it { is_expected.not_to include "00" }
|
110
110
|
end
|
111
111
|
end
|
112
112
|
|
@@ -120,18 +120,18 @@ describe 'MoneyRails::ActionViewExtension' do
|
|
120
120
|
|
121
121
|
describe '#humanized_money' do
|
122
122
|
subject { helper.humanized_money Money.new(12500) }
|
123
|
-
it {
|
124
|
-
it {
|
125
|
-
it {
|
126
|
-
it {
|
123
|
+
it { is_expected.to be_a String }
|
124
|
+
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
125
|
+
it { is_expected.not_to include Money.default_currency.symbol }
|
126
|
+
it { is_expected.not_to include "00" }
|
127
127
|
end
|
128
128
|
|
129
129
|
describe '#humanized_money_with_symbol' do
|
130
130
|
subject { helper.humanized_money_with_symbol Money.new(12500) }
|
131
|
-
it {
|
132
|
-
it {
|
133
|
-
it {
|
134
|
-
it {
|
131
|
+
it { is_expected.to be_a String }
|
132
|
+
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
133
|
+
it { is_expected.to include Money.default_currency.symbol }
|
134
|
+
it { is_expected.not_to include "00" }
|
135
135
|
end
|
136
136
|
end
|
137
137
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
if defined? ActiveRecord
|
4
|
-
describe "Test helper in form blocks" do
|
4
|
+
describe "Test helper in form blocks", :type => :helper do
|
5
5
|
|
6
6
|
let :product do
|
7
7
|
Product.create(:price_cents => 3000, :discount => 150,
|
@@ -12,7 +12,7 @@ if defined? ActiveRecord
|
|
12
12
|
context "textfield" do
|
13
13
|
it "uses the current value of money field in textfield" do
|
14
14
|
helper.instance_variable_set :@product, product
|
15
|
-
helper.text_field(:product, :price).
|
15
|
+
expect(helper.text_field(:product, :price)).to match(/value=\"#{product.price.to_s}\"/)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
data/spec/mongoid/four_spec.rb
CHANGED
@@ -22,18 +22,18 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^4(.*)/
|
|
22
22
|
|
23
23
|
context "mongoize" do
|
24
24
|
it "correctly mongoizes a Money object to a hash of cents and currency" do
|
25
|
-
priceable.price.cents.
|
26
|
-
priceable.price.currency.
|
25
|
+
expect(priceable.price.cents).to eq(100)
|
26
|
+
expect(priceable.price.currency).to eq(Money::Currency.find('EUR'))
|
27
27
|
end
|
28
28
|
|
29
29
|
it "correctly mongoizes a Numeric object to a hash of cents and currency" do
|
30
|
-
priceable_from_num.price.cents.
|
31
|
-
priceable_from_num.price.currency.
|
30
|
+
expect(priceable_from_num.price.cents).to eq(100)
|
31
|
+
expect(priceable_from_num.price.currency).to eq(Money.default_currency)
|
32
32
|
end
|
33
33
|
|
34
34
|
it "correctly mongoizes a String object to a hash of cents and currency" do
|
35
|
-
priceable_from_string.price.cents.
|
36
|
-
priceable_from_string.price.currency.
|
35
|
+
expect(priceable_from_string.price.cents).to eq(100)
|
36
|
+
expect(priceable_from_string.price.currency).to eq(Money::Currency.find('EUR'))
|
37
37
|
end
|
38
38
|
|
39
39
|
context "when MoneyRails.raise_error_on_money_parsing is true" do
|
@@ -51,22 +51,22 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^4(.*)/
|
|
51
51
|
|
52
52
|
context "when MoneyRails.raise_error_on_money_parsing is false" do
|
53
53
|
it "does not correctly mongoize a String with a hyphen in its middle" do
|
54
|
-
priceable_from_string_with_hyphen.price.
|
54
|
+
expect(priceable_from_string_with_hyphen.price).to eq(nil)
|
55
55
|
end
|
56
56
|
|
57
57
|
it "does not correctly mongoize a String with an unknown currency" do
|
58
|
-
priceable_from_string_with_unknown_currency.price.
|
58
|
+
expect(priceable_from_string_with_unknown_currency.price).to eq(nil)
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
62
|
it "correctly mongoizes a hash of cents and currency" do
|
63
|
-
priceable_from_hash.price.cents.
|
64
|
-
priceable_from_hash.price.currency.
|
63
|
+
expect(priceable_from_hash.price.cents).to eq(100)
|
64
|
+
expect(priceable_from_hash.price.currency).to eq(Money::Currency.find('EUR'))
|
65
65
|
end
|
66
66
|
|
67
67
|
it "correctly mongoizes a HashWithIndifferentAccess of cents and currency" do
|
68
|
-
priceable_from_hash_with_indifferent_access.price.cents.
|
69
|
-
priceable_from_hash_with_indifferent_access.price.currency.
|
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
70
|
end
|
71
71
|
|
72
72
|
context "infinite_precision = true" do
|
@@ -79,36 +79,36 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^4(.*)/
|
|
79
79
|
end
|
80
80
|
|
81
81
|
it "correctly mongoizes a Money object to a hash of cents and currency" do
|
82
|
-
priceable_with_infinite_precision.price.cents.
|
83
|
-
priceable_with_infinite_precision.price.currency.
|
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
84
|
end
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
88
|
it "correctly serializes a Hash field containing Money objects" do
|
89
|
-
priceable_with_hash_field.price_hash[:key1][:cents].
|
90
|
-
priceable_with_hash_field.price_hash[:key2][:cents].
|
91
|
-
priceable_with_hash_field.price_hash[:key1][:currency_iso].
|
92
|
-
priceable_with_hash_field.price_hash[:key2][:currency_iso].
|
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
93
|
end
|
94
94
|
|
95
95
|
context "demongoize" do
|
96
96
|
subject { Priceable.first.price }
|
97
|
-
it {
|
98
|
-
it {
|
97
|
+
it { is_expected.to be_an_instance_of(Money) }
|
98
|
+
it { is_expected.to eq(Money.new(100, 'EUR')) }
|
99
99
|
it "returns nil if a nil value was stored" do
|
100
100
|
nil_priceable = Priceable.create(:price => nil)
|
101
|
-
nil_priceable.price.
|
101
|
+
expect(nil_priceable.price).to be_nil
|
102
102
|
end
|
103
103
|
it 'returns nil if an unknown value was stored' do
|
104
104
|
zero_priceable = Priceable.create(:price => [])
|
105
|
-
zero_priceable.price.
|
105
|
+
expect(zero_priceable.price).to be_nil
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
109
109
|
context "evolve" do
|
110
110
|
it "correctly transforms a Money object into a Mongo friendly value" do
|
111
|
-
Priceable.where(:price => Money.new(100, 'EUR')).first.
|
111
|
+
expect(Priceable.where(:price => Money.new(100, 'EUR')).first).to eq(priceable)
|
112
112
|
end
|
113
113
|
end
|
114
114
|
end
|
data/spec/mongoid/three_spec.rb
CHANGED
@@ -22,18 +22,18 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^3(.*)/
|
|
22
22
|
|
23
23
|
context "mongoize" do
|
24
24
|
it "mongoizes correctly a Money object to a hash of cents and currency" do
|
25
|
-
priceable.price.cents.
|
26
|
-
priceable.price.currency.
|
25
|
+
expect(priceable.price.cents).to eq(100)
|
26
|
+
expect(priceable.price.currency).to eq(Money::Currency.find('EUR'))
|
27
27
|
end
|
28
28
|
|
29
29
|
it "mongoizes correctly a Numeric object to a hash of cents and currency" do
|
30
|
-
priceable_from_num.price.cents.
|
31
|
-
priceable_from_num.price.currency.
|
30
|
+
expect(priceable_from_num.price.cents).to eq(100)
|
31
|
+
expect(priceable_from_num.price.currency).to eq(Money.default_currency)
|
32
32
|
end
|
33
33
|
|
34
34
|
it "mongoizes correctly a String object to a hash of cents and currency" do
|
35
|
-
priceable_from_string.price.cents.
|
36
|
-
priceable_from_string.price.currency.
|
35
|
+
expect(priceable_from_string.price.cents).to eq(100)
|
36
|
+
expect(priceable_from_string.price.currency).to eq(Money::Currency.find('EUR'))
|
37
37
|
end
|
38
38
|
|
39
39
|
context "when MoneyRails.raise_error_on_money_parsing is true" do
|
@@ -51,22 +51,22 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^3(.*)/
|
|
51
51
|
|
52
52
|
context "when MoneyRails.raise_error_on_money_parsing is false" do
|
53
53
|
it "does not mongoizes correctly a String with hyphen in its middle" do
|
54
|
-
priceable_from_string_with_hyphen.price.
|
54
|
+
expect(priceable_from_string_with_hyphen.price).to eq(nil)
|
55
55
|
end
|
56
56
|
|
57
57
|
it "does not mongoize correctly a String with an unknown currency" do
|
58
|
-
priceable_from_string_with_unknown_currency.price.
|
58
|
+
expect(priceable_from_string_with_unknown_currency.price).to eq(nil)
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
62
|
it "mongoizes correctly a hash of cents and currency" do
|
63
|
-
priceable_from_hash.price.cents.
|
64
|
-
priceable_from_hash.price.currency.
|
63
|
+
expect(priceable_from_hash.price.cents).to eq(100)
|
64
|
+
expect(priceable_from_hash.price.currency).to eq(Money::Currency.find('EUR'))
|
65
65
|
end
|
66
66
|
|
67
67
|
it "mongoizes correctly a HashWithIndifferentAccess of cents and currency" do
|
68
|
-
priceable_from_hash_with_indifferent_access.price.cents.
|
69
|
-
priceable_from_hash_with_indifferent_access.price.currency.
|
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
70
|
end
|
71
71
|
|
72
72
|
context "infinite_precision = true" do
|
@@ -79,36 +79,36 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^3(.*)/
|
|
79
79
|
end
|
80
80
|
|
81
81
|
it "mongoizes correctly a Money object to a hash of cents and currency" do
|
82
|
-
priceable_with_infinite_precision.price.cents.
|
83
|
-
priceable_with_infinite_precision.price.currency.
|
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
84
|
end
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
88
|
it "serializes correctly a Hash field containing Money objects" do
|
89
|
-
priceable_with_hash_field.price_hash[:key1][:cents].
|
90
|
-
priceable_with_hash_field.price_hash[:key2][:cents].
|
91
|
-
priceable_with_hash_field.price_hash[:key1][:currency_iso].
|
92
|
-
priceable_with_hash_field.price_hash[:key2][:currency_iso].
|
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
93
|
end
|
94
94
|
|
95
95
|
context "demongoize" do
|
96
96
|
subject { Priceable.first.price }
|
97
|
-
it {
|
98
|
-
it {
|
97
|
+
it { is_expected.to be_an_instance_of(Money) }
|
98
|
+
it { is_expected.to eq(Money.new(100, 'EUR')) }
|
99
99
|
it "returns nil if a nil value was stored" do
|
100
100
|
nil_priceable = Priceable.create(:price => nil)
|
101
|
-
nil_priceable.price.
|
101
|
+
expect(nil_priceable.price).to be_nil
|
102
102
|
end
|
103
103
|
it 'returns nil if an unknown value was stored' do
|
104
104
|
zero_priceable = Priceable.create(:price => [])
|
105
|
-
zero_priceable.price.
|
105
|
+
expect(zero_priceable.price).to be_nil
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
109
109
|
context "evolve" do
|
110
110
|
it "transforms correctly a Money object to a Mongo friendly value" do
|
111
|
-
Priceable.where(:price => Money.new(100, 'EUR')).first.
|
111
|
+
expect(Priceable.where(:price => Money.new(100, 'EUR')).first).to eq(priceable)
|
112
112
|
end
|
113
113
|
end
|
114
114
|
end
|
data/spec/mongoid/two_spec.rb
CHANGED
@@ -11,18 +11,18 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^2(.*)/
|
|
11
11
|
|
12
12
|
context "serialize" do
|
13
13
|
it "serializes correctly a Money object to a hash of cents and currency" do
|
14
|
-
priceable.price.cents.
|
15
|
-
priceable.price.currency.
|
14
|
+
expect(priceable.price.cents).to eq(100)
|
15
|
+
expect(priceable.price.currency).to eq(Money::Currency.find('EUR'))
|
16
16
|
end
|
17
17
|
|
18
18
|
it "mongoizes correctly a Numeric object to a hash of cents and currency" do
|
19
|
-
priceable_from_num.price.cents.
|
20
|
-
priceable_from_num.price.currency.
|
19
|
+
expect(priceable_from_num.price.cents).to eq(100)
|
20
|
+
expect(priceable_from_num.price.currency).to eq(Money.default_currency)
|
21
21
|
end
|
22
22
|
|
23
23
|
it "mongoizes correctly a String object to a hash of cents and currency" do
|
24
|
-
priceable_from_string.price.cents.
|
25
|
-
priceable_from_string.price.currency.
|
24
|
+
expect(priceable_from_string.price.cents).to eq(100)
|
25
|
+
expect(priceable_from_string.price.currency).to eq(Money::Currency.find('EUR'))
|
26
26
|
end
|
27
27
|
|
28
28
|
context "infinite_precision = true" do
|
@@ -35,8 +35,8 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^2(.*)/
|
|
35
35
|
end
|
36
36
|
|
37
37
|
it "mongoizes correctly a Money object to a hash of cents and currency" do
|
38
|
-
priceable_with_infinite_precision.price.cents.
|
39
|
-
priceable_with_infinite_precision.price.currency.
|
38
|
+
expect(priceable_with_infinite_precision.price.cents).to eq(BigDecimal.new('100.1'))
|
39
|
+
expect(priceable_with_infinite_precision.price.currency).to eq(Money::Currency.find('EUR'))
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -51,22 +51,22 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^2(.*)/
|
|
51
51
|
|
52
52
|
context "when MoneyRails.raise_error_on_money_parsing is false" do
|
53
53
|
it "does not mongoizes correctly a String with hyphen in its middle" do
|
54
|
-
priceable_from_string_with_hyphen.price.
|
54
|
+
expect(priceable_from_string_with_hyphen.price).to eq(nil)
|
55
55
|
end
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
59
|
context "deserialize" do
|
60
60
|
subject { priceable.price }
|
61
|
-
it {
|
62
|
-
it {
|
61
|
+
it { is_expected.to be_an_instance_of(Money) }
|
62
|
+
it { is_expected.to eq(Money.new(100, 'EUR')) }
|
63
63
|
it "returns nil if a nil value was stored" do
|
64
64
|
nil_priceable = Priceable.create(:price => nil)
|
65
|
-
nil_priceable.price.
|
65
|
+
expect(nil_priceable.price).to be_nil
|
66
66
|
end
|
67
67
|
it 'returns nil if an unknown value was stored' do
|
68
68
|
zero_priceable = Priceable.create(:price => [])
|
69
|
-
zero_priceable.price.
|
69
|
+
expect(zero_priceable.price).to be_nil
|
70
70
|
end
|
71
71
|
end
|
72
72
|
end
|