mirror of
https://github.com/we-promise/sure.git
synced 2026-09-06 15:14:19 +00:00
* feat(rules): add not-equal, does-not-contain, is-not-empty condition operators Extend transaction rule conditions beyond "equal to" / "is empty": - text: add "does not contain" (not_like), "not equal to" (!=), "is not empty" (is_not_null) - number: add "not equal to" (!=) - select: add "not equal to" (!=), "is not empty" (is_not_null) NULL handling is inclusive so the operators match user intent: - "!=" uses IS DISTINCT FROM, so e.g. "category not equal to X" also matches uncategorized (NULL) transactions - "does not contain" also matches rows where the field is NULL transaction_type keeps its custom operator set, and transaction_details is pinned to the original operators since its JSONB apply only supports contains/equals/empty semantics. The conditions Stimulus controller hides the value field for both valueless operators (is_null and is_not_null). * refactor(rules): address PR review feedback on condition operators - Pass VALUELESS_OPERATORS from Ruby to JS via Stimulus value attribute instead of duplicating the list as a static class property, so there is a single source of truth for which operators suppress the value field - Clarify IS DISTINCT FROM comment to note the NULL-inclusion behaviour is intentional for select-type fields (merchant_id, category_id) and not applicable to number fields where NULL is impossible at the DB level - Add test that exercises the OR IS NULL branch of not_like by using transaction_notes (entries.notes is nullable, unlike entries.name) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * refactor(rules): localize condition operator labels via i18n Moves all Rule::ConditionFilter operator labels (including ones that predate this PR) out of OPERATORS_MAP and into config/locales, so operators() resolves them through t() per request instead of hardcoded English strings. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GvxjdTgH34cPoJenQAqnpN --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
143 lines
4.0 KiB
JavaScript
143 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() {
|
|
return Date.now();
|
|
}
|
|
|
|
#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");
|
|
}
|
|
}
|
|
}
|