The personal finance app for everyone (by everyone)
Ruby
fix(rules): make explicit re-apply override locked attributes (#2273)
Augusto Xavier09dc428136f6
diff --git a/app/models/concerns/enrichable.rb b/app/models/concerns/enrichable.rbindex a354d42457..8b01698e2e 100644--- a/app/models/concerns/enrichable.rb+++ b/app/models/concerns/enrichable.rb@@ -41,20 +41,20 @@ module Enrichableend# Convenience method for a single attribute- def enrich_attribute(attr, value, source:, metadata: {})- enrich_attributes({ attr => value }, source:, metadata:)+ def enrich_attribute(attr, value, source:, metadata: {}, ignore_locks: false)+ enrich_attributes({ attr => value }, source:, metadata:, ignore_locks:)end# Enriches and logs all attributes that:- # - Are not locked+ # - Are not locked (unless ignore_locks: true, e.g. an explicit rule re-apply)# - Are not ignored# - Have changed value from the last saved value# Returns true if any attributes were actually changed, false otherwise- def enrich_attributes(attrs, source:, metadata: {})+ def enrich_attributes(attrs, source:, metadata: {}, ignore_locks: false)# Track current values before modification for virtual attributes (like tag_ids)current_values = {}enrichable_attrs = Array(attrs).reject do |attr_key, attr_value|- if locked?(attr_key) || ignored_enrichable_attributes.include?(attr_key)+ if (locked?(attr_key) && !ignore_locks) || ignored_enrichable_attributes.include?(attr_key)trueelse# For virtual attributes (like tag_ids), use the getter methoddiff --git a/app/models/rule/action_executor/exclude_transaction.rb b/app/models/rule/action_executor/exclude_transaction.rbindex e56edadc5f..79f1b0658f 100644--- a/app/models/rule/action_executor/exclude_transaction.rb+++ b/app/models/rule/action_executor/exclude_transaction.rb@@ -19,7 +19,8 @@ class Rule::ActionExecutor::ExcludeTransaction < Rule::ActionExecutortxn.entry.enrich_attribute(:excluded,true,- source: "rule"+ source: "rule",+ ignore_locks: ignore_attribute_locks)endenddiff --git a/app/models/rule/action_executor/set_investment_activity_label.rb b/app/models/rule/action_executor/set_investment_activity_label.rbindex 21e421292b..3ae4225183 100644--- a/app/models/rule/action_executor/set_investment_activity_label.rb+++ b/app/models/rule/action_executor/set_investment_activity_label.rb@@ -24,7 +24,8 @@ class Rule::ActionExecutor::SetInvestmentActivityLabel < Rule::ActionExecutortxn.enrich_attribute(:investment_activity_label,value,- source: "rule"+ source: "rule",+ ignore_locks: ignore_attribute_locks)endenddiff --git a/app/models/rule/action_executor/set_transaction_category.rb b/app/models/rule/action_executor/set_transaction_category.rbindex 790cc8ae2e..689372f00d 100644--- a/app/models/rule/action_executor/set_transaction_category.rb+++ b/app/models/rule/action_executor/set_transaction_category.rb@@ -22,7 +22,8 @@ class Rule::ActionExecutor::SetTransactionCategory < Rule::ActionExecutortxn.enrich_attribute(:category_id,category.id,- source: "rule"+ source: "rule",+ ignore_locks: ignore_attribute_locks)endenddiff --git a/app/models/rule/action_executor/set_transaction_merchant.rb b/app/models/rule/action_executor/set_transaction_merchant.rbindex 0b0d43ad3b..06bdf1b5d3 100644--- a/app/models/rule/action_executor/set_transaction_merchant.rb+++ b/app/models/rule/action_executor/set_transaction_merchant.rb@@ -21,7 +21,8 @@ class Rule::ActionExecutor::SetTransactionMerchant < Rule::ActionExecutortxn.enrich_attribute(:merchant_id,merchant.id,- source: "rule"+ source: "rule",+ ignore_locks: ignore_attribute_locks)endenddiff --git a/app/models/rule/action_executor/set_transaction_name.rb b/app/models/rule/action_executor/set_transaction_name.rbindex 4d4c84d8ab..737c258f0d 100644--- a/app/models/rule/action_executor/set_transaction_name.rb+++ b/app/models/rule/action_executor/set_transaction_name.rb@@ -24,7 +24,8 @@ class Rule::ActionExecutor::SetTransactionName < Rule::ActionExecutortxn.entry.enrich_attribute(:name,value,- source: "rule"+ source: "rule",+ ignore_locks: ignore_attribute_locks)endenddiff --git a/app/models/rule/action_executor/set_transaction_tags.rb b/app/models/rule/action_executor/set_transaction_tags.rbindex 80166950e2..00ceff10f4 100644--- a/app/models/rule/action_executor/set_transaction_tags.rb+++ b/app/models/rule/action_executor/set_transaction_tags.rb@@ -26,7 +26,8 @@ class Rule::ActionExecutor::SetTransactionTags < Rule::ActionExecutortxn.enrich_attribute(:tag_ids,merged_tag_ids,- source: "rule"+ source: "rule",+ ignore_locks: ignore_attribute_locks)endenddiff --git a/test/models/rule/action_test.rb b/test/models/rule/action_test.rbindex c6370653a1..6e115be67c 100644--- a/test/models/rule/action_test.rb+++ b/test/models/rule/action_test.rb@@ -38,6 +38,21 @@ class Rule::ActionTest < ActiveSupport::TestCaseendend+ test "set_transaction_category overrides locked category when ignore_attribute_locks" do+ # Explicit "Re-apply" from the rules UI passes ignore_attribute_locks: true (issue #2051)+ @txn1.lock_attr!(:category_id)++ action = Rule::Action.new(+ rule: @transaction_rule,+ action_type: "set_transaction_category",+ value: @grocery_category.id+ )++ action.apply(@rule_scope, ignore_attribute_locks: true)++ assert_equal @grocery_category.id, @txn1.reload.category_id+ end+test "set_transaction_tags" dotag = @family.tags.create!(name: "Rule test tag")@@ -146,6 +161,22 @@ class Rule::ActionTest < ActiveSupport::TestCaseendend+ test "set_transaction_name overrides locked name when ignore_attribute_locks" do+ # Explicit "Re-apply" from the rules UI passes ignore_attribute_locks: true (issue #2051)+ new_name = "Renamed Transaction"+ @txn1.entry.lock_attr!(:name)++ action = Rule::Action.new(+ rule: @transaction_rule,+ action_type: "set_transaction_name",+ value: new_name+ )++ action.apply(@rule_scope, ignore_attribute_locks: true)++ assert_equal new_name, @txn1.reload.entry.name+ end+test "set_investment_activity_label" do# Does not modify transactions that are locked (user edited them)@txn1.lock_attr!(:investment_activity_label)