Files
sure/app/javascript/controllers/rules_controller.js
T
Juan José MataandInstinct Agent fb01bbbae3 fix(rules): keep dynamically added nested-attribute keys numeric (#3444)
* fix(rules): keep dynamically added nested-attribute keys numeric

Rails strong params only treat integer-keyed hashes as nested attributes
(ActionController::Parameters.nested_attribute? matches /\A-?\d+\z/), so
rules_controller.js keys of the form "new_1"/"new_2" (introduced in
457698f, PR #3397) were silently dropped on submit: creating a rule from
the modal failed with "must have at least one action" even though an
action was present, and the re-rendered form lost the entered rows.

Date.now() alone can repeat within the same millisecond and a small
counter alone can collide with the numeric indexes Rails assigns to
persisted rows on edit forms, so combine them (Date.now() * 1000 + seq)
in both rules_controller.js and rule/conditions_controller.js.

Adds a system test that creates a rule through the modal with
dynamically added rows; no system test previously covered this path,
which is why the regression shipped.

* Fix rule creation regression

---------

Co-authored-by: Instinct Agent <agent@instinct.com>
2026-09-08 05:10:03 +02:00

80 lines
2.1 KiB
JavaScript

import { Controller } from "@hotwired/stimulus";
// Connects to data-controller="rules"
export default class extends Controller {
static targets = [
"conditionTemplate",
"conditionGroupTemplate",
"actionTemplate",
"conditionsList",
"actionsList",
"effectiveDateInput",
];
connect() {
// Update condition prefixes on first connection (form render on edit)
this.updateConditionPrefixes();
}
addConditionGroup() {
this.#appendTemplate(
this.conditionGroupTemplateTarget,
this.conditionsListTarget,
);
this.updateConditionPrefixes();
}
addCondition() {
this.#appendTemplate(
this.conditionTemplateTarget,
this.conditionsListTarget,
);
this.updateConditionPrefixes();
}
addAction() {
this.#appendTemplate(this.actionTemplateTarget, this.actionsListTarget);
}
clearEffectiveDate() {
this.effectiveDateInputTarget.value = "";
}
#appendTemplate(templateEl, listEl) {
const html = templateEl.innerHTML.replaceAll(
"IDX_PLACEHOLDER",
this.#uniqueKey(),
);
listEl.insertAdjacentHTML("beforeend", html);
}
#uniqueKey() {
this.keySequence = (this.keySequence ?? 0) + 1;
return Date.now() * 1000 + this.keySequence;
}
// Updates the prefix visibility of all conditions and condition groups
// This is also called by the rule/conditions_controller when a subcondition is removed
updateConditionPrefixes() {
const conditions = Array.from(this.conditionsListTarget.children);
let conditionIndex = 0;
conditions.forEach((condition) => {
// Only process visible conditions, this prevents conditions that are marked for removal and hidden
// from being added to the index. This is important when editing a rule.
if (!condition.classList.contains('hidden')) {
const prefixEl = condition.querySelector('[data-condition-prefix]');
if (prefixEl) {
if (conditionIndex === 0) {
prefixEl.classList.add('hidden');
} else {
prefixEl.classList.remove('hidden');
}
conditionIndex++;
}
}
});
}
}