Files
sure/app/models/exchange_rate.rb
Dave Corson-Knowles e5750d1a13 Add presence validations for required fields (#545)
* Adds basic validations for required fields

Also deletes a few extraneous .keep files

Does not add the family_id required field for user, since that breaks the basic test setup

* Restore keep files to this branch

* Remove Credit model and validate models behind ids

* Restore concerns .keep
2024-03-15 15:21:59 -04:00

17 lines
466 B
Ruby

class ExchangeRate < ApplicationRecord
validates :base_currency, :converted_currency, presence: true
def self.convert(from, to, amount)
return amount unless EXCHANGE_RATE_ENABLED
rate = ExchangeRate.find_by(base_currency: from, converted_currency: to)
# TODO: Handle the case where the rate is not found
if rate.nil?
amount # Silently handle the error by returning the original amount
else
amount * rate.rate
end
end
end