Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(rules): batch category/merchant/tag operand resolution in exports (#3416)

GFR7f2e528603ff
diff --git a/app/models/family/data_exporter.rb b/app/models/family/data_exporter.rb
index bc55fc64ef..49a7603131 100644
--- a/app/models/family/data_exporter.rb
+++ b/app/models/family/data_exporter.rb
@@ -761,17 +761,17 @@ class Family::DataExporter
# Map category UUIDs to names for portability
if condition.condition_type == "transaction_category"
- return rule_operand(condition.value, type: "Category", relation: @family.categories)
+ return rule_operand(condition.value, type: "Category", relation: :categories)
end
# Map merchant UUIDs to names for portability
if condition.condition_type == "transaction_merchant"
- return rule_operand(condition.value, type: "Merchant", relation: @family.merchants)
+ return rule_operand(condition.value, type: "Merchant", relation: :merchants)
end
# Map tag UUIDs to names for portability
if condition.condition_type == "transaction_tag"
- return rule_operand(condition.value, type: "Tag", relation: @family.tags)
+ return rule_operand(condition.value, type: "Tag", relation: :tags)
end
rule_operand(condition.value)
@@ -782,12 +782,12 @@ class Family::DataExporter
# Map category UUIDs to names for portability
if action.action_type == "set_transaction_category"
- return rule_operand(action.value, type: "Category", relation: @family.categories, fallback_to_name: true)
+ return rule_operand(action.value, type: "Category", relation: :categories, fallback_to_name: true)
end
# Map merchant UUIDs to names for portability
if action.action_type == "set_transaction_merchant"
- return rule_operand(action.value, type: "Merchant", relation: @family.merchants, fallback_to_name: true)
+ return rule_operand(action.value, type: "Merchant", relation: :merchants, fallback_to_name: true)
end
# Map tag UUIDs to names for portability. Stored as a comma-separated
@@ -802,7 +802,7 @@ class Family::DataExporter
def resolve_multi_tag_operand(value)
ids = value.to_s.split(",")
- records = ids.map { |id| resolve_rule_operand_record(@family.tags, id, fallback_to_name: true) }
+ records = ids.map { |id| resolve_rule_operand_record(:tags, id, fallback_to_name: true) }
names = records.each_with_index.map { |record, i| record&.name || ids[i] }
refs = records.compact.map { |record| rule_value_ref("Tag", record) }
@@ -827,10 +827,28 @@ class Family::DataExporter
}
end
- def resolve_rule_operand_record(relation, value, fallback_to_name:)
- return relation.find_by(id: value) if uuid_like?(value)
+ def resolve_rule_operand_record(relation_key, value, fallback_to_name:)
+ return operand_records_by_id(relation_key)[value.to_s.downcase] if uuid_like?(value)
- relation.find_by(name: value) if fallback_to_name
+ operand_records_by_name(relation_key)[value] if fallback_to_name
+ end
+
+ def operand_records(relation_key)
+ @operand_records ||= {}
+ # `.reload` bypasses any pre-existing in-memory association cache on
+ # `@family` (e.g. a prior `create!` through the same association) so
+ # destroyed records aren't served stale from that cache.
+ @operand_records[relation_key] ||= @family.public_send(relation_key).reload.to_a
+ end
+
+ def operand_records_by_id(relation_key)
+ @operand_records_by_id ||= {}
+ @operand_records_by_id[relation_key] ||= operand_records(relation_key).index_by(&:id)
+ end
+
+ def operand_records_by_name(relation_key)
+ @operand_records_by_name ||= {}
+ @operand_records_by_name[relation_key] ||= operand_records(relation_key).index_by(&:name)
end
def rule_value_ref(type, record)
diff --git a/test/models/family/data_exporter_test.rb b/test/models/family/data_exporter_test.rb
index a87c6fc0c6..141b6b5d83 100644
--- a/test/models/family/data_exporter_test.rb
+++ b/test/models/family/data_exporter_test.rb
@@ -522,17 +522,28 @@ class Family::DataExporterTest < ActiveSupport::TestCase
end
end
+ test "rule operand lookup matches uppercase UUID values" do
+ operand = @exporter.send(
+ :rule_operand,
+ @category.id.upcase,
+ type: "Category",
+ relation: :categories,
+ fallback_to_name: true
+ )
+
+ assert_equal "Test Category", operand[:value]
+ assert_equal({ type: "Category", id: @category.id, name: "Test Category" }, operand[:value_ref])
+ end
+
test "rule operand lookup skips name fallback for stale UUID values" do
stale_uuid = SecureRandom.uuid
- relation = mock
- relation.expects(:find_by).with(id: stale_uuid).once.returns(nil)
- relation.expects(:find_by).with(name: stale_uuid).never
+ @exporter.expects(:operand_records_by_name).never
operand = @exporter.send(
:rule_operand,
stale_uuid,
type: "Category",
- relation: relation,
+ relation: :categories,
fallback_to_name: true
)
@@ -540,6 +551,13 @@ class Family::DataExporterTest < ActiveSupport::TestCase
assert_nil operand[:value_ref]
end
+ test "operand id and name lookups for the same relation share a single query" do
+ assert_queries_count(1) do
+ @exporter.send(:operand_records_by_id, :categories)
+ @exporter.send(:operand_records_by_name, :categories)
+ end
+ end
+
test "exports rule actions and maps tag UUIDs to names" do
# Create a rule with a tag action
tag_rule = @family.rules.build(