mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 15:49:39 +02:00
* Add start_balance to accounts * Add tests * Cleanup * Refactor code and add tests * Update physical cash demo account to be manual * Do not populate start_balance in migration * Cleanup * Review fixes * Revert calc change * Update app/models/exchange_rate.rb Co-authored-by: Zach Gollwitzer <zach.gollwitzer@gmail.com> Signed-off-by: Jakub Kottnauer <jk@jakubkottnauer.com> * Add test * Fix syncable bug and update csv tests --------- Signed-off-by: Jakub Kottnauer <jk@jakubkottnauer.com> Co-authored-by: Zach Gollwitzer <zach.gollwitzer@gmail.com>
29 lines
890 B
Ruby
29 lines
890 B
Ruby
class ExchangeRate < ApplicationRecord
|
|
include Provided
|
|
|
|
validates :base_currency, :converted_currency, presence: true
|
|
|
|
class << self
|
|
def find_rate(from:, to:, date:)
|
|
find_by \
|
|
base_currency: Money::Currency.new(from).iso_code,
|
|
converted_currency: Money::Currency.new(to).iso_code,
|
|
date: date
|
|
end
|
|
|
|
def find_rate_or_fetch(from:, to:, date:)
|
|
find_rate(from:, to:, date:) || fetch_rate_from_provider(from:, to:, date:).tap(&:save!)
|
|
end
|
|
|
|
def get_rate_series(from, to, date_range)
|
|
where(base_currency: from, converted_currency: to, date: date_range).order(:date)
|
|
end
|
|
|
|
def convert(value:, from:, to:, date:)
|
|
rate = ExchangeRate.find_by(base_currency: from, converted_currency: to, date:)
|
|
raise "Conversion from: #{from} to: #{to} on: #{date} not found" unless rate
|
|
|
|
value * rate.rate
|
|
end
|
|
end
|
|
end
|