mongoid_money_field 4.0.8 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-gemset +1 -1
- data/.ruby-version +1 -1
- data/.travis.yml +6 -5
- data/Gemfile +2 -0
- data/README.rdoc +5 -3
- data/gemfiles/money51-mongoid31.gemfile +6 -0
- data/gemfiles/money51-mongoid4.gemfile +6 -0
- data/gemfiles/money6-mongoid31.gemfile +6 -0
- data/gemfiles/money6-mongoid4.gemfile +6 -0
- data/lib/mongoid_money_field.rb +10 -162
- data/lib/mongoid_money_field/field.rb +1 -1
- data/lib/mongoid_money_field/type.rb +82 -0
- data/lib/mongoid_money_field/version.rb +1 -1
- data/mongoid_money_field.gemspec +8 -10
- data/spec/money_spec.rb +106 -107
- data/spec/ruby_money_spec.rb +15 -15
- data/spec/support/dummy_money.rb +1 -1
- data/spec/support/dummy_money_required.rb +9 -9
- data/spec/support/dummy_money_with_default.rb +9 -9
- data/spec/support/money3.rb +1 -1
- metadata +50 -72
- data/lib/mongoid_money_field/rails_admin.rb +0 -46
- data/spec/model_spec.rb +0 -8
- data/spec/money3_compat_spec.rb +0 -123
- data/spec/money3_spec.rb +0 -342
data/spec/money3_spec.rb
DELETED
@@ -1,342 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Mongoid::MoneyField do
|
6
|
-
|
7
|
-
describe 'when money field is required' do
|
8
|
-
it 'should be valid to save when field is filled in' do
|
9
|
-
dummy = DummyMoneyRequired.new
|
10
|
-
dummy.price = '$10'
|
11
|
-
expect(dummy).to be_valid
|
12
|
-
expect(dummy.save).to eq true
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'should be not valid to save when field is not filled in' do
|
16
|
-
dummy = DummyMoneyRequired.new
|
17
|
-
expect(dummy).not_to be_valid
|
18
|
-
expect(dummy.errors.count).to eq 1
|
19
|
-
expect(dummy.errors.messages[:price][0]).to eq "can't be blank"
|
20
|
-
expect(dummy.save).to eq false
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'should be valid to save when field is filled in but currency is not' do
|
24
|
-
dummy = DummyMoneyRequired.new
|
25
|
-
dummy.price_cents = 123
|
26
|
-
expect(dummy).to be_valid
|
27
|
-
expect(dummy.save).to eq true
|
28
|
-
expect(dummy.price_currency).to eq Money.default_currency.iso_code
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe 'when value is filled from code' do
|
33
|
-
it 'should raise the error when value consists non digits' do
|
34
|
-
dummy = DummyNotANumber.new
|
35
|
-
dummy.price = 'incorrect1'
|
36
|
-
expect(dummy).not_to be_valid
|
37
|
-
expect(dummy.errors.count).to eq 1
|
38
|
-
expect(dummy.errors.messages[:price][0]).to eq "is not a number"
|
39
|
-
expect(dummy.save).to eq false
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'should raise the error when value consists more then one decimal separator' do
|
43
|
-
dummy = DummyNotANumber.new
|
44
|
-
dummy.price = '121,212,22'
|
45
|
-
expect(dummy).not_to be_valid
|
46
|
-
expect(dummy.errors.count).to eq 1
|
47
|
-
expect(dummy.errors.messages[:price][0]).to eq "is not a number"
|
48
|
-
expect(dummy.save).to eq false
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'should raise the error when value is not present' do
|
52
|
-
dummy = DummyNotANumber.new
|
53
|
-
expect(dummy).not_to be_valid
|
54
|
-
expect(dummy.errors.count).to eq 1
|
55
|
-
expect(dummy.errors.messages[:price][0]).to eq "is not a number"
|
56
|
-
expect(dummy.save).to eq false
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'should raise the error when value is not present' do
|
60
|
-
dummy = DummyNotANumber.new( price: '' )
|
61
|
-
expect(dummy).not_to be_valid
|
62
|
-
expect(dummy.errors.count).to eq 1
|
63
|
-
expect(dummy.errors.messages[:price][0]).to eq "is not a number"
|
64
|
-
expect(dummy.save).to eq false
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
describe 'when value is filled from SimpleForm' do
|
69
|
-
it 'should raise the error when value consists non digits' do
|
70
|
-
dummy = DummyNotANumber.new
|
71
|
-
dummy.price_plain = 'incorrect1'
|
72
|
-
expect(dummy).not_to be_valid
|
73
|
-
expect(dummy.errors.count).to eq 1
|
74
|
-
expect(dummy.errors.messages[:price][0]).to eq "is not a number"
|
75
|
-
expect(dummy.save).to eq false
|
76
|
-
end
|
77
|
-
|
78
|
-
it 'should raise the error when value consists more then one decimal separator' do
|
79
|
-
dummy = DummyNotANumber.new
|
80
|
-
dummy.price_plain = '121,212,22'
|
81
|
-
expect(dummy).not_to be_valid
|
82
|
-
expect(dummy.errors.count).to eq 1
|
83
|
-
expect(dummy.errors.messages[:price][0]).to eq "is not a number"
|
84
|
-
expect(dummy.save).to eq false
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
describe 'when value should be a positive number' do
|
89
|
-
it 'should raise the error when value lesser than 1' do
|
90
|
-
dummy = DummyPositiveNumber.new( price: '-10' )
|
91
|
-
expect(dummy).not_to be_valid
|
92
|
-
expect(dummy.errors.count).to eq 1
|
93
|
-
expect(dummy.errors.messages[:price][0]).to eq "must be greater than 1"
|
94
|
-
expect(dummy.save).to eq false
|
95
|
-
expect(dummy.price_cents).to eq -1000
|
96
|
-
end
|
97
|
-
|
98
|
-
it 'should raise the error when value lesser than 1' do
|
99
|
-
dummy = DummyPositiveNumber.new( price: '0' )
|
100
|
-
expect(dummy).not_to be_valid
|
101
|
-
expect(dummy.errors.count).to eq 1
|
102
|
-
expect(dummy.errors.messages[:price][0]).to eq "must be greater than 1"
|
103
|
-
expect(dummy.save).to eq false
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'should be ok when value is greater than 1' do
|
107
|
-
dummy = DummyPositiveNumber.new( price: '10' )
|
108
|
-
expect(dummy).to be_valid
|
109
|
-
expect(dummy.save).to eq true
|
110
|
-
end
|
111
|
-
|
112
|
-
it 'should be ok when value is not present' do
|
113
|
-
dummy = DummyPositiveNumber.new( price: '' )
|
114
|
-
expect(dummy).to be_valid
|
115
|
-
expect(dummy.save).to eq true
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
describe 'when both default currency and fixed currency is specified' do
|
120
|
-
it 'should use fixed currency instead of default' do
|
121
|
-
DummyOverrideDefaultCurrency.create!(price: '1.23')
|
122
|
-
expect(DummyOverrideDefaultCurrency.first.price.currency.iso_code).to eq 'GBP'
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
describe 'when default currency is specified' do
|
127
|
-
it 'should use it instead of Money.default_currency' do
|
128
|
-
DummyWithDefaultCurrency.create!(price: '1.23')
|
129
|
-
expect(DummyWithDefaultCurrency.first.price.currency.iso_code).to eq 'EUR'
|
130
|
-
expect(Money.default_currency.iso_code).to eq 'RUB'
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
describe 'when persisting a document with a Money datatype' do
|
135
|
-
it 'should be persisted normally when set as dollars' do
|
136
|
-
dummy = DummyMoney.new
|
137
|
-
dummy.price = '$10'
|
138
|
-
expect(dummy.save).to eq true
|
139
|
-
end
|
140
|
-
|
141
|
-
it 'should be persisted normally when set as cents' do
|
142
|
-
dummy = DummyMoney.new
|
143
|
-
dummy.price = '$9.99'
|
144
|
-
expect(dummy.save).to eq true
|
145
|
-
end
|
146
|
-
|
147
|
-
it 'should be persisted normally when set as Money' do
|
148
|
-
dummy = DummyMoney.new
|
149
|
-
dummy.price = Monetize.parse(1.23)
|
150
|
-
expect(dummy.save).to eq true
|
151
|
-
end
|
152
|
-
|
153
|
-
it 'should be possible to set value to nil' do
|
154
|
-
dummy = DummyMoney.new
|
155
|
-
dummy.price = Monetize.parse(1.23)
|
156
|
-
expect(dummy.save).to eq true
|
157
|
-
|
158
|
-
dummy = DummyMoney.first
|
159
|
-
expect(dummy.price).to eq Monetize.parse(1.23)
|
160
|
-
dummy.price = nil
|
161
|
-
expect(dummy.save).to eq true
|
162
|
-
dummy = DummyMoney.first
|
163
|
-
expect(dummy.price).to be_nil
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
describe 'when accessing a document from the datastore with a Money datatype' do
|
168
|
-
before(:each) do
|
169
|
-
DummyMoney.create(:description => "Test", :price => '9.99')
|
170
|
-
end
|
171
|
-
|
172
|
-
it 'should have a Money value that matches the money value that was initially persisted' do
|
173
|
-
dummy = DummyMoney.first
|
174
|
-
expect(dummy.price).to eq Monetize.parse('9.99')
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
describe 'when accessing a document from the datastore with a Money datatype set as money' do
|
179
|
-
before(:each) do
|
180
|
-
dm = DummyMoney.create(:description => "Test")
|
181
|
-
dm.price = Monetize.parse('1.23')
|
182
|
-
dm.save!
|
183
|
-
end
|
184
|
-
|
185
|
-
it 'should have a Money value that matches the money value that was initially persisted' do
|
186
|
-
dummy = DummyMoney.first
|
187
|
-
expect(dummy.price.cents).to eq 123
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
describe 'when accessing a document from the datastore with a Money datatype set as money with mass asignment' do
|
192
|
-
before(:each) do
|
193
|
-
DummyMoney.create(:description => "Test", :price => Monetize.parse('1.23'))
|
194
|
-
end
|
195
|
-
|
196
|
-
it 'should have a Money value that matches the money value that was initially persisted' do
|
197
|
-
dummy = DummyMoney.first
|
198
|
-
expect(dummy.price.cents).to eq 123
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
describe 'when accessing a document from the datastore with a Money datatype and empty value' do
|
203
|
-
it 'should be nil' do
|
204
|
-
dummy = DummyMoneyWithoutDefault.new
|
205
|
-
expect(dummy.save).to eq true
|
206
|
-
expect(DummyMoneyWithoutDefault.first.price).to be_nil
|
207
|
-
end
|
208
|
-
|
209
|
-
it 'should be 0 when used with default' do
|
210
|
-
dummy = DummyMoney.new
|
211
|
-
expect(dummy.save).to eq true
|
212
|
-
expect(DummyMoney.first.price.cents).to eq 0
|
213
|
-
end
|
214
|
-
|
215
|
-
it 'should set money to default currency if money is given without it' do
|
216
|
-
dummy = DummyMoneyWithDefault.new
|
217
|
-
expect(dummy.save).to eq true
|
218
|
-
dummy = DummyMoneyWithDefault.first
|
219
|
-
expect(dummy.price.currency.iso_code).to eq Money.default_currency.iso_code
|
220
|
-
expect(dummy.price.cents).to eq 100
|
221
|
-
end
|
222
|
-
|
223
|
-
it 'should set money to default currency if money is given without it on a document with multiple money fields' do
|
224
|
-
dummy = DummyPrices.new
|
225
|
-
expect(dummy.save).to eq true
|
226
|
-
dummy = DummyPrices.first
|
227
|
-
expect(dummy.price.currency.iso_code).to eq Money.default_currency.iso_code
|
228
|
-
expect(dummy.price.cents).to eq 100
|
229
|
-
|
230
|
-
expect(dummy.price2).to be_nil
|
231
|
-
|
232
|
-
expect(dummy.price1.cents).to eq 0
|
233
|
-
end
|
234
|
-
|
235
|
-
|
236
|
-
it 'should set money to correct currency if money is given with it' do
|
237
|
-
dummy = DummyMoneyWithDefaultWithCurrency.new
|
238
|
-
expect(dummy.save).to eq true
|
239
|
-
dummy = DummyMoneyWithDefaultWithCurrency.first
|
240
|
-
expect(dummy.price.currency.iso_code).to eq 'GBP'
|
241
|
-
expect(dummy.price.cents).to eq 100
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
describe 'when accessing a document from the datastore with a Money datatype and fixed currency' do
|
246
|
-
it 'should have correct currency when value is set to 5$' do
|
247
|
-
DummyMoneyWithFixedCurrency.create!(price: '5$')
|
248
|
-
dummy = DummyMoneyWithFixedCurrency.first
|
249
|
-
expect(dummy.price.currency.iso_code).to eq 'GBP'
|
250
|
-
expect(dummy.price.cents).to eq 500
|
251
|
-
expect(dummy.price).to eq Monetize.parse('5 GBP')
|
252
|
-
end
|
253
|
-
|
254
|
-
it 'should have correct currency when value is set to 100 RUB' do
|
255
|
-
DummyMoneyWithFixedCurrency.create!(price: '100 RUB')
|
256
|
-
dummy = DummyMoneyWithFixedCurrency.first
|
257
|
-
expect(dummy.price.currency.iso_code).to eq 'GBP'
|
258
|
-
expect(dummy.price.cents).to eq 100_00
|
259
|
-
expect(dummy.price).to eq Monetize.parse('100 GBP')
|
260
|
-
end
|
261
|
-
end
|
262
|
-
|
263
|
-
describe 'when setting to a string value with currency' do
|
264
|
-
it 'should handle RUB' do
|
265
|
-
DummyMoney.create(description: "Test", price: '1.23 RUB')
|
266
|
-
dummy = DummyMoney.first
|
267
|
-
expect(dummy.price.currency.iso_code).to eq 'RUB'
|
268
|
-
expect(dummy.price.cents).to eq 123
|
269
|
-
expect(dummy.price).to eq Monetize.parse('1.23 RUB')
|
270
|
-
end
|
271
|
-
|
272
|
-
it 'should handle $' do
|
273
|
-
DummyMoney.create(description: "Test", price: '1.23 USD')
|
274
|
-
dummy = DummyMoney.first
|
275
|
-
expect(dummy.price.currency.iso_code).to eq 'USD'
|
276
|
-
expect(dummy.price.cents).to eq 123
|
277
|
-
expect(dummy.price).to eq Monetize.parse('1.23 USD')
|
278
|
-
end
|
279
|
-
end
|
280
|
-
|
281
|
-
describe 'when accessing a document from the datastore with a Money datatype and blank value' do
|
282
|
-
before(:each) do
|
283
|
-
DummyMoney.create(description: "Test", price: '')
|
284
|
-
end
|
285
|
-
|
286
|
-
it 'should be nil' do
|
287
|
-
dummy = DummyMoney.first
|
288
|
-
expect(dummy.price).to be_nil
|
289
|
-
end
|
290
|
-
|
291
|
-
it 'should be updated correctly' do
|
292
|
-
dummy = DummyMoney.first
|
293
|
-
expect(dummy.price).to be_nil
|
294
|
-
dummy.price = '1.23 USD'
|
295
|
-
expect(dummy.save).to eq true
|
296
|
-
dummy = DummyMoney.first
|
297
|
-
expect(dummy.price.currency.iso_code).to eq 'USD'
|
298
|
-
expect(dummy.price.cents).to eq 123
|
299
|
-
end
|
300
|
-
end
|
301
|
-
|
302
|
-
describe 'when accessing a document from the datastore with embedded documents with money fields' do
|
303
|
-
before(:each) do
|
304
|
-
o = DummyOrder.new(first_name: 'test')
|
305
|
-
|
306
|
-
o.dummy_line_items << DummyLineItem.new({name: 'item 1', price: Money.new(1299)})
|
307
|
-
li = DummyLineItem.new({name: 'item 2', price: Money.new(1499)})
|
308
|
-
o.dummy_line_items.push li
|
309
|
-
|
310
|
-
o.save
|
311
|
-
end
|
312
|
-
|
313
|
-
it 'should have correct value for first item' do
|
314
|
-
o = DummyOrder.first
|
315
|
-
expect(o.dummy_line_items.first.price).to eq Monetize.parse('12.99')
|
316
|
-
end
|
317
|
-
|
318
|
-
it 'should have correct value for first item' do
|
319
|
-
o = DummyOrder.first
|
320
|
-
expect(o.dummy_line_items.last.price).to eq Monetize.parse('14.99')
|
321
|
-
end
|
322
|
-
end
|
323
|
-
|
324
|
-
describe 'when accessing a document from the datastore with multiple Money datatypes' do
|
325
|
-
before(:each) do
|
326
|
-
DummyPrices.create(description: "Test", price3: '1', price1: '1.23', price2: '2.33')
|
327
|
-
end
|
328
|
-
|
329
|
-
it 'should have correct Money value for field 1' do
|
330
|
-
dummy = DummyPrices.first
|
331
|
-
expect(dummy.price1).to eq Monetize.parse('1.23')
|
332
|
-
end
|
333
|
-
it 'should have correct Money value for field 2' do
|
334
|
-
dummy = DummyPrices.first
|
335
|
-
expect(dummy.price2).to eq Monetize.parse('2.33')
|
336
|
-
end
|
337
|
-
it 'should have correct Money value for field 3' do
|
338
|
-
dummy = DummyPrices.first
|
339
|
-
expect(dummy.price3).to eq Monetize.parse('1')
|
340
|
-
end
|
341
|
-
end
|
342
|
-
end
|