Skip to content

armgit / sure

Public Imported from GitHub
0

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.rb
index 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 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
diff --git a/app/models/concerns/enrichable.rb b/app/models/concerns/enrichable.rb
index d963c663d2..a354d42457 100644
--- a/app/models/concerns/enrichable.rb
+++ b/app/models/concerns/enrichable.rb
@@ -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
diff --git a/app/models/entry.rb b/app/models/entry.rb
index 31118db455..1436eb5456 100644
--- a/app/models/entry.rb
+++ b/app/models/entry.rb
@@ -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
diff --git a/app/models/transaction.rb b/app/models/transaction.rb
index 4218e980e1..4f94bb488f 100644
--- a/app/models/transaction.rb
+++ b/app/models/transaction.rb
@@ -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?