1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-10 07:55:21 +02:00
Maybe/app/views/rule/conditions/_condition.html.erb
Alex Hatzenbuhler 30d3eef67f
Fix AND prefix on rule form (#2234)
* Fix AND prefix on rule form

This new condition prefix data target is used to ensure the AND prefix is added/removed to additional conditions/groups when there aren't any saved conditions yet.

* Ensure the condition group "Add condition" button is type button to avoid form submission

* Add prefix update when removing a subcondition

* Use data condition to update the prefix on conditions, condition groups, and subconditions

* Use condition remove instead of element remove for condition groups so prefix logic runs

* Add back explicit show_prefixes to ensure subconditions never have a prefix

* Run the prefix update runs on a load of a form, which handles prefixes on an edit since no conditions change

* Ensure saved items that are marked for removal don't impact the index

* Simplify logic since we don't process subconditions

* Clean up comments

* Add primary_condition_title field to rule model
2025-05-13 10:34:41 -04:00

44 lines
1.7 KiB
Text

<%# locals: (form:, show_prefix: true) %>
<% condition = form.object %>
<% rule = condition.rule %>
<li data-controller="rule--conditions" data-rule--conditions-condition-filters-value="<%= rule.condition_filters.to_json %>" class="flex items-center gap-3">
<%# Conditionally render the prefix %>
<%# Condition groups pass in show_prefix: false for subconditions since the ANY/ALL selector makes that clear %>
<% if show_prefix %>
<div class="pl-2" data-condition-prefix>
<span class="font-medium uppercase text-xs">and</span>
</div>
<% end %>
<div class="grow flex gap-2 items-center h-full">
<%= form.hidden_field :_destroy, value: false, data: { rule__conditions_target: "destroyField" } %>
<div class="w-2/5 shrink-0">
<%= form.select :condition_type, rule.condition_filters.map { |filter| [ filter.label, filter.key ] }, {}, data: { action: "rule--conditions#handleConditionTypeChange" } %>
</div>
<%= form.select :operator, condition.operators, { container_class: "w-fit min-w-36" }, data: { rule__conditions_target: "operatorSelect" } %>
<div data-rule--conditions-target="filterValue" class="grow">
<% if condition.filter.type == "select" %>
<%= form.select :value, condition.options, {} %>
<% else %>
<% if condition.filter.type == "number" %>
<%= form.number_field :value, placeholder: "10", step: 0.01 %>
<% else %>
<%= form.text_field :value, placeholder: "Enter a value" %>
<% end %>
<% end %>
</div>
</div>
<%= icon(
"trash-2",
as_button: true,
size: "sm",
data: { action: "rule--conditions#remove", rule__conditions_destroy_param: condition.persisted? }
) %>
</li>