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.
@@ -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.should == Product.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.should be_an_instance_of(Money)
24
- product.discount_value.should be_an_instance_of(Money)
25
- product.bonus.should be_an_instance_of(Money)
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.should == Money.new(3000, "USD")
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.should eq(true)
35
- product.price_cents.should == 3210
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?.should eq(true)
42
- product.price_cents.should == 3210
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")).should eq(true)
47
- product.price_cents.should == 215
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.should == Money.new(150, "USD")
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.should eq(false)
71
+ expect(product.save).to be_falsey
64
72
 
65
73
  product.price_cents = 2000
66
- product.save.should eq(true)
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.should eq(true)
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.should_not be_valid
84
+ expect(product).not_to be_valid
77
85
  product.price_in_a_range_cents = 500
78
- product.should be_valid
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").should eq(false)
98
- product.update_attributes(:price_cents => 2000).should eq(true)
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.should eq(false)
111
+ expect(product.save).to be_falsey
104
112
 
105
113
  product.price = Money.new(320, "USD")
106
- product.save.should eq(true)
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?.should eq(true)
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.should eq(false)
116
- product.errors[:price].first.should match(/Must be a valid/)
117
- product.errors[:price].first.should match(/Got 12.23.24/)
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.should eq(false)
123
- product.errors[:price].first.should match(/Must be a valid/)
124
- product.errors[:price].first.should match(/Got .../)
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.should eq(false)
130
- product.errors[:price].first.should match(/Must be a valid/)
131
- product.errors[:price].first.should match(/Got 12,23.24/)
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.should eq(true)
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.should eq(true)
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.should eq(true)
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?.should eq(false)
153
- product.errors[:price_in_a_range].first.should match(/Must be greater than zero and less than \$100/)
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?.should eq(false)
157
- product.errors[:price_in_a_range].first.should match(/Must be greater than zero and less than \$100/)
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?.should eq(false)
161
- product.errors[:price_in_a_range].first.should match(/Must be greater than zero and less than \$100/)
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?.should eq(true)
182
+ expect(product.valid?).to be_truthy
165
183
 
166
184
  product.price_in_a_range = Money.new(1200, "USD")
167
- product.valid?.should eq(true)
185
+ expect(product.valid?).to be_truthy
168
186
 
169
187
  product.price_in_a_range = "101"
170
- product.valid?.should eq(false)
171
- product.errors[:price_in_a_range].first.should match(/Must be greater than zero and less than \$100/)
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?.should eq(false)
175
- product.errors[:price_in_a_range].first.should match(/Must be greater than zero and less than \$100/)
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?.should eq(false)
181
- product.errors[:validates_method_amount].first.should match(/Must be greater than zero and less than \$100/)
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?.should eq(false)
185
- product.errors[:validates_method_amount].first.should match(/Must be greater than zero and less than \$100/)
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?.should eq(false)
189
- product.errors[:validates_method_amount].first.should match(/Must be greater than zero and less than \$100/)
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?.should eq(true)
210
+ expect(product.valid?).to be_truthy
193
211
 
194
212
  product.validates_method_amount = Money.new(1200, "USD")
195
- product.valid?.should eq(true)
213
+ expect(product.valid?).to be_truthy
196
214
 
197
215
  product.validates_method_amount = "101"
198
- product.valid?.should eq(false)
199
- product.errors[:validates_method_amount].first.should match(/Must be greater than zero and less than \$100/)
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?.should eq(false)
203
- product.errors[:validates_method_amount].first.should match(/Must be greater than zero and less than \$100/)
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?.should eq(false)
209
- product.errors[:price_in_a_range_cents].first.should match(/greater than 0/)
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?.should eq(false)
213
- product.errors[:price_in_a_range_cents].first.should match(/greater than 0/)
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?.should eq(true)
234
+ expect(product.valid?).to be_truthy
217
235
 
218
236
  product.price_in_a_range = "101"
219
- product.valid?.should eq(false)
220
- product.errors[:price_in_a_range_cents].first.should match(/less than or equal to 10000/)
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?.should eq(false)
226
- product.errors[:price_in_a_range].first.should match(/greater than zero/)
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?.should eq(false)
230
- product.errors[:price_in_a_range].first.should match(/greater than zero/)
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?.should eq(false)
234
- product.errors[:price].first.should match(/is not a number/)
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?.should eq(false)
238
- product.errors[:price].first.should match(/is not a number/)
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.should be_valid
244
- product.price_cents.should == 9999999999
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.should be_valid
250
- product.price_cents.should == 9999999999
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.should eq(true)
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.should eq(true)
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").should eq(false)
265
- product.update_attributes(:price => Money.new(320, 'USD')).should eq(true)
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.should == Money.new(1200, :eur)
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.should == 1200
276
- transaction.valid?.should eq(true)
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.should == Money.new(1200, :eur)
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.should == 1200
290
- transaction.valid?.should eq(true)
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.should eq(false)
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.should eq(true)
304
- product.optional_price.should be_nil
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.should_not be_nil
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.should == v
338
+ expect(product.price_money_before_type_cast).to eq(v)
321
339
  product.save
322
- product.price_money_before_type_cast.should be_nil
340
+ expect(product.price_money_before_type_cast).to be_nil
323
341
  product.price = 10
324
- product.price_money_before_type_cast.should == 10
342
+ expect(product.price_money_before_type_cast).to eq(10)
325
343
  product.save
326
- product.price_money_before_type_cast.should be_nil
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.should == ""
332
- product.save.should eq(false)
333
- product.bonus_money_before_type_cast.should == ""
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.should == Money::Currency.find(:eur)
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.should == Money::Currency.find(:usd)
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.should == Money::Currency.find(:usd)
346
- product.bonus.currency.should == Money::Currency.find(:gbp)
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.should eq(true)
352
- product.price.cents.should == 2500
353
- product.price.currency_as_string.should == "USD"
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.should eq(true)
359
- product.price.cents.should == 2500
360
- product.price.currency_as_string.should == "USD"
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.should eq(true)
364
- service.discount.cents.should == 200
365
- service.discount.currency_as_string.should == "EUR"
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.should eq(true)
371
- product.price.cents.should == 2500
372
- product.price.currency_as_string.should == "USD"
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.should eq(true)
376
- service.discount.cents.should == 200
377
- service.discount.currency_as_string.should == "EUR"
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.should eq(true)
383
- product.bonus.cents.should == 2500
384
- product.bonus.currency_as_string.should == "GBP"
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.should eq(true)
388
- service.charge.cents.should == 200
389
- service.charge.currency_as_string.should == "USD"
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.should eq(true)
395
- product.bonus.cents.should == 2500
396
- product.bonus.currency_as_string.should == "GBP"
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.should eq(true)
400
- service.charge.cents.should == 200
401
- service.charge.currency_as_string.should == "USD"
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.should eq(true)
407
- product.discount_value.cents.should == 500
408
- product.discount_value.currency_as_string.should == "USD"
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.should eq(true)
414
- product.discount_value.cents.should == 500
415
- product.discount_value.currency_as_string.should == "USD"
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.should eq(true)
421
- service.discount.cents.should == 500
422
- service.discount.currency_as_string.should == "EUR"
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.should eq(true)
428
- service.discount.cents.should == 500
429
- service.discount.currency_as_string.should == "EUR"
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.should eq(true)
435
- product.optional_price.should be_nil
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.should be_nil
442
- pr.save.should eq(true)
443
- pr.optional_price.should be_nil
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.should eq(true)
449
- product.optional_price.should be_nil
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.should == 'CAD'
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.should == 'USD'
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.should == 'CAD'
479
- product.discount_value.currency_as_string.should == 'USD'
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.should == "EUR"
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.should == Money::Currency.find(:usd)
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.should ==
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.should ==
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.should == Money::Currency.find(:usd)
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.should == Money.new(2400, :usd)
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.should == Money.new(2500, "CAD")
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.should eq(true)
540
- transaction.amount.cents.should == Money.new(2500, :eur).cents
541
- transaction.amount.currency_as_string.should == "EUR"
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.should == "USD"
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.should == Money.new(3000, :usd)
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.should == Money::Currency.find(:usd)
559
- DummyProduct.currency.should == Money::Currency.find(:gbp)
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