1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-24 15:49:39 +02:00
Maybe/app/models/account.rb
Josh Brown e767aca37f
Refresh account on update (#476)
* Refresh account on update

* 🐛 Replace turbo frame on create valuation

This avoids the frame reloading it's src when refreshing.

* Change Action Cable adapater to Postgres
2024-02-23 20:18:30 -06:00

47 lines
1.5 KiB
Ruby

class Account < ApplicationRecord
broadcasts_refreshes
belongs_to :family
has_many :balances, class_name: "AccountBalance"
has_many :valuations
delegated_type :accountable, types: Accountable::TYPES, dependent: :destroy
delegate :type_name, to: :accountable
before_create :check_currency
def balance_series(period)
filtered_balances = balances.in_period(period).order(:date)
return nil if filtered_balances.empty?
series_data = [ nil, *filtered_balances ].each_cons(2).map do |previous, current|
trend = current&.trend(previous)
{ data: current, trend: { amount: trend&.amount, direction: trend&.direction, percent: trend&.percent } }
end
last_balance = series_data.last[:data]
{
series_data: series_data,
last_balance: last_balance.balance,
trend: last_balance.trend(series_data.first[:data])
}
end
def valuation_series
series_data = [ nil, *valuations.order(:date) ].each_cons(2).map do |previous, current|
{ value: current, trend: current&.trend(previous) }
end
series_data.reverse_each
end
def check_currency
if self.original_currency == self.family.currency
self.converted_balance = self.original_balance
self.converted_currency = self.original_currency
else
self.converted_balance = ExchangeRate.convert(self.original_currency, self.family.currency, self.original_balance)
self.converted_currency = self.family.currency
end
end
end