Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Address PR review feedback

eureka928029d09685ee5
diff --git a/app/jobs/clear_ai_cache_job.rb b/app/jobs/clear_ai_cache_job.rb
index bff65fa793..0592938189 100644
--- a/app/jobs/clear_ai_cache_job.rb
+++ b/app/jobs/clear_ai_cache_job.rb
@@ -2,14 +2,27 @@ class ClearAiCacheJob < ApplicationJob
queue_as :low_priority
def perform(family)
+ if family.nil?
+ Rails.logger.warn("ClearAiCacheJob called with nil family, skipping")
+ return
+ end
+
Rails.logger.info("Clearing AI cache for family #{family.id}")
# Clear AI enrichment data for transactions
- Transaction.clear_ai_cache(family)
- Rails.logger.info("Cleared AI cache for transactions")
+ begin
+ Transaction.clear_ai_cache(family)
+ Rails.logger.info("Cleared AI cache for transactions")
+ rescue => e
+ Rails.logger.error("Failed to clear AI cache for transactions: #{e.message}")
+ end
# Clear AI enrichment data for entries
- Entry.clear_ai_cache(family)
- Rails.logger.info("Cleared AI cache for entries")
+ begin
+ Entry.clear_ai_cache(family)
+ Rails.logger.info("Cleared AI cache for entries")
+ rescue => e
+ Rails.logger.error("Failed to clear AI cache for entries: #{e.message}")
+ end
end
end
diff --git a/app/models/concerns/enrichable.rb b/app/models/concerns/enrichable.rb
index 04c557dc65..d963c663d2 100644
--- a/app/models/concerns/enrichable.rb
+++ b/app/models/concerns/enrichable.rb
@@ -150,17 +150,23 @@ module Enrichable
def clear_ai_cache
ActiveRecord::Base.transaction do
- # Find attributes that were locked by AI enrichment
- ai_enriched_attrs = data_enrichments.where(source: "ai").pluck(:attribute_name).uniq
-
- # Batch unlock all AI-enriched attributes in a single update
- if ai_enriched_attrs.any?
- new_locked_attrs = locked_attributes.except(*ai_enriched_attrs)
+ ai_enrichments = data_enrichments.where(source: "ai")
+
+ # 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]
+ current_value.to_s == enrichment.value.to_s
+ end.map(&:attribute_name).uniq
+
+ # Batch unlock in a single update
+ if attrs_to_unlock.any?
+ new_locked_attrs = locked_attributes.except(*attrs_to_unlock)
update_column(:locked_attributes, new_locked_attrs) if new_locked_attrs != locked_attributes
end
# Delete AI enrichment records
- data_enrichments.where(source: "ai").delete_all
+ ai_enrichments.delete_all
end
end