RubyMoney / money-rails

Integration of RubyMoney - Money with Rails
MIT License
1.8k stars 387 forks source link

Support validation when `monetized` is used #649

Closed wwahammy closed 1 year ago

wwahammy commented 2 years ago

Supports validation when a hash is provided to a monetized attribute.

Example:

Given Product:

class Product
  monetize :price_in_a_range_cents, allow_nil: true,
                                    subunit_numericality: {
                                      greater_than: 0,
                                      less_than_or_equal_to: 10000
                                    },
                                    numericality: {
                                      greater_than: 0,
                                      less_than_or_equal_to: 100,
                                      message: "must be greater than zero and less than $100"
                                    }
end

the following code passes validation:

Product.create(price_in_a_range:{amount: 4, currency: 'usd'})
Product.create(price_in_a_range:{cents: 4})
Product.create(price_in_a_range:{}) # treats it as if `nil` was passed

While the following would not pass:

Product.create(price_in_a_range:{currency: 'usd'}) # treat it as if `0` was passed to cents
Product.create(price_in_range:{some_other_key: 289}) # treat it as if `0` was passed to cents

Closes #627

wwahammy commented 2 years ago

Done!

semmons99 commented 2 years ago

need to figure out why test is failing, hang tight

semmons99 commented 2 years ago

happy to merge once tests are passing