money-rails 1.13.0 → 1.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +31 -0
- data/LICENSE +18 -18
- data/README.md +6 -5
- data/Rakefile +7 -0
- data/config/locales/money.da.yml +4 -0
- data/lib/generators/templates/money.rb +23 -0
- data/lib/money-rails/active_model/validator.rb +82 -78
- data/lib/money-rails/active_record/migration_extensions/schema_statements_pg_rails4.rb +4 -3
- data/lib/money-rails/active_record/migration_extensions/table_pg_rails4.rb +2 -1
- data/lib/money-rails/active_record/monetizable.rb +1 -1
- data/lib/money-rails/configuration.rb +1 -2
- data/lib/money-rails/helpers/action_view_extension.rb +10 -3
- data/lib/money-rails/hooks.rb +7 -1
- data/lib/money-rails/money.rb +6 -1
- data/lib/money-rails/mongoid/money.rb +28 -19
- data/lib/money-rails/rails_admin.rb +1 -1
- data/lib/money-rails/railtie.rb +1 -1
- data/lib/money-rails/version.rb +1 -1
- data/money-rails.gemspec +8 -1
- data/spec/active_record/monetizable_spec.rb +40 -30
- data/spec/configuration_spec.rb +3 -3
- data/spec/dummy/app/assets/config/manifest.js +0 -0
- data/spec/dummy/db/schema.rb +30 -31
- data/spec/helpers/action_view_extension_spec.rb +33 -0
- data/spec/money_spec.rb +40 -0
- data/spec/mongoid/{four_spec.rb → mongoid_spec.rb} +10 -3
- data/spec/mongoid/two_spec.rb +2 -2
- metadata +33 -16
- data/spec/mongoid/five_spec.rb +0 -117
- data/spec/mongoid/three_spec.rb +0 -122
@@ -5,6 +5,18 @@ describe 'MoneyRails::ActionViewExtension', type: :helper do
|
|
5
5
|
subject { helper.currency_symbol }
|
6
6
|
it { is_expected.to be_a String }
|
7
7
|
it { is_expected.to include Money.default_currency.symbol }
|
8
|
+
|
9
|
+
context 'with given currency' do
|
10
|
+
subject { helper.currency_symbol(Money::Currency.find(:brl)) }
|
11
|
+
it { is_expected.to include Money::Currency.find(:brl).symbol }
|
12
|
+
it { is_expected.to include Money::Currency.find(:brl).symbol }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'with given currency symbol' do
|
16
|
+
subject { helper.currency_symbol(:brl) }
|
17
|
+
it { is_expected.to include Money::Currency.find(:brl).symbol }
|
18
|
+
it { is_expected.to include Money::Currency.find(:brl).symbol }
|
19
|
+
end
|
8
20
|
end
|
9
21
|
|
10
22
|
describe '#humanized_money' do
|
@@ -74,6 +86,27 @@ describe 'MoneyRails::ActionViewExtension', type: :helper do
|
|
74
86
|
it { is_expected.not_to include "00" }
|
75
87
|
end
|
76
88
|
|
89
|
+
describe '#money_only_cents' do
|
90
|
+
let(:monetizable_object){ Money.new(125_00) }
|
91
|
+
subject { helper.money_only_cents monetizable_object }
|
92
|
+
it { is_expected.to eq "00" }
|
93
|
+
|
94
|
+
context 'with a non-money object' do
|
95
|
+
let(:monetizable_object){ 125 }
|
96
|
+
it { is_expected.to eq "00" }
|
97
|
+
end
|
98
|
+
|
99
|
+
context 'with less than 10 cents' do
|
100
|
+
let(:monetizable_object){ Money.new(8) }
|
101
|
+
it { is_expected.to eq "08" }
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'with a non monetizable object' do
|
105
|
+
let(:monetizable_object){ false }
|
106
|
+
it { is_expected.to eq "00" }
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
77
110
|
context 'respects MoneyRails::Configuration settings' do
|
78
111
|
context 'with no_cents_if_whole: false' do
|
79
112
|
|
data/spec/money_spec.rb
ADDED
@@ -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
|
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
|
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
|
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
|
data/spec/mongoid/two_spec.rb
CHANGED
@@ -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
|
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
|
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,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: money-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.14.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andreas Loupasakis
|
8
8
|
- Shane Emmons
|
9
9
|
- Simone Carletti
|
10
|
-
autorequire:
|
10
|
+
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2021-03-02 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.
|
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.
|
28
|
+
version: 6.13.2
|
29
29
|
- !ruby/object:Gem::Dependency
|
30
30
|
name: monetize
|
31
31
|
requirement: !ruby/object:Gem::Requirement
|
@@ -124,6 +124,20 @@ dependencies:
|
|
124
124
|
- - "~>"
|
125
125
|
- !ruby/object:Gem::Version
|
126
126
|
version: '3.0'
|
127
|
+
- !ruby/object:Gem::Dependency
|
128
|
+
name: bundler
|
129
|
+
requirement: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
127
141
|
description: This library provides integration of RubyMoney - Money gem with Rails
|
128
142
|
email:
|
129
143
|
- alup.rubymoney@gmail.com
|
@@ -135,6 +149,7 @@ files:
|
|
135
149
|
- LICENSE
|
136
150
|
- README.md
|
137
151
|
- Rakefile
|
152
|
+
- config/locales/money.da.yml
|
138
153
|
- config/locales/money.en.yml
|
139
154
|
- lib/generators/money_rails/initializer_generator.rb
|
140
155
|
- lib/generators/templates/money.rb
|
@@ -165,6 +180,7 @@ files:
|
|
165
180
|
- spec/configuration_spec.rb
|
166
181
|
- spec/dummy/README.rdoc
|
167
182
|
- spec/dummy/Rakefile
|
183
|
+
- spec/dummy/app/assets/config/manifest.js
|
168
184
|
- spec/dummy/app/assets/javascripts/application.js
|
169
185
|
- spec/dummy/app/assets/stylesheets/application.css
|
170
186
|
- spec/dummy/app/controllers/application_controller.rb
|
@@ -221,9 +237,8 @@ files:
|
|
221
237
|
- spec/dummy/script/rails
|
222
238
|
- spec/helpers/action_view_extension_spec.rb
|
223
239
|
- spec/helpers/form_helper_spec.rb
|
224
|
-
- spec/
|
225
|
-
- spec/mongoid/
|
226
|
-
- spec/mongoid/three_spec.rb
|
240
|
+
- spec/money_spec.rb
|
241
|
+
- spec/mongoid/mongoid_spec.rb
|
227
242
|
- spec/mongoid/two_spec.rb
|
228
243
|
- spec/spec_helper.rb
|
229
244
|
- spec/support/database_cleaner.rb
|
@@ -231,8 +246,11 @@ files:
|
|
231
246
|
homepage: https://github.com/RubyMoney/money-rails
|
232
247
|
licenses:
|
233
248
|
- MIT
|
234
|
-
metadata:
|
235
|
-
|
249
|
+
metadata:
|
250
|
+
changelog_uri: https://github.com/RubyMoney/money-rails/blob/master/CHANGELOG.md
|
251
|
+
source_code_uri: https://github.com/RubyMoney/money-rails/
|
252
|
+
bug_tracker_uri: https://github.com/RubyMoney/money-rails/issues
|
253
|
+
post_install_message:
|
236
254
|
rdoc_options: []
|
237
255
|
require_paths:
|
238
256
|
- lib
|
@@ -247,9 +265,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
247
265
|
- !ruby/object:Gem::Version
|
248
266
|
version: '0'
|
249
267
|
requirements: []
|
250
|
-
|
251
|
-
|
252
|
-
signing_key:
|
268
|
+
rubygems_version: 3.2.3
|
269
|
+
signing_key:
|
253
270
|
specification_version: 4
|
254
271
|
summary: Money gem integration with Rails
|
255
272
|
test_files:
|
@@ -259,6 +276,7 @@ test_files:
|
|
259
276
|
- spec/configuration_spec.rb
|
260
277
|
- spec/dummy/README.rdoc
|
261
278
|
- spec/dummy/Rakefile
|
279
|
+
- spec/dummy/app/assets/config/manifest.js
|
262
280
|
- spec/dummy/app/assets/javascripts/application.js
|
263
281
|
- spec/dummy/app/assets/stylesheets/application.css
|
264
282
|
- spec/dummy/app/controllers/application_controller.rb
|
@@ -315,9 +333,8 @@ test_files:
|
|
315
333
|
- spec/dummy/script/rails
|
316
334
|
- spec/helpers/action_view_extension_spec.rb
|
317
335
|
- spec/helpers/form_helper_spec.rb
|
318
|
-
- spec/
|
319
|
-
- spec/mongoid/
|
320
|
-
- spec/mongoid/three_spec.rb
|
336
|
+
- spec/money_spec.rb
|
337
|
+
- spec/mongoid/mongoid_spec.rb
|
321
338
|
- spec/mongoid/two_spec.rb
|
322
339
|
- spec/spec_helper.rb
|
323
340
|
- spec/support/database_cleaner.rb
|
data/spec/mongoid/five_spec.rb
DELETED
@@ -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
|
data/spec/mongoid/three_spec.rb
DELETED
@@ -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
|