The personal finance app for everyone (by everyone)
Ruby
Refactor clear_ai_cache to use family_scope pattern
eureka92823e9749ed18c
diff --git a/app/jobs/clear_ai_cache_job.rb b/app/jobs/clear_ai_cache_job.rbindex 0592938189..478acba648 100644--- a/app/jobs/clear_ai_cache_job.rb+++ b/app/jobs/clear_ai_cache_job.rb@@ -11,16 +11,16 @@ class ClearAiCacheJob < ApplicationJob# Clear AI enrichment data for transactionsbegin- 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 => eRails.logger.error("Failed to clear AI cache for transactions: #{e.message}")end# Clear AI enrichment data for entriesbegin- 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 => eRails.logger.error("Failed to clear AI cache for entries: #{e.message}")enddiff --git a/app/models/concerns/enrichable.rb b/app/models/concerns/enrichable.rbindex d963c663d2..a354d42457 100644--- a/app/models/concerns/enrichable.rb+++ b/app/models/concerns/enrichable.rb@@ -25,24 +25,18 @@ module Enrichableendclass_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 += 1end+ countendend@@ -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 unlockattrs_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_send.map(&:attribute_name).uniqdiff --git a/app/models/entry.rb b/app/models/entry.rbindex 31118db455..1436eb5456 100644--- a/app/models/entry.rb+++ b/app/models/entry.rb@@ -66,6 +66,11 @@ class Entry < ApplicationRecordpending.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 updiff --git a/app/models/transaction.rb b/app/models/transaction.rbindex 4218e980e1..4f94bb488f 100644--- a/app/models/transaction.rb+++ b/app/models/transaction.rb@@ -47,6 +47,11 @@ class Transaction < ApplicationRecordSQL}+ # 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 transactionsdef transfer?funds_movement? || cc_payment? || loan_payment?