2024-02-20 09:07:55 -05:00
|
|
|
class Valuation < ApplicationRecord
|
2024-03-18 11:21:00 -04:00
|
|
|
include Monetizable
|
2024-02-22 11:35:06 -05:00
|
|
|
|
2024-03-18 11:21:00 -04:00
|
|
|
belongs_to :account
|
2024-03-15 12:21:59 -07:00
|
|
|
validates :account, :date, :value, presence: true
|
2024-02-29 08:32:52 -05:00
|
|
|
after_commit :sync_account
|
2024-03-18 11:21:00 -04:00
|
|
|
monetize :value
|
2024-02-22 11:35:06 -05:00
|
|
|
|
2024-03-06 09:56:59 -05:00
|
|
|
scope :in_period, ->(period) { period.date_range.nil? ? all : where(date: period.date_range) }
|
|
|
|
|
|
|
|
def self.to_series(account, period = Period.all)
|
|
|
|
MoneySeries.new(
|
|
|
|
in_period(period).order(:date),
|
|
|
|
{ trend_type: account.classification, amount_accessor: :value }
|
|
|
|
)
|
2024-02-22 11:35:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2024-02-29 08:32:52 -05:00
|
|
|
def sync_account
|
|
|
|
self.account.sync_later
|
2024-02-22 11:35:06 -05:00
|
|
|
end
|
2024-02-20 09:07:55 -05:00
|
|
|
end
|