1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-05 13:35:21 +02:00

Account namespace updates: part 4 (transfers, singular namespacing) (#896)

* Move Transfer to Account namespace

* Fix partial resolution due to namespacing plurality

* Make category and tag controllers consistent with namespacing convention

* Update stale partial reference
This commit is contained in:
Zach Gollwitzer 2024-06-20 13:32:44 -04:00 committed by GitHub
parent dc3147c101
commit bddaab0192
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
50 changed files with 227 additions and 127 deletions

View file

@ -0,0 +1,57 @@
class Account::Transfer < ApplicationRecord
has_many :transactions, dependent: :nullify
validate :transaction_count, :from_different_accounts, :net_zero_flows, :all_transactions_marked
def inflow_transaction
transactions.find { |t| t.inflow? }
end
def outflow_transaction
transactions.find { |t| t.outflow? }
end
def destroy_and_remove_marks!
transaction do
transactions.each do |t|
t.update! marked_as_transfer: false
end
destroy!
end
end
class << self
def build_from_accounts(from_account, to_account, date:, amount:, currency:, name:)
outflow = from_account.transactions.build(amount: amount.abs, currency: currency, date: date, name: name, marked_as_transfer: true)
inflow = to_account.transactions.build(amount: -amount.abs, currency: currency, date: date, name: name, marked_as_transfer: true)
new transactions: [ outflow, inflow ]
end
end
private
def transaction_count
unless transactions.size == 2
errors.add :transactions, "must have exactly 2 transactions"
end
end
def from_different_accounts
accounts = transactions.map(&:account_id).uniq
errors.add :transactions, "must be from different accounts" if accounts.size < transactions.size
end
def net_zero_flows
unless transactions.sum(&:amount).zero?
errors.add :transactions, "must have an inflow and outflow that net to zero"
end
end
def all_transactions_marked
unless transactions.all?(&:marked_as_transfer)
errors.add :transactions, "must be marked as transfer"
end
end
end