1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-24 15:49:39 +02:00

Allow for optional start date on account creation (#866)

This commit is contained in:
Zach Gollwitzer 2024-06-13 09:16:00 -04:00 committed by GitHub
parent c5704ffd45
commit 8c1a7af37f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 83 additions and 33 deletions

View file

@ -80,4 +80,20 @@ class Account < ApplicationRecord
grouped_accounts
end
def self.create_with_optional_start_balance!(attributes:, start_date: nil, start_balance: nil)
account = self.new(attributes.except(:accountable_type))
account.accountable = Accountable.from_type(attributes[:accountable_type])&.new
# Always build the initial valuation
account.valuations.build(date: Date.current, value: attributes[:balance], currency: account.currency)
# Conditionally build the optional start valuation
if start_date.present? && start_balance.present?
account.valuations.build(date: start_date, value: start_balance, currency: account.currency)
end
account.save!
account
end
end