money-rails 1.15.0 → 2.0.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 +4 -4
- data/CHANGELOG.md +24 -6
- data/LICENSE +1 -1
- data/README.md +121 -102
- data/config/locales/money.pt.yml +4 -0
- data/lib/money-rails/active_job/money_serializer.rb +19 -0
- data/lib/money-rails/active_record/monetizable.rb +17 -7
- data/lib/money-rails/configuration.rb +2 -2
- data/lib/money-rails/helpers/action_view_extension.rb +1 -1
- data/lib/money-rails/hooks.rb +11 -0
- data/lib/money-rails/version.rb +3 -1
- data/money-rails.gemspec +13 -26
- metadata +16 -225
- data/Rakefile +0 -84
- data/spec/active_record/migration_extensions/schema_statements_spec.rb +0 -101
- data/spec/active_record/migration_extensions/table_spec.rb +0 -104
- data/spec/active_record/monetizable_spec.rb +0 -1062
- data/spec/configuration_spec.rb +0 -147
- data/spec/dummy/README.rdoc +0 -261
- data/spec/dummy/Rakefile +0 -7
- data/spec/dummy/app/assets/config/manifest.js +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +0 -15
- data/spec/dummy/app/assets/stylesheets/application.css +0 -13
- data/spec/dummy/app/controllers/application_controller.rb +0 -3
- data/spec/dummy/app/helpers/application_helper.rb +0 -2
- data/spec/dummy/app/models/dummy_product.rb +0 -8
- data/spec/dummy/app/models/priceable.rb +0 -8
- data/spec/dummy/app/models/product.rb +0 -59
- data/spec/dummy/app/models/service.rb +0 -5
- data/spec/dummy/app/models/transaction.rb +0 -13
- data/spec/dummy/app/views/layouts/application.html.erb +0 -14
- data/spec/dummy/config/application.rb +0 -66
- data/spec/dummy/config/boot.rb +0 -13
- data/spec/dummy/config/database.yml +0 -25
- data/spec/dummy/config/environment.rb +0 -5
- data/spec/dummy/config/environments/development.rb +0 -30
- data/spec/dummy/config/environments/production.rb +0 -69
- data/spec/dummy/config/environments/test.rb +0 -36
- data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/dummy/config/initializers/inflections.rb +0 -15
- data/spec/dummy/config/initializers/mime_types.rb +0 -5
- data/spec/dummy/config/initializers/money.rb +0 -31
- data/spec/dummy/config/initializers/secret_token.rb +0 -7
- data/spec/dummy/config/initializers/session_store.rb +0 -8
- data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/spec/dummy/config/locales/en-GB.yml +0 -11
- data/spec/dummy/config/locales/en-US.yml +0 -5
- data/spec/dummy/config/locales/en.yml +0 -5
- data/spec/dummy/config/locales/it.yml +0 -11
- data/spec/dummy/config/mongoid.yml +0 -29
- data/spec/dummy/config/routes.rb +0 -3
- data/spec/dummy/config.ru +0 -4
- data/spec/dummy/db/migrate/20120331190108_create_products.rb +0 -10
- data/spec/dummy/db/migrate/20120402080348_add_bonus_cents_to_product.rb +0 -6
- data/spec/dummy/db/migrate/20120524052716_create_services.rb +0 -10
- data/spec/dummy/db/migrate/20120528181002_create_transactions.rb +0 -11
- data/spec/dummy/db/migrate/20120528210103_create_dummy_products.rb +0 -10
- data/spec/dummy/db/migrate/20120607210247_add_column_that_allows_nil.rb +0 -5
- data/spec/dummy/db/migrate/20120712202655_add_sale_price_cents_to_product.rb +0 -7
- data/spec/dummy/db/migrate/20130124023419_add_price_in_a_range_cents_to_products.rb +0 -5
- data/spec/dummy/db/migrate/20140110194016_add_validates_method_amount_cents_to_products.rb +0 -5
- data/spec/dummy/db/migrate/20141005075025_add_aliased_attr_to_products.rb +0 -5
- data/spec/dummy/db/migrate/20150107061030_add_delivery_fee_cents_and_restock_fee_cents_to_product.rb +0 -6
- data/spec/dummy/db/migrate/20150126231442_add_reduced_price_to_products.rb +0 -6
- data/spec/dummy/db/migrate/20150213234410_add_special_price_to_products.rb +0 -5
- data/spec/dummy/db/migrate/20150217222612_add_lambda_price_to_products.rb +0 -5
- data/spec/dummy/db/migrate/20150303222230_add_skip_validation_price_cents_to_products.rb +0 -5
- data/spec/dummy/db/migrate/20151026220420_add_optional_amount_to_transactions.rb +0 -5
- data/spec/dummy/db/schema.rb +0 -59
- data/spec/dummy/db/structure.sql +0 -21
- data/spec/dummy/public/404.html +0 -26
- data/spec/dummy/public/422.html +0 -26
- data/spec/dummy/public/500.html +0 -25
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +0 -6
- data/spec/helpers/action_view_extension_spec.rb +0 -189
- data/spec/helpers/form_helper_spec.rb +0 -19
- data/spec/money_spec.rb +0 -40
- data/spec/mongoid/mongoid_spec.rb +0 -130
- data/spec/mongoid/two_spec.rb +0 -80
- data/spec/spec_helper.rb +0 -23
- data/spec/support/database_cleaner.rb +0 -14
- data/spec/test_helpers_spec.rb +0 -72
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe 'MoneyRails::ActionViewExtension', type: :helper do
|
|
4
|
-
describe '#currency_symbol' do
|
|
5
|
-
subject { helper.currency_symbol }
|
|
6
|
-
it { is_expected.to be_a String }
|
|
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
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
describe '#humanized_money' do
|
|
23
|
-
let(:money_object){ Money.new(12500) }
|
|
24
|
-
let(:options) { {} }
|
|
25
|
-
subject { helper.humanized_money money_object, options }
|
|
26
|
-
it { is_expected.to be_a String }
|
|
27
|
-
it { is_expected.not_to include Money.default_currency.symbol }
|
|
28
|
-
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
|
29
|
-
|
|
30
|
-
context 'with symbol options' do
|
|
31
|
-
let(:options) { { symbol: true } }
|
|
32
|
-
it { is_expected.to include Money.default_currency.symbol }
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
context 'with deprecated symbol' do
|
|
36
|
-
let(:options) { true }
|
|
37
|
-
before(:each) do
|
|
38
|
-
expect(helper).to receive(:warn)
|
|
39
|
-
end
|
|
40
|
-
it { is_expected.to include Money.default_currency.symbol }
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
context 'with a currency with an alternate symbol' do
|
|
44
|
-
let(:money_object) { Money.new(125_00, 'SGD') }
|
|
45
|
-
|
|
46
|
-
context 'with symbol options' do
|
|
47
|
-
let(:options) { { symbol: true } }
|
|
48
|
-
it { is_expected.to include Money::Currency.new(:sgd).symbol }
|
|
49
|
-
|
|
50
|
-
context 'with disambiguate options' do
|
|
51
|
-
let(:options) { { symbol: true, disambiguate: true } }
|
|
52
|
-
it { is_expected.to include Money::Currency.new(:sgd).disambiguate_symbol }
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
describe '#humanized_money_with_symbol' do
|
|
59
|
-
subject { helper.humanized_money_with_symbol Money.new(12500) }
|
|
60
|
-
it { is_expected.to be_a String }
|
|
61
|
-
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
|
62
|
-
it { is_expected.to include Money.default_currency.symbol }
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
describe '#money_without_cents' do
|
|
66
|
-
let(:options) { {} }
|
|
67
|
-
subject { helper.money_without_cents Money.new(12500), options }
|
|
68
|
-
it { is_expected.to be_a String }
|
|
69
|
-
it { is_expected.not_to include Money.default_currency.symbol }
|
|
70
|
-
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
|
71
|
-
|
|
72
|
-
context 'with deprecated symbol' do
|
|
73
|
-
let(:options) { true }
|
|
74
|
-
before(:each) do
|
|
75
|
-
expect(helper).to receive(:warn)
|
|
76
|
-
end
|
|
77
|
-
it { is_expected.to include Money.default_currency.symbol }
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
describe '#money_without_cents_and_with_symbol' do
|
|
82
|
-
subject { helper.money_without_cents_and_with_symbol Money.new(12500) }
|
|
83
|
-
it { is_expected.to be_a String }
|
|
84
|
-
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
|
85
|
-
it { is_expected.to include Money.default_currency.symbol }
|
|
86
|
-
it { is_expected.not_to include "00" }
|
|
87
|
-
end
|
|
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
|
-
|
|
110
|
-
context 'respects MoneyRails::Configuration settings' do
|
|
111
|
-
context 'with no_cents_if_whole: false' do
|
|
112
|
-
|
|
113
|
-
before do
|
|
114
|
-
MoneyRails.configure do |config|
|
|
115
|
-
config.no_cents_if_whole = false
|
|
116
|
-
end
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
describe '#humanized_money' do
|
|
120
|
-
subject { helper.humanized_money Money.new(12500) }
|
|
121
|
-
it { is_expected.to be_a String }
|
|
122
|
-
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
|
123
|
-
it { is_expected.not_to include Money.default_currency.symbol }
|
|
124
|
-
it { is_expected.to include "00" }
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
describe '#humanized_money_with_symbol' do
|
|
128
|
-
subject { helper.humanized_money_with_symbol Money.new(12500) }
|
|
129
|
-
it { is_expected.to be_a String }
|
|
130
|
-
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
|
131
|
-
it { is_expected.to include Money.default_currency.symbol }
|
|
132
|
-
it { is_expected.to include "00" }
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
context 'with no_cents_if_whole: nil' do
|
|
137
|
-
|
|
138
|
-
before do
|
|
139
|
-
MoneyRails.configure do |config|
|
|
140
|
-
config.no_cents_if_whole = nil
|
|
141
|
-
end
|
|
142
|
-
end
|
|
143
|
-
|
|
144
|
-
describe '#humanized_money' do
|
|
145
|
-
subject { helper.humanized_money Money.new(12500) }
|
|
146
|
-
it { is_expected.to be_a String }
|
|
147
|
-
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
|
148
|
-
it { is_expected.not_to include Money.default_currency.symbol }
|
|
149
|
-
it { is_expected.not_to include "00" }
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
describe '#humanized_money_with_symbol' do
|
|
153
|
-
subject { helper.humanized_money_with_symbol Money.new(12500) }
|
|
154
|
-
it { is_expected.to be_a String }
|
|
155
|
-
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
|
156
|
-
it { is_expected.to include Money.default_currency.symbol }
|
|
157
|
-
it { is_expected.not_to include "00" }
|
|
158
|
-
end
|
|
159
|
-
end
|
|
160
|
-
|
|
161
|
-
context 'with no_cents_if_whole: true' do
|
|
162
|
-
|
|
163
|
-
before do
|
|
164
|
-
MoneyRails.configure do |config|
|
|
165
|
-
config.no_cents_if_whole = true
|
|
166
|
-
end
|
|
167
|
-
end
|
|
168
|
-
|
|
169
|
-
describe '#humanized_money' do
|
|
170
|
-
subject { helper.humanized_money Money.new(12500) }
|
|
171
|
-
it { is_expected.to be_a String }
|
|
172
|
-
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
|
173
|
-
it { is_expected.not_to include Money.default_currency.symbol }
|
|
174
|
-
it { is_expected.not_to include "00" }
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
describe '#humanized_money_with_symbol' do
|
|
178
|
-
subject { helper.humanized_money_with_symbol Money.new(12500) }
|
|
179
|
-
it { is_expected.to be_a String }
|
|
180
|
-
it { is_expected.not_to include Money.default_currency.decimal_mark }
|
|
181
|
-
it { is_expected.to include Money.default_currency.symbol }
|
|
182
|
-
it { is_expected.not_to include "00" }
|
|
183
|
-
end
|
|
184
|
-
end
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
end
|
|
188
|
-
|
|
189
|
-
end
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
if defined? ActiveRecord
|
|
4
|
-
describe "Test helper in form blocks", type: :helper do
|
|
5
|
-
|
|
6
|
-
let :product do
|
|
7
|
-
Product.create(price_cents: 3000, discount: 150,
|
|
8
|
-
bonus_cents: 200, optional_price: 100,
|
|
9
|
-
sale_price_amount: 1200)
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
context "textfield" do
|
|
13
|
-
it "uses the current value of money field in textfield" do
|
|
14
|
-
helper.instance_variable_set :@product, product
|
|
15
|
-
expect(helper.text_field(:product, :price)).to match(/value=\"#{product.price.to_s}\"/)
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
end
|
data/spec/money_spec.rb
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
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,130 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
if defined?(Mongoid) && ::Mongoid::VERSION.split('.').first.to_i > 2
|
|
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_blank_strings_hash) {
|
|
12
|
-
Priceable.create(price: {cents: '', currency_iso: ''})
|
|
13
|
-
}
|
|
14
|
-
let(:priceable_from_hash_with_indifferent_access) {
|
|
15
|
-
Priceable.create(price: {cents: 100, currency_iso: "EUR"}.with_indifferent_access)
|
|
16
|
-
}
|
|
17
|
-
let(:priceable_from_string_with_hyphen) { Priceable.create(price: '1-2 EUR' )}
|
|
18
|
-
let(:priceable_from_string_with_unknown_currency) { Priceable.create(price: '1 TLDR') }
|
|
19
|
-
let(:priceable_with_infinite_precision) { Priceable.create(price: Money.new(BigDecimal('100.1'), 'EUR')) }
|
|
20
|
-
let(:priceable_with_hash_field) {
|
|
21
|
-
Priceable.create(price_hash: {
|
|
22
|
-
key1: Money.new(100, "EUR"),
|
|
23
|
-
key2: Money.new(200, "USD")
|
|
24
|
-
})
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
context "mongoize" do
|
|
28
|
-
it "correctly mongoizes nil to nil" do
|
|
29
|
-
expect(priceable_from_nil.price).to be_nil
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
it "correctly mongoizes a Money object to a hash of cents and currency" do
|
|
33
|
-
expect(priceable.price.cents).to eq(100)
|
|
34
|
-
expect(priceable.price.currency).to eq(Money::Currency.find('EUR'))
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
it "correctly mongoizes a Numeric object to a hash of cents and currency" do
|
|
38
|
-
expect(priceable_from_num.price.cents).to eq(100)
|
|
39
|
-
expect(priceable_from_num.price.currency).to eq(Money.default_currency)
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
it "correctly mongoizes a String object to a hash of cents and currency" do
|
|
43
|
-
expect(priceable_from_string.price.cents).to eq(100)
|
|
44
|
-
expect(priceable_from_string.price.currency).to eq(Money::Currency.find('EUR'))
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
context "when MoneyRails.raise_error_on_money_parsing is true" do
|
|
48
|
-
before { MoneyRails.raise_error_on_money_parsing = true }
|
|
49
|
-
after { MoneyRails.raise_error_on_money_parsing = false }
|
|
50
|
-
|
|
51
|
-
it "raises exception if the mongoized value is a String with a hyphen" do
|
|
52
|
-
expect { priceable_from_string_with_hyphen }.to raise_error MoneyRails::Error
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
it "raises exception if the mongoized value is a String with an unknown currency" do
|
|
56
|
-
expect { priceable_from_string_with_unknown_currency }.to raise_error MoneyRails::Error
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
context "when MoneyRails.raise_error_on_money_parsing is false" do
|
|
61
|
-
it "does not correctly mongoize a String with a hyphen in its middle" do
|
|
62
|
-
expect(priceable_from_string_with_hyphen.price).to eq(nil)
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
it "does not correctly mongoize a String with an unknown currency" do
|
|
66
|
-
expect(priceable_from_string_with_unknown_currency.price).to eq(nil)
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
it "correctly mongoizes a hash of cents and currency" do
|
|
71
|
-
expect(priceable_from_hash.price.cents).to eq(100)
|
|
72
|
-
expect(priceable_from_hash.price.currency).to eq(Money::Currency.find('EUR'))
|
|
73
|
-
end
|
|
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
|
-
|
|
79
|
-
it "correctly mongoizes a HashWithIndifferentAccess of cents and currency" do
|
|
80
|
-
expect(priceable_from_hash_with_indifferent_access.price.cents).to eq(100)
|
|
81
|
-
expect(priceable_from_hash_with_indifferent_access.price.currency).to eq(Money::Currency.find('EUR'))
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
context "infinite_precision = true" do
|
|
85
|
-
before do
|
|
86
|
-
Money.infinite_precision = true
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
after do
|
|
90
|
-
Money.infinite_precision = false
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
it "correctly mongoizes a Money object to a hash of cents and currency" do
|
|
94
|
-
expect(priceable_with_infinite_precision.price.cents).to eq(BigDecimal('100.1'))
|
|
95
|
-
expect(priceable_with_infinite_precision.price.currency).to eq(Money::Currency.find('EUR'))
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
it "correctly serializes a Hash field containing Money objects" do
|
|
101
|
-
expect(priceable_with_hash_field.price_hash[:key1][:cents]).to eq(100)
|
|
102
|
-
expect(priceable_with_hash_field.price_hash[:key2][:cents]).to eq(200)
|
|
103
|
-
expect(priceable_with_hash_field.price_hash[:key1][:currency_iso]).to eq('EUR')
|
|
104
|
-
expect(priceable_with_hash_field.price_hash[:key2][:currency_iso]).to eq('USD')
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
context "demongoize" do
|
|
108
|
-
subject { Priceable.first.price }
|
|
109
|
-
|
|
110
|
-
it { is_expected.to be_an_instance_of(Money) }
|
|
111
|
-
it { is_expected.to eq(Money.new(100, 'EUR')) }
|
|
112
|
-
|
|
113
|
-
it "returns nil if a nil value was stored" do
|
|
114
|
-
nil_priceable = Priceable.create(price: nil)
|
|
115
|
-
expect(nil_priceable.price).to be_nil
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
it 'returns nil if an unknown value was stored' do
|
|
119
|
-
zero_priceable = Priceable.create(price: [])
|
|
120
|
-
expect(zero_priceable.price).to be_nil
|
|
121
|
-
end
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
context "evolve" do
|
|
125
|
-
it "correctly transforms a Money object into a Mongo friendly value" do
|
|
126
|
-
expect(Priceable.where(price: Money.new(100, 'EUR')).first).to eq(priceable)
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
end
|
|
130
|
-
end
|
data/spec/mongoid/two_spec.rb
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
if defined?(Mongoid) && ::Mongoid::VERSION =~ /^2(.*)/
|
|
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_with_infinite_precision) { Priceable.create(price: Money.new(BigDecimal('100.1'), 'EUR')) }
|
|
11
|
-
let(:priceable_from_string_with_hyphen) { Priceable.create(price: '1-2 EUR' )}
|
|
12
|
-
|
|
13
|
-
context "serialize" do
|
|
14
|
-
it "mongoizes correctly nil to nil" do
|
|
15
|
-
expect(priceable_from_nil.price).to be_nil
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
it "serializes correctly a Money object to a hash of cents and currency" do
|
|
19
|
-
expect(priceable.price.cents).to eq(100)
|
|
20
|
-
expect(priceable.price.currency).to eq(Money::Currency.find('EUR'))
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
it "mongoizes correctly a Numeric object to a hash of cents and currency" do
|
|
24
|
-
expect(priceable_from_num.price.cents).to eq(100)
|
|
25
|
-
expect(priceable_from_num.price.currency).to eq(Money.default_currency)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
it "mongoizes correctly a String object to a hash of cents and currency" do
|
|
29
|
-
expect(priceable_from_string.price.cents).to eq(100)
|
|
30
|
-
expect(priceable_from_string.price.currency).to eq(Money::Currency.find('EUR'))
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
context "infinite_precision = true" do
|
|
34
|
-
before do
|
|
35
|
-
Money.infinite_precision = true
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
after do
|
|
39
|
-
Money.infinite_precision = false
|
|
40
|
-
end
|
|
41
|
-
|
|
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('100.1'))
|
|
44
|
-
expect(priceable_with_infinite_precision.price.currency).to eq(Money::Currency.find('EUR'))
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
context "when MoneyRails.raise_error_on_money_parsing is true" do
|
|
49
|
-
before { MoneyRails.raise_error_on_money_parsing = true }
|
|
50
|
-
after { MoneyRails.raise_error_on_money_parsing = false }
|
|
51
|
-
|
|
52
|
-
it "raises exception if the mongoized value is a String with a hyphen" do
|
|
53
|
-
expect { priceable_from_string_with_hyphen }.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
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
context "deserialize" do
|
|
65
|
-
subject { priceable.price }
|
|
66
|
-
it { is_expected.to be_an_instance_of(Money) }
|
|
67
|
-
it { is_expected.to eq(Money.new(100, 'EUR')) }
|
|
68
|
-
|
|
69
|
-
it "returns nil if a nil value was stored" do
|
|
70
|
-
nil_priceable = Priceable.create(price: nil)
|
|
71
|
-
expect(nil_priceable.price).to be_nil
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
it 'returns nil if an unknown value was stored' do
|
|
75
|
-
zero_priceable = Priceable.create(price: [])
|
|
76
|
-
expect(zero_priceable.price).to be_nil
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
end
|
data/spec/spec_helper.rb
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
ENV["RAILS_ENV"] = 'test'
|
|
2
|
-
# Require dummy Rails app
|
|
3
|
-
require File.expand_path("../../spec/dummy/config/environment", __FILE__)
|
|
4
|
-
|
|
5
|
-
require 'database_cleaner'
|
|
6
|
-
require 'rspec/rails'
|
|
7
|
-
|
|
8
|
-
# Requires supporting ruby files with custom matchers and macros, etc,
|
|
9
|
-
# in spec/support/ and its subdirectories.
|
|
10
|
-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
|
11
|
-
|
|
12
|
-
# Silence warnings
|
|
13
|
-
if Money.respond_to?(:silence_core_extensions_deprecations=)
|
|
14
|
-
Money.silence_core_extensions_deprecations = true
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
RSpec.configure do |config|
|
|
18
|
-
# If true, the base class of anonymous controllers will be inferred
|
|
19
|
-
# automatically. This will be the default behavior in future versions of
|
|
20
|
-
# rspec-rails.
|
|
21
|
-
config.infer_base_class_for_anonymous_controllers = false
|
|
22
|
-
config.infer_spec_type_from_file_location!
|
|
23
|
-
end
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
DatabaseCleaner[:active_record].strategy = :transaction if defined? ActiveRecord
|
|
2
|
-
DatabaseCleaner[:mongoid].strategy = :truncation if defined? Mongoid
|
|
3
|
-
|
|
4
|
-
RSpec.configure do |config|
|
|
5
|
-
config.before :suite do
|
|
6
|
-
DatabaseCleaner.clean_with :truncation
|
|
7
|
-
end
|
|
8
|
-
config.before :each do
|
|
9
|
-
DatabaseCleaner.start
|
|
10
|
-
end
|
|
11
|
-
config.after :each do
|
|
12
|
-
DatabaseCleaner.clean
|
|
13
|
-
end
|
|
14
|
-
end
|
data/spec/test_helpers_spec.rb
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
|
2
|
-
require 'spec_helper'
|
|
3
|
-
|
|
4
|
-
if defined? ActiveRecord
|
|
5
|
-
describe 'TestHelpers' do
|
|
6
|
-
|
|
7
|
-
require "money-rails/test_helpers"
|
|
8
|
-
include MoneyRails::TestHelpers
|
|
9
|
-
|
|
10
|
-
let(:product) do
|
|
11
|
-
Product.create(price_cents: 3000, discount: 150,
|
|
12
|
-
bonus_cents: 200,
|
|
13
|
-
sale_price_amount: 1200)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
describe "monetize matcher" do
|
|
17
|
-
|
|
18
|
-
shared_context "monetize matcher" do
|
|
19
|
-
|
|
20
|
-
it "matches model attribute without a '_cents' suffix by default" do
|
|
21
|
-
is_expected.to monetize(:price)
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
it "matches model attribute specified by :as chain" do
|
|
25
|
-
is_expected.to monetize(:discount).as(:discount_value)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
it "matches model attribute with nil value specified by :allow_nil chain" do
|
|
29
|
-
is_expected.to monetize(:optional_price).allow_nil
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
it "matches nullable model attribute when tested instance has a non-nil value" do
|
|
33
|
-
is_expected.to monetize(:optional_price).allow_nil
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
it "matches model attribute with currency specified by :with_currency chain" do
|
|
37
|
-
is_expected.to monetize(:bonus).with_currency(:gbp)
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
it "matches model attribute with currency attribute specified by :with_model_currency chain" do
|
|
41
|
-
is_expected.to(
|
|
42
|
-
monetize(:sale_price_amount)
|
|
43
|
-
.as(:sale_price)
|
|
44
|
-
.with_model_currency(:sale_price_currency_code)
|
|
45
|
-
)
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
it "does not match non existed attribute" do
|
|
49
|
-
is_expected.not_to monetize(:price_fake)
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
it "does not match wrong currency iso" do
|
|
53
|
-
is_expected.not_to monetize(:bonus).with_currency(:usd)
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
it "does not match wrong money attribute name" do
|
|
57
|
-
is_expected.not_to monetize(:bonus).as(:bonussss)
|
|
58
|
-
end
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
describe "testing against an instance of the model class" do
|
|
62
|
-
subject { product }
|
|
63
|
-
include_context "monetize matcher"
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
describe "testing against the model class itself" do
|
|
67
|
-
subject { Product }
|
|
68
|
-
include_context "monetize matcher"
|
|
69
|
-
end
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
end
|