Files
sure/app/models/transaction/transferable.rb
ghost 2fb7962d6e perf(transfers): narrow auto-transfer candidate search (#2080)
* perf(transfers): narrow auto-transfer candidate search

* perf(transfers): preload auto-match transactions

* perf(transfers): tighten auto-match candidate handling

* fix(transfers): reject negative match tolerance

* test(statements): stabilize coverage status dates

---------

Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2026-06-01 22:02:01 +02:00

37 lines
1.4 KiB
Ruby

module Transaction::Transferable
extend ActiveSupport::Concern
included do
has_one :transfer_as_inflow, class_name: "Transfer", foreign_key: "inflow_transaction_id", dependent: :destroy
has_one :transfer_as_outflow, class_name: "Transfer", foreign_key: "outflow_transaction_id", dependent: :destroy
# We keep track of rejected transfers to avoid auto-matching them again
has_one :rejected_transfer_as_inflow, class_name: "RejectedTransfer", foreign_key: "inflow_transaction_id", dependent: :destroy
has_one :rejected_transfer_as_outflow, class_name: "RejectedTransfer", foreign_key: "outflow_transaction_id", dependent: :destroy
end
def transfer
transfer_as_inflow || transfer_as_outflow
end
def transfer_match_candidates(date_window: 30)
candidates_scope = if self.entry.amount.negative?
family_matches_scope(date_window: date_window, inflow_transaction_id: self.id)
else
family_matches_scope(date_window: date_window, outflow_transaction_id: self.id)
end
candidates_scope.map do |match|
Transfer.new(
inflow_transaction_id: match.inflow_transaction_id,
outflow_transaction_id: match.outflow_transaction_id,
)
end
end
private
def family_matches_scope(date_window:, **filters)
self.entry.account.family.transfer_match_candidates(date_window: date_window, **filters)
end
end