Fix tags getting removed after / during bank sync (#634)

* fix: Preserve transaction tags during rule application

When rules set tags, they now ADD to existing tags instead of replacing
them. This fixes issue #518 where tags were being removed during bank sync.

The root cause was that SetTransactionTags called enrich_attribute with
just the single tag from the rule, which replaced all existing tags.
Now it merges the new tag with existing tags using .uniq to prevent
duplicates.

This preserves:
- User-applied tags that shouldn't be overwritten by rules
- Tags from other rules when multiple rules match the same transaction
- Tags set during previous syncs

* fix: Add nil guard for tag in SetTransactionTags

Return early with 0 if the tag is not found, preventing NoMethodError
when find_by_id returns nil. This matches the pattern used in
SetTransactionMerchant.

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Juan José Mata
2026-01-13 14:33:46 +01:00
committed by GitHub
parent 0bf3084738
commit 88241cf5cb
3 changed files with 87 additions and 1 deletions

View File

@@ -59,6 +59,51 @@ class Rule::ActionTest < ActiveSupport::TestCase
end
end
test "set_transaction_tags preserves existing tags" do
existing_tag = @family.tags.create!(name: "Existing tag")
new_tag = @family.tags.create!(name: "New tag from rule")
# Add existing tag to transaction
@txn2.tags << existing_tag
@txn2.save!
assert_equal [ existing_tag ], @txn2.reload.tags
action = Rule::Action.new(
rule: @transaction_rule,
action_type: "set_transaction_tags",
value: new_tag.id
)
action.apply(@rule_scope)
# Transaction should have BOTH the existing tag and the new tag
@txn2.reload
assert_includes @txn2.tags, existing_tag
assert_includes @txn2.tags, new_tag
assert_equal 2, @txn2.tags.count
end
test "set_transaction_tags does not duplicate existing tags" do
tag = @family.tags.create!(name: "Single tag")
# Add tag to transaction
@txn2.tags << tag
@txn2.save!
assert_equal [ tag ], @txn2.reload.tags
action = Rule::Action.new(
rule: @transaction_rule,
action_type: "set_transaction_tags",
value: tag.id
)
action.apply(@rule_scope)
# Transaction should still have just one tag (not duplicated)
@txn2.reload
assert_equal [ tag ], @txn2.tags
end
test "set_transaction_merchant" do
merchant = @family.merchants.create!(name: "Rule test merchant")