Files
sure/app/javascript/controllers/rule/conditions_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

144 lines
4.0 KiB
JavaScript

import { Controller } from "@hotwired/stimulus";
// Connects to data-controller="rule--conditions"
export default class extends Controller {
static values = { conditionFilters: Array, valuelessOperators: Array };
static targets = [
"destroyField",
"filterValue",
"operatorSelect",
"subConditionTemplate",
"subConditionsList",
];
connect() {
// Hide value field on initial load for valueless operators (e.g. "is_null")
this.#toggleValueFieldVisibility();
}
addSubCondition() {
const html = this.subConditionTemplateTarget.innerHTML.replaceAll(
"IDX_CHILD_PLACEHOLDER",
this.#uniqueKey(),
);
this.subConditionsListTarget.insertAdjacentHTML("beforeend", html);
}
remove(e) {
e.preventDefault();
e.stopPropagation();
// Find the parent rules controller before removing the condition
const rulesEl = this.element.closest('[data-controller~="rules"]');
if (e.params.destroy) {
this.destroyFieldTarget.value = true;
this.element.classList.add("hidden");
} else {
this.element.remove();
}
// Update the prefixes of all conditions from the parent rules controller
if (rulesEl) {
const rulesController = this.application.getControllerForElementAndIdentifier(rulesEl, "rules");
if (rulesController && typeof rulesController.updateConditionPrefixes === "function") {
rulesController.updateConditionPrefixes();
}
}
}
handleConditionTypeChange(e) {
const conditionFilter = this.conditionFiltersValue.find(
(filter) => filter.key === e.target.value,
);
if (conditionFilter.type === "select") {
this.#buildSelectFor(conditionFilter);
} else {
this.#buildTextInputFor(conditionFilter);
}
this.#updateOperatorsField(conditionFilter);
this.#toggleValueFieldVisibility();
}
handleOperatorChange(e) {
this.#toggleValueFieldVisibility();
}
get valueInputEl() {
const textInput = this.filterValueTarget.querySelector("input");
const selectInput = this.filterValueTarget.querySelector("select");
return textInput || selectInput;
}
#updateOperatorsField(conditionFilter) {
this.operatorSelectTarget.innerHTML = "";
for (const operator of conditionFilter.operators) {
const optionEl = document.createElement("option");
optionEl.value = operator[1];
optionEl.textContent = operator[0];
this.operatorSelectTarget.appendChild(optionEl);
}
}
#buildSelectFor(conditionFilter) {
const selectEl = this.#convertFormFieldTo("select", this.valueInputEl);
for (const option of conditionFilter.options) {
const optionEl = document.createElement("option");
optionEl.value = option[1];
optionEl.textContent = option[0];
selectEl.appendChild(optionEl);
}
this.valueInputEl.replaceWith(selectEl);
}
#buildTextInputFor(conditionFilter) {
const textInput = this.#convertFormFieldTo("input", this.valueInputEl);
textInput.placeholder = "Enter a value";
textInput.type = conditionFilter.type; // "text" || "number"
if (conditionFilter.type === "number") {
textInput.step = conditionFilter.number_step;
}
this.valueInputEl.replaceWith(textInput);
}
#convertFormFieldTo(type, el) {
const priorClasses = el.classList;
const priorId = el.id;
const priorName = el.name;
const newFormField = document.createElement(type);
newFormField.classList.add(...priorClasses);
newFormField.id = priorId;
newFormField.name = priorName;
return newFormField;
}
#uniqueKey() {
this.keySequence = (this.keySequence ?? 0) + 1;
return Date.now() * 1000 + this.keySequence;
}
#toggleValueFieldVisibility() {
const operator = this.operatorSelectTarget.value;
if (this.valuelessOperatorsValue.includes(operator)) {
this.filterValueTarget.classList.add("hidden");
// Clear the value since it's not needed
if (this.valueInputEl) {
this.valueInputEl.value = "";
}
} else {
this.filterValueTarget.classList.remove("hidden");
}
}
}