money-rails 1.0.0 → 1.1.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/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
@@ -16,35 +16,35 @@ if defined? ActiveRecord
|
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should be inherited by subclasses" do
|
19
|
-
Sub.monetized_attributes.
|
19
|
+
expect(Sub.monetized_attributes).to eq(Product.monetized_attributes)
|
20
20
|
end
|
21
21
|
|
22
22
|
it "attaches a Money object to model field" do
|
23
|
-
product.price.
|
24
|
-
product.discount_value.
|
25
|
-
product.bonus.
|
23
|
+
expect(product.price).to be_an_instance_of(Money)
|
24
|
+
expect(product.discount_value).to be_an_instance_of(Money)
|
25
|
+
expect(product.bonus).to be_an_instance_of(Money)
|
26
26
|
end
|
27
27
|
|
28
28
|
it "returns the expected money amount as a Money object" do
|
29
|
-
product.price.
|
29
|
+
expect(product.price).to eq(Money.new(3000, "USD"))
|
30
30
|
end
|
31
31
|
|
32
32
|
it "assigns the correct value from a Money object" do
|
33
33
|
product.price = Money.new(3210, "USD")
|
34
|
-
product.save.
|
35
|
-
product.price_cents.
|
34
|
+
expect(product.save).to be_truthy
|
35
|
+
expect(product.price_cents).to eq(3210)
|
36
36
|
end
|
37
37
|
|
38
38
|
it "assigns the correct value from a Money object using create" do
|
39
39
|
product = Product.create(:price => Money.new(3210, "USD"), :discount => 150,
|
40
40
|
:bonus_cents => 200, :optional_price => 100)
|
41
|
-
product.valid
|
42
|
-
product.price_cents.
|
41
|
+
expect(product.valid?).to be_truthy
|
42
|
+
expect(product.price_cents).to eq(3210)
|
43
43
|
end
|
44
44
|
|
45
45
|
it "correctly updates from a Money object using update_attributes" do
|
46
|
-
product.update_attributes(:price => Money.new(215, "USD")).
|
47
|
-
product.price_cents.
|
46
|
+
expect(product.update_attributes(:price => Money.new(215, "USD"))).to be_truthy
|
47
|
+
expect(product.price_cents).to eq(215)
|
48
48
|
end
|
49
49
|
|
50
50
|
it "should raise error if can't change currency" do
|
@@ -54,28 +54,36 @@ if defined? ActiveRecord
|
|
54
54
|
}.to raise_error("Can't change readonly currency 'USD' to 'RUB' for field 'price'")
|
55
55
|
end
|
56
56
|
|
57
|
+
it "raises an error if trying to create two attributes with the same name" do
|
58
|
+
expect do
|
59
|
+
class Product
|
60
|
+
monetize :discount, as: :price
|
61
|
+
end
|
62
|
+
end.to raise_error
|
63
|
+
end
|
64
|
+
|
57
65
|
it "respects :as argument" do
|
58
|
-
product.discount_value.
|
66
|
+
expect(product.discount_value).to eq(Money.new(150, "USD"))
|
59
67
|
end
|
60
68
|
|
61
69
|
it "uses numericality validation" do
|
62
70
|
product.price_cents = "foo"
|
63
|
-
product.save.
|
71
|
+
expect(product.save).to be_falsey
|
64
72
|
|
65
73
|
product.price_cents = 2000
|
66
|
-
product.save.
|
74
|
+
expect(product.save).to be_truthy
|
67
75
|
end
|
68
76
|
|
69
77
|
it "skips numericality validation when disabled" do
|
70
78
|
product.invalid_price_cents = 'not_valid'
|
71
|
-
product.save.
|
79
|
+
expect(product.save).to be_truthy
|
72
80
|
end
|
73
81
|
|
74
82
|
it "passes validation after updating fractional attribute which was previously invalid" do
|
75
83
|
product.price_in_a_range = -5
|
76
|
-
product.
|
84
|
+
expect(product).not_to be_valid
|
77
85
|
product.price_in_a_range_cents = 500
|
78
|
-
product.
|
86
|
+
expect(product).to be_valid
|
79
87
|
end
|
80
88
|
|
81
89
|
context "when MoneyRails.raise_error_on_money_parsing is true" do
|
@@ -94,175 +102,185 @@ if defined? ActiveRecord
|
|
94
102
|
end
|
95
103
|
|
96
104
|
it "respects numericality validation when using update_attributes" do
|
97
|
-
product.update_attributes(:price_cents => "some text").
|
98
|
-
product.update_attributes(:price_cents => 2000).
|
105
|
+
expect(product.update_attributes(:price_cents => "some text")).to be_falsey
|
106
|
+
expect(product.update_attributes(:price_cents => 2000)).to be_truthy
|
99
107
|
end
|
100
108
|
|
101
109
|
it "uses numericality validation on money attribute" do
|
102
110
|
product.price = "some text"
|
103
|
-
product.save.
|
111
|
+
expect(product.save).to be_falsey
|
104
112
|
|
105
113
|
product.price = Money.new(320, "USD")
|
106
|
-
product.save.
|
114
|
+
expect(product.save).to be_truthy
|
107
115
|
|
108
116
|
product.sale_price = "12.34"
|
109
117
|
product.sale_price_currency_code = 'EUR'
|
110
|
-
product.valid
|
118
|
+
expect(product.valid?).to be_truthy
|
111
119
|
end
|
112
120
|
|
113
121
|
it "fails validation with the proper error message if money value is invalid decimal" do
|
114
122
|
product.price = "12.23.24"
|
115
|
-
product.save.
|
116
|
-
product.errors[:price].first.
|
117
|
-
product.errors[:price].first.
|
123
|
+
expect(product.save).to be_falsey
|
124
|
+
expect(product.errors[:price].first).to match(/Must be a valid/)
|
125
|
+
expect(product.errors[:price].first).to match(/Got 12.23.24/)
|
118
126
|
end
|
119
127
|
|
120
128
|
it "fails validation with the proper error message if money value is nothing but periods" do
|
121
129
|
product.price = "..."
|
122
|
-
product.save.
|
123
|
-
product.errors[:price].first.
|
124
|
-
product.errors[:price].first.
|
130
|
+
expect(product.save).to be_falsey
|
131
|
+
expect(product.errors[:price].first).to match(/Must be a valid/)
|
132
|
+
expect(product.errors[:price].first).to match(/Got .../)
|
125
133
|
end
|
126
134
|
|
127
135
|
it "fails validation with the proper error message if money value has invalid thousands part" do
|
128
136
|
product.price = "12,23.24"
|
129
|
-
product.save.
|
130
|
-
product.errors[:price].first.
|
131
|
-
product.errors[:price].first.
|
137
|
+
expect(product.save).to be_falsey
|
138
|
+
expect(product.errors[:price].first).to match(/Must be a valid/)
|
139
|
+
expect(product.errors[:price].first).to match(/Got 12,23.24/)
|
140
|
+
end
|
141
|
+
|
142
|
+
it "allows an empty string as the thousands separator" do
|
143
|
+
begin
|
144
|
+
I18n.locale = 'en-US'
|
145
|
+
product.price = '10.00'
|
146
|
+
expect(product).to be_valid
|
147
|
+
ensure
|
148
|
+
I18n.locale = I18n.default_locale
|
149
|
+
end
|
132
150
|
end
|
133
151
|
|
134
152
|
it "passes validation if money value is a Float and the currency decimal mark is not period" do
|
135
153
|
# The corresponding String would be "12,34" euros
|
136
154
|
service.discount = 12.34
|
137
|
-
service.save.
|
155
|
+
expect(service.save).to be_truthy
|
138
156
|
end
|
139
157
|
|
140
158
|
it "passes validation if money value is a Float" do
|
141
159
|
product.price = 12.34
|
142
|
-
product.save.
|
160
|
+
expect(product.save).to be_truthy
|
143
161
|
end
|
144
162
|
|
145
163
|
it "passes validation if money value is an Integer" do
|
146
164
|
product.price = 12
|
147
|
-
product.save.
|
165
|
+
expect(product.save).to be_truthy
|
148
166
|
end
|
149
167
|
|
150
168
|
it "fails validation with the proper error message using numericality validations" do
|
151
169
|
product.price_in_a_range = "-12"
|
152
|
-
product.valid
|
153
|
-
product.errors[:price_in_a_range].first.
|
170
|
+
expect(product.valid?).to be_falsey
|
171
|
+
expect(product.errors[:price_in_a_range].first).to match(/Must be greater than zero and less than \$100/)
|
154
172
|
|
155
173
|
product.price_in_a_range = Money.new(-1200, "USD")
|
156
|
-
product.valid
|
157
|
-
product.errors[:price_in_a_range].first.
|
174
|
+
expect(product.valid?).to be_falsey
|
175
|
+
expect(product.errors[:price_in_a_range].first).to match(/Must be greater than zero and less than \$100/)
|
158
176
|
|
159
177
|
product.price_in_a_range = "0"
|
160
|
-
product.valid
|
161
|
-
product.errors[:price_in_a_range].first.
|
178
|
+
expect(product.valid?).to be_falsey
|
179
|
+
expect(product.errors[:price_in_a_range].first).to match(/Must be greater than zero and less than \$100/)
|
162
180
|
|
163
181
|
product.price_in_a_range = "12"
|
164
|
-
product.valid
|
182
|
+
expect(product.valid?).to be_truthy
|
165
183
|
|
166
184
|
product.price_in_a_range = Money.new(1200, "USD")
|
167
|
-
product.valid
|
185
|
+
expect(product.valid?).to be_truthy
|
168
186
|
|
169
187
|
product.price_in_a_range = "101"
|
170
|
-
product.valid
|
171
|
-
product.errors[:price_in_a_range].first.
|
188
|
+
expect(product.valid?).to be_falsey
|
189
|
+
expect(product.errors[:price_in_a_range].first).to match(/Must be greater than zero and less than \$100/)
|
172
190
|
|
173
191
|
product.price_in_a_range = Money.new(10100, "USD")
|
174
|
-
product.valid
|
175
|
-
product.errors[:price_in_a_range].first.
|
192
|
+
expect(product.valid?).to be_falsey
|
193
|
+
expect(product.errors[:price_in_a_range].first).to match(/Must be greater than zero and less than \$100/)
|
176
194
|
end
|
177
195
|
|
178
196
|
it "fails validation with the proper error message using validates :money" do
|
179
197
|
product.validates_method_amount = "-12"
|
180
|
-
product.valid
|
181
|
-
product.errors[:validates_method_amount].first.
|
198
|
+
expect(product.valid?).to be_falsey
|
199
|
+
expect(product.errors[:validates_method_amount].first).to match(/Must be greater than zero and less than \$100/)
|
182
200
|
|
183
201
|
product.validates_method_amount = Money.new(-1200, "USD")
|
184
|
-
product.valid
|
185
|
-
product.errors[:validates_method_amount].first.
|
202
|
+
expect(product.valid?).to be_falsey
|
203
|
+
expect(product.errors[:validates_method_amount].first).to match(/Must be greater than zero and less than \$100/)
|
186
204
|
|
187
205
|
product.validates_method_amount = "0"
|
188
|
-
product.valid
|
189
|
-
product.errors[:validates_method_amount].first.
|
206
|
+
expect(product.valid?).to be_falsey
|
207
|
+
expect(product.errors[:validates_method_amount].first).to match(/Must be greater than zero and less than \$100/)
|
190
208
|
|
191
209
|
product.validates_method_amount = "12"
|
192
|
-
product.valid
|
210
|
+
expect(product.valid?).to be_truthy
|
193
211
|
|
194
212
|
product.validates_method_amount = Money.new(1200, "USD")
|
195
|
-
product.valid
|
213
|
+
expect(product.valid?).to be_truthy
|
196
214
|
|
197
215
|
product.validates_method_amount = "101"
|
198
|
-
product.valid
|
199
|
-
product.errors[:validates_method_amount].first.
|
216
|
+
expect(product.valid?).to be_falsey
|
217
|
+
expect(product.errors[:validates_method_amount].first).to match(/Must be greater than zero and less than \$100/)
|
200
218
|
|
201
219
|
product.validates_method_amount = Money.new(10100, "USD")
|
202
|
-
product.valid
|
203
|
-
product.errors[:validates_method_amount].first.
|
220
|
+
expect(product.valid?).to be_falsey
|
221
|
+
expect(product.errors[:validates_method_amount].first).to match(/Must be greater than zero and less than \$100/)
|
204
222
|
end
|
205
223
|
|
206
224
|
it "fails validation with the proper error message on the cents field " do
|
207
225
|
product.price_in_a_range = "-12"
|
208
|
-
product.valid
|
209
|
-
product.errors[:price_in_a_range_cents].first.
|
226
|
+
expect(product.valid?).to be_falsey
|
227
|
+
expect(product.errors[:price_in_a_range_cents].first).to match(/greater than 0/)
|
210
228
|
|
211
229
|
product.price_in_a_range = "0"
|
212
|
-
product.valid
|
213
|
-
product.errors[:price_in_a_range_cents].first.
|
230
|
+
expect(product.valid?).to be_falsey
|
231
|
+
expect(product.errors[:price_in_a_range_cents].first).to match(/greater than 0/)
|
214
232
|
|
215
233
|
product.price_in_a_range = "12"
|
216
|
-
product.valid
|
234
|
+
expect(product.valid?).to be_truthy
|
217
235
|
|
218
236
|
product.price_in_a_range = "101"
|
219
|
-
product.valid
|
220
|
-
product.errors[:price_in_a_range_cents].first.
|
237
|
+
expect(product.valid?).to be_falsey
|
238
|
+
expect(product.errors[:price_in_a_range_cents].first).to match(/less than or equal to 10000/)
|
221
239
|
end
|
222
240
|
|
223
241
|
it "fails validation when a non number string is given" do
|
224
242
|
product = Product.create(:price_in_a_range => "asd")
|
225
|
-
product.valid
|
226
|
-
product.errors[:price_in_a_range].first.
|
243
|
+
expect(product.valid?).to be_falsey
|
244
|
+
expect(product.errors[:price_in_a_range].first).to match(/greater than zero/)
|
227
245
|
|
228
246
|
product = Product.create(:price_in_a_range => "asd23")
|
229
|
-
product.valid
|
230
|
-
product.errors[:price_in_a_range].first.
|
247
|
+
expect(product.valid?).to be_falsey
|
248
|
+
expect(product.errors[:price_in_a_range].first).to match(/greater than zero/)
|
231
249
|
|
232
250
|
product = Product.create(:price => "asd")
|
233
|
-
product.valid
|
234
|
-
product.errors[:price].first.
|
251
|
+
expect(product.valid?).to be_falsey
|
252
|
+
expect(product.errors[:price].first).to match(/is not a number/)
|
235
253
|
|
236
254
|
product = Product.create(:price => "asd23")
|
237
|
-
product.valid
|
238
|
-
product.errors[:price].first.
|
255
|
+
expect(product.valid?).to be_falsey
|
256
|
+
expect(product.errors[:price].first).to match(/is not a number/)
|
239
257
|
end
|
240
258
|
|
241
259
|
it "passes validation when amount contains spaces (99 999 999.99)" do
|
242
260
|
product.price = "99 999 999.99"
|
243
|
-
product.
|
244
|
-
product.price_cents.
|
261
|
+
expect(product).to be_valid
|
262
|
+
expect(product.price_cents).to eq(9999999999)
|
245
263
|
end
|
246
264
|
|
247
265
|
it "passes validation when amount contains underscores (99_999_999.99)" do
|
248
266
|
product.price = "99_999_999.99"
|
249
|
-
product.
|
250
|
-
product.price_cents.
|
267
|
+
expect(product).to be_valid
|
268
|
+
expect(product.price_cents).to eq(9999999999)
|
251
269
|
end
|
252
270
|
|
253
271
|
it "passes validation if money value has correct format" do
|
254
272
|
product.price = "12,230.24"
|
255
|
-
product.save.
|
273
|
+
expect(product.save).to be_truthy
|
256
274
|
end
|
257
275
|
|
258
276
|
it "passes validation if there is a whitespace between the currency symbol and amount" do
|
259
277
|
product.price = "$ 123,456.78"
|
260
|
-
product.save.
|
278
|
+
expect(product.save).to be_truthy
|
261
279
|
end
|
262
280
|
|
263
281
|
it "respects numericality validation when using update_attributes on money attribute" do
|
264
|
-
product.update_attributes(:price => "some text").
|
265
|
-
product.update_attributes(:price => Money.new(320, 'USD')).
|
282
|
+
expect(product.update_attributes(:price => "some text")).to be_falsey
|
283
|
+
expect(product.update_attributes(:price => Money.new(320, 'USD'))).to be_truthy
|
266
284
|
end
|
267
285
|
|
268
286
|
it "uses i18n currency format when validating" do
|
@@ -270,10 +288,10 @@ if defined? ActiveRecord
|
|
270
288
|
|
271
289
|
I18n.locale = "en-GB"
|
272
290
|
Money.default_currency = Money::Currency.find('EUR')
|
273
|
-
"12.00".to_money.
|
291
|
+
expect("12.00".to_money).to eq(Money.new(1200, :eur))
|
274
292
|
transaction = Transaction.new(amount: "12.00", tax: "13.00")
|
275
|
-
transaction.amount_cents.
|
276
|
-
transaction.valid
|
293
|
+
expect(transaction.amount_cents).to eq(1200)
|
294
|
+
expect(transaction.valid?).to be_truthy
|
277
295
|
|
278
296
|
# reset locale setting
|
279
297
|
I18n.locale = old_locale
|
@@ -284,10 +302,10 @@ if defined? ActiveRecord
|
|
284
302
|
|
285
303
|
I18n.locale = "zxsw"
|
286
304
|
Money.default_currency = Money::Currency.find('EUR')
|
287
|
-
"12,00".to_money.
|
305
|
+
expect("12,00".to_money).to eq(Money.new(1200, :eur))
|
288
306
|
transaction = Transaction.new(amount: "12,00", tax: "13,00")
|
289
|
-
transaction.amount_cents.
|
290
|
-
transaction.valid
|
307
|
+
expect(transaction.amount_cents).to eq(1200)
|
308
|
+
expect(transaction.valid?).to be_truthy
|
291
309
|
|
292
310
|
# reset locale setting
|
293
311
|
I18n.locale = old_locale
|
@@ -295,13 +313,13 @@ if defined? ActiveRecord
|
|
295
313
|
|
296
314
|
it "doesn't allow nil by default" do
|
297
315
|
product.price_cents = nil
|
298
|
-
product.save.
|
316
|
+
expect(product.save).to be_falsey
|
299
317
|
end
|
300
318
|
|
301
319
|
it "allows nil if optioned" do
|
302
320
|
product.optional_price = nil
|
303
|
-
product.save.
|
304
|
-
product.optional_price.
|
321
|
+
expect(product.save).to be_truthy
|
322
|
+
expect(product.optional_price).to be_nil
|
305
323
|
end
|
306
324
|
|
307
325
|
it "doesn't raise exception if validation is used and nil is not allowed" do
|
@@ -311,142 +329,151 @@ if defined? ActiveRecord
|
|
311
329
|
it "doesn't save nil values if validation is used and nil is not allowed" do
|
312
330
|
product.price = nil
|
313
331
|
product.save
|
314
|
-
product.price_cents.
|
332
|
+
expect(product.price_cents).not_to be_nil
|
315
333
|
end
|
316
334
|
|
317
335
|
it "resets money_before_type_cast attr every time a save operation occurs" do
|
318
336
|
v = Money.new(100, :usd)
|
319
337
|
product.price = v
|
320
|
-
product.price_money_before_type_cast.
|
338
|
+
expect(product.price_money_before_type_cast).to eq(v)
|
321
339
|
product.save
|
322
|
-
product.price_money_before_type_cast.
|
340
|
+
expect(product.price_money_before_type_cast).to be_nil
|
323
341
|
product.price = 10
|
324
|
-
product.price_money_before_type_cast.
|
342
|
+
expect(product.price_money_before_type_cast).to eq(10)
|
325
343
|
product.save
|
326
|
-
product.price_money_before_type_cast.
|
344
|
+
expect(product.price_money_before_type_cast).to be_nil
|
327
345
|
end
|
328
346
|
|
329
347
|
it "does not reset money_before_type_cast attr if save operation fails" do
|
330
348
|
product.bonus = ""
|
331
|
-
product.bonus_money_before_type_cast.
|
332
|
-
product.save.
|
333
|
-
product.bonus_money_before_type_cast.
|
349
|
+
expect(product.bonus_money_before_type_cast).to eq("")
|
350
|
+
expect(product.save).to be_falsey
|
351
|
+
expect(product.bonus_money_before_type_cast).to eq("")
|
334
352
|
end
|
335
353
|
|
336
354
|
it "uses Money default currency if :with_currency has not been used" do
|
337
|
-
service.discount.currency.
|
355
|
+
expect(service.discount.currency).to eq(Money::Currency.find(:eur))
|
338
356
|
end
|
339
357
|
|
340
358
|
it "overrides default currency with the currency registered for the model" do
|
341
|
-
product.price.currency.
|
359
|
+
expect(product.price.currency).to eq(Money::Currency.find(:usd))
|
342
360
|
end
|
343
361
|
|
344
362
|
it "overrides default currency with the value of :with_currency argument" do
|
345
|
-
service.charge.currency.
|
346
|
-
product.bonus.currency.
|
363
|
+
expect(service.charge.currency).to eq(Money::Currency.find(:usd))
|
364
|
+
expect(product.bonus.currency).to eq(Money::Currency.find(:gbp))
|
347
365
|
end
|
348
366
|
|
349
367
|
it "correctly assigns Money objects to the attribute" do
|
350
368
|
product.price = Money.new(2500, :USD)
|
351
|
-
product.save.
|
352
|
-
product.price.cents.
|
353
|
-
product.price.currency_as_string.
|
369
|
+
expect(product.save).to be_truthy
|
370
|
+
expect(product.price.cents).to eq(2500)
|
371
|
+
expect(product.price.currency_as_string).to eq("USD")
|
354
372
|
end
|
355
373
|
|
356
374
|
it "correctly assigns Fixnum objects to the attribute" do
|
357
375
|
product.price = 25
|
358
|
-
product.save.
|
359
|
-
product.price.cents.
|
360
|
-
product.price.currency_as_string.
|
376
|
+
expect(product.save).to be_truthy
|
377
|
+
expect(product.price.cents).to eq(2500)
|
378
|
+
expect(product.price.currency_as_string).to eq("USD")
|
361
379
|
|
362
380
|
service.discount = 2
|
363
|
-
service.save.
|
364
|
-
service.discount.cents.
|
365
|
-
service.discount.currency_as_string.
|
381
|
+
expect(service.save).to be_truthy
|
382
|
+
expect(service.discount.cents).to eq(200)
|
383
|
+
expect(service.discount.currency_as_string).to eq("EUR")
|
366
384
|
end
|
367
385
|
|
368
386
|
it "correctly assigns String objects to the attribute" do
|
369
387
|
product.price = "25"
|
370
|
-
product.save.
|
371
|
-
product.price.cents.
|
372
|
-
product.price.currency_as_string.
|
388
|
+
expect(product.save).to be_truthy
|
389
|
+
expect(product.price.cents).to eq(2500)
|
390
|
+
expect(product.price.currency_as_string).to eq("USD")
|
373
391
|
|
374
392
|
service.discount = "2"
|
375
|
-
service.save.
|
376
|
-
service.discount.cents.
|
377
|
-
service.discount.currency_as_string.
|
393
|
+
expect(service.save).to be_truthy
|
394
|
+
expect(service.discount.cents).to eq(200)
|
395
|
+
expect(service.discount.currency_as_string).to eq("EUR")
|
378
396
|
end
|
379
397
|
|
380
398
|
it "overrides default, model currency with the value of :with_currency in fixnum assignments" do
|
381
399
|
product.bonus = 25
|
382
|
-
product.save.
|
383
|
-
product.bonus.cents.
|
384
|
-
product.bonus.currency_as_string.
|
400
|
+
expect(product.save).to be_truthy
|
401
|
+
expect(product.bonus.cents).to eq(2500)
|
402
|
+
expect(product.bonus.currency_as_string).to eq("GBP")
|
385
403
|
|
386
404
|
service.charge = 2
|
387
|
-
service.save.
|
388
|
-
service.charge.cents.
|
389
|
-
service.charge.currency_as_string.
|
405
|
+
expect(service.save).to be_truthy
|
406
|
+
expect(service.charge.cents).to eq(200)
|
407
|
+
expect(service.charge.currency_as_string).to eq("USD")
|
390
408
|
end
|
391
409
|
|
392
410
|
it "overrides default, model currency with the value of :with_currency in string assignments" do
|
393
411
|
product.bonus = "25"
|
394
|
-
product.save.
|
395
|
-
product.bonus.cents.
|
396
|
-
product.bonus.currency_as_string.
|
412
|
+
expect(product.save).to be_truthy
|
413
|
+
expect(product.bonus.cents).to eq(2500)
|
414
|
+
expect(product.bonus.currency_as_string).to eq("GBP")
|
397
415
|
|
398
416
|
service.charge = "2"
|
399
|
-
service.save.
|
400
|
-
service.charge.cents.
|
401
|
-
service.charge.currency_as_string.
|
417
|
+
expect(service.save).to be_truthy
|
418
|
+
expect(service.charge.cents).to eq(200)
|
419
|
+
expect(service.charge.currency_as_string).to eq("USD")
|
402
420
|
end
|
403
421
|
|
404
422
|
it "overrides default currency with model currency, in fixnum assignments" do
|
405
423
|
product.discount_value = 5
|
406
|
-
product.save.
|
407
|
-
product.discount_value.cents.
|
408
|
-
product.discount_value.currency_as_string.
|
424
|
+
expect(product.save).to be_truthy
|
425
|
+
expect(product.discount_value.cents).to eq(500)
|
426
|
+
expect(product.discount_value.currency_as_string).to eq("USD")
|
409
427
|
end
|
410
428
|
|
411
429
|
it "overrides default currency with model currency, in string assignments" do
|
412
430
|
product.discount_value = "5"
|
413
|
-
product.save.
|
414
|
-
product.discount_value.cents.
|
415
|
-
product.discount_value.currency_as_string.
|
431
|
+
expect(product.save).to be_truthy
|
432
|
+
expect(product.discount_value.cents).to eq(500)
|
433
|
+
expect(product.discount_value.currency_as_string).to eq("USD")
|
416
434
|
end
|
417
435
|
|
418
436
|
it "falls back to default currency, in fixnum assignments" do
|
419
437
|
service.discount = 5
|
420
|
-
service.save.
|
421
|
-
service.discount.cents.
|
422
|
-
service.discount.currency_as_string.
|
438
|
+
expect(service.save).to be_truthy
|
439
|
+
expect(service.discount.cents).to eq(500)
|
440
|
+
expect(service.discount.currency_as_string).to eq("EUR")
|
423
441
|
end
|
424
442
|
|
425
443
|
it "falls back to default currency, in string assignments" do
|
426
444
|
service.discount = "5"
|
427
|
-
service.save.
|
428
|
-
service.discount.cents.
|
429
|
-
service.discount.currency_as_string.
|
445
|
+
expect(service.save).to be_truthy
|
446
|
+
expect(service.discount.cents).to eq(500)
|
447
|
+
expect(service.discount.currency_as_string).to eq("EUR")
|
430
448
|
end
|
431
449
|
|
432
450
|
it "sets field to nil, in nil assignments if allow_nil is set" do
|
433
451
|
product.optional_price = nil
|
434
|
-
product.save.
|
435
|
-
product.optional_price.
|
452
|
+
expect(product.save).to be_truthy
|
453
|
+
expect(product.optional_price).to be_nil
|
436
454
|
end
|
437
455
|
|
438
456
|
it "sets field to nil, in instantiation if allow_nil is set" do
|
439
457
|
pr = Product.new(:optional_price => nil, :price_cents => 5320,
|
440
458
|
:discount => 350, :bonus_cents => 320)
|
441
|
-
pr.optional_price.
|
442
|
-
pr.save.
|
443
|
-
pr.optional_price.
|
459
|
+
expect(pr.optional_price).to be_nil
|
460
|
+
expect(pr.save).to be_truthy
|
461
|
+
expect(pr.optional_price).to be_nil
|
444
462
|
end
|
445
463
|
|
446
464
|
it "sets field to nil, in blank assignments if allow_nil is set" do
|
447
465
|
product.optional_price = ""
|
448
|
-
product.save.
|
449
|
-
product.optional_price.
|
466
|
+
expect(product.save).to be_truthy
|
467
|
+
expect(product.optional_price).to be_nil
|
468
|
+
end
|
469
|
+
|
470
|
+
|
471
|
+
context "when the monetized field is an aliased attribute" do
|
472
|
+
it "writes the subunits to the original (unaliased) column" do
|
473
|
+
pending if Rails::VERSION::MAJOR < 4
|
474
|
+
product.renamed = "$10.00"
|
475
|
+
expect(product.aliased_cents).to eq 10_00
|
476
|
+
end
|
450
477
|
end
|
451
478
|
|
452
479
|
context "for column with model currency:" do
|
@@ -458,7 +485,7 @@ if defined? ActiveRecord
|
|
458
485
|
it "is overridden by instance currency column" do
|
459
486
|
product = Product.create(:sale_price_amount => 1234,
|
460
487
|
:sale_price_currency_code => 'CAD')
|
461
|
-
product.sale_price.currency_as_string.
|
488
|
+
expect(product.sale_price.currency_as_string).to eq('CAD')
|
462
489
|
end
|
463
490
|
|
464
491
|
it 'can change currency of custom column' do
|
@@ -469,14 +496,14 @@ if defined? ActiveRecord
|
|
469
496
|
:sale_price_amount => 1234,
|
470
497
|
:sale_price_currency_code => 'USD')
|
471
498
|
|
472
|
-
product.sale_price.currency_as_string.
|
499
|
+
expect(product.sale_price.currency_as_string).to eq('USD')
|
473
500
|
|
474
501
|
product.sale_price = Money.new 456, 'CAD'
|
475
502
|
product.save
|
476
503
|
product.reload
|
477
504
|
|
478
|
-
product.sale_price.currency_as_string.
|
479
|
-
product.discount_value.currency_as_string.
|
505
|
+
expect(product.sale_price.currency_as_string).to eq('CAD')
|
506
|
+
expect(product.discount_value.currency_as_string).to eq('USD')
|
480
507
|
end
|
481
508
|
end
|
482
509
|
|
@@ -504,50 +531,52 @@ if defined? ActiveRecord
|
|
504
531
|
d.price = Money.new(10, "EUR")
|
505
532
|
d.save!
|
506
533
|
d.reload
|
507
|
-
d.currency.
|
534
|
+
expect(d.currency).to eq("EUR")
|
508
535
|
end
|
509
536
|
|
510
537
|
it "overrides default currency with the value of row currency" do
|
511
|
-
transaction.amount.currency.
|
538
|
+
expect(transaction.amount.currency).to eq(Money::Currency.find(:usd))
|
512
539
|
end
|
513
540
|
|
514
541
|
it "overrides default currency with the currency registered for the model" do
|
515
|
-
dummy_product_with_nil_currency.price.currency.
|
542
|
+
expect(dummy_product_with_nil_currency.price.currency).to eq(
|
516
543
|
Money::Currency.find(:gbp)
|
544
|
+
)
|
517
545
|
end
|
518
546
|
|
519
547
|
it "overrides default currency with the currency registered for the model if currency is invalid" do
|
520
|
-
dummy_product_with_invalid_currency.price.currency.
|
548
|
+
expect(dummy_product_with_invalid_currency.price.currency).to eq(
|
521
549
|
Money::Currency.find(:gbp)
|
550
|
+
)
|
522
551
|
end
|
523
552
|
|
524
553
|
it "overrides default and model currency with the row currency" do
|
525
|
-
dummy_product.price.currency.
|
554
|
+
expect(dummy_product.price.currency).to eq(Money::Currency.find(:usd))
|
526
555
|
end
|
527
556
|
|
528
557
|
it "constructs the money attribute from the stored mapped attribute values" do
|
529
|
-
transaction.amount.
|
558
|
+
expect(transaction.amount).to eq(Money.new(2400, :usd))
|
530
559
|
end
|
531
560
|
|
532
561
|
it "correctly instantiates Money objects from the mapped attributes" do
|
533
562
|
t = Transaction.new(:amount_cents => 2500, :currency => "CAD")
|
534
|
-
t.amount.
|
563
|
+
expect(t.amount).to eq(Money.new(2500, "CAD"))
|
535
564
|
end
|
536
565
|
|
537
566
|
it "correctly assigns Money objects to the attribute" do
|
538
567
|
transaction.amount = Money.new(2500, :eur)
|
539
|
-
transaction.save.
|
540
|
-
transaction.amount.cents.
|
541
|
-
transaction.amount.currency_as_string.
|
568
|
+
expect(transaction.save).to be_truthy
|
569
|
+
expect(transaction.amount.cents).to eq(Money.new(2500, :eur).cents)
|
570
|
+
expect(transaction.amount.currency_as_string).to eq("EUR")
|
542
571
|
end
|
543
572
|
|
544
573
|
it "uses default currency if a non Money object is assigned to the attribute" do
|
545
574
|
transaction.amount = 234
|
546
|
-
transaction.amount.currency_as_string.
|
575
|
+
expect(transaction.amount.currency_as_string).to eq("USD")
|
547
576
|
end
|
548
577
|
|
549
578
|
it "constructs the money object from the mapped method value" do
|
550
|
-
transaction.total.
|
579
|
+
expect(transaction.total).to eq(Money.new(3000, :usd))
|
551
580
|
end
|
552
581
|
|
553
582
|
end
|
@@ -555,8 +584,8 @@ if defined? ActiveRecord
|
|
555
584
|
|
556
585
|
describe "register_currency" do
|
557
586
|
it "attaches currency at model level" do
|
558
|
-
Product.currency.
|
559
|
-
DummyProduct.currency.
|
587
|
+
expect(Product.currency).to eq(Money::Currency.find(:usd))
|
588
|
+
expect(DummyProduct.currency).to eq(Money::Currency.find(:gbp))
|
560
589
|
end
|
561
590
|
end
|
562
591
|
end
|