`super` needs to run for numericality validations when Money object is used #91

Closed
geehsien opened this Issue Mar 21, 2013 · 1 comment

2 participants

@geehsien

Relating to issue #79, at https://github.com/RubyMoney/money-rails/blob/master/lib/money-rails/active_model/validator.rb#L17 we have:

return if raw_value.is_a?(Money) || raw_value.nil?

However, this stops https://github.com/RubyMoney/money-rails/blob/master/lib/money-rails/active_model/validator.rb#L60 from getting run:

super(record, attr, raw_value)

This is unexpected behavior, since it may skip numericality validations passed in as options. The example below shows numericality validations requiring that the price is greater than or equal to 0. In the last case, when we have a Money object, those validations never run.

# Model
monetize :price_cents, numericality: { greater_than_or_equal_to: 0 }

# Console
> obj.price = "-100"
 => "-100" 
> obj.valid?; obj.errors[:price]
 => ["must be greater than or equal to 0"] 

> obj.price = -100
 => -100 
> obj.valid?; obj.errors[:price]
 => ["must be greater than or equal to 0"] 

> obj.price = Money.new(-100)
 => #<Money fractional:-100 currency:USD> 
> obj.valid?; obj.errors[:price]
 => [] 
@alup
RubyMoney member

Fixed in master

@alup alup closed this Jun 18, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment