The personal finance app for everyone (by everyone)
Ruby
fix(rules): keep dynamically added nested-attribute keys numeric (#3444)
Juan José Matafb01bbbae3a0
diff --git a/app/javascript/controllers/rule/conditions_controller.js b/app/javascript/controllers/rule/conditions_controller.jsindex ad23c862ce..766c40e05d 100644--- a/app/javascript/controllers/rule/conditions_controller.js+++ b/app/javascript/controllers/rule/conditions_controller.js@@ -123,7 +123,8 @@ export default class extends Controller {}#uniqueKey() {- return Date.now();+ this.keySequence = (this.keySequence ?? 0) + 1;+ return Date.now() * 1000 + this.keySequence;}#toggleValueFieldVisibility() {diff --git a/app/javascript/controllers/rules_controller.js b/app/javascript/controllers/rules_controller.jsindex 5466a0a622..77c40d4b9c 100644--- a/app/javascript/controllers/rules_controller.js+++ b/app/javascript/controllers/rules_controller.js@@ -50,12 +50,8 @@ export default class extends Controller {}#uniqueKey() {- // Prefixed so it can never collide with the numeric indexes Rails- // assigns to already-persisted conditions/actions when rendering an- // edit form (0, 1, 2, ...). A plain monotonic counter starting at 1- // would otherwise reuse index 1 and clobber an existing nested record.this.keySequence = (this.keySequence ?? 0) + 1;- return `new_${this.keySequence}`;+ return Date.now() * 1000 + this.keySequence;}// Updates the prefix visibility of all conditions and condition groupsdiff --git a/test/system/rules_test.rb b/test/system/rules_test.rbindex 6de9612844..2e54c814b1 100644--- a/test/system/rules_test.rb+++ b/test/system/rules_test.rb@@ -56,4 +56,26 @@ class RulesTest < ApplicationSystemTestCaseassert_selector "h3", text: "Legacy bad rule"assert_text "Nicht unterstützt (name)"end++ test "creates a transaction rule through the modal with dynamically added condition and action" do+ visit new_rule_path(resource_type: "transaction")++ within "dialog" do+ click_on "Add condition"+ find("[data-rules-target='conditionsList'] input[name$='[value]']").fill_in(with: "Coffee")+ click_on "Add action"+ click_on "Create Rule"+ end++ # A successful create lands on the confirmation dialog; before the+ # nested-attribute keys were numeric, the submit failed validation with+ # "must have at least one action" because strong params dropped the rows.+ assert_text "Confirm changes"++ rule = Rule.order(:created_at).last+ assert_equal "transaction_name", rule.conditions.first.condition_type+ assert_equal "Coffee", rule.conditions.first.value+ assert_equal "set_transaction_category", rule.actions.first.action_type+ assert rule.actions.first.value.present?+ endend