mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 14:31:25 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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?
|
||||
|
||||
Reference in New Issue
Block a user