Refactor clear_ai_cache to use family_scope pattern

- Move family-scoped queries to models via family_scope class method
- Remove hardcoded model names from Enrichable concern
- Replace inline rescue with proper respond_to? check
- Add count tracking for better logging
This commit is contained in:
eureka928
2026-01-26 10:17:28 +01:00
parent 029d09685e
commit 23e9749ed1
4 changed files with 25 additions and 20 deletions

View File

@@ -11,16 +11,16 @@ class ClearAiCacheJob < ApplicationJob
# Clear AI enrichment data for transactions
begin
Transaction.clear_ai_cache(family)
Rails.logger.info("Cleared AI cache for transactions")
count = Transaction.clear_ai_cache(family)
Rails.logger.info("Cleared AI cache for #{count} transactions")
rescue => e
Rails.logger.error("Failed to clear AI cache for transactions: #{e.message}")
end
# Clear AI enrichment data for entries
begin
Entry.clear_ai_cache(family)
Rails.logger.info("Cleared AI cache for entries")
count = Entry.clear_ai_cache(family)
Rails.logger.info("Cleared AI cache for #{count} entries")
rescue => e
Rails.logger.error("Failed to clear AI cache for entries: #{e.message}")
end

View File

@@ -25,24 +25,18 @@ module Enrichable
end
class_methods do
def clear_ai_cache(family)
# Get all records that belong to this family
records = if respond_to?(:joins)
case name
when "Transaction"
joins(entry: :account).where(accounts: { family_id: family.id })
when "Entry"
joins(:account).where(accounts: { family_id: family.id })
else
none
end
else
none
end
# Override in models to define family-scoped query
def family_scope(family)
none
end
records.find_each do |record|
def clear_ai_cache(family)
count = 0
family_scope(family).find_each do |record|
record.clear_ai_cache
count += 1
end
count
end
end
@@ -155,7 +149,8 @@ module Enrichable
# Only unlock attributes where current value still matches what AI set
# If user changed the value, they took ownership - don't unlock
attrs_to_unlock = ai_enrichments.select do |enrichment|
current_value = send(enrichment.attribute_name) rescue self[enrichment.attribute_name]
attr_name = enrichment.attribute_name
current_value = respond_to?(attr_name) ? send(attr_name) : self[attr_name]
current_value.to_s == enrichment.value.to_s
end.map(&:attribute_name).uniq

View File

@@ -66,6 +66,11 @@ class Entry < ApplicationRecord
pending.where("entries.date < ?", days.days.ago.to_date)
}
# Family-scoped query for Enrichable#clear_ai_cache
def self.family_scope(family)
joins(:account).where(accounts: { family_id: family.id })
end
# Auto-exclude stale pending transactions for an account
# Called during sync to clean up pending transactions that never posted
# @param account [Account] The account to clean up

View File

@@ -47,6 +47,11 @@ class Transaction < ApplicationRecord
SQL
}
# Family-scoped query for Enrichable#clear_ai_cache
def self.family_scope(family)
joins(entry: :account).where(accounts: { family_id: family.id })
end
# Overarching grouping method for all transfer-type transactions
def transfer?
funds_movement? || cc_payment? || loan_payment?