1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-09 15:35:22 +02:00

Merge branch 'main' of github.com:maybe-finance/maybe into zachgoll/plaid-domain-improvements

This commit is contained in:
Zach Gollwitzer 2025-05-13 15:54:38 -04:00
commit 7ad3ea8223
17 changed files with 242 additions and 91 deletions

View file

@ -78,6 +78,14 @@
}
}
@utility bg-divider {
@apply bg-alpha-black-100;
@variant theme-dark {
@apply bg-alpha-white-100;
}
}
@utility bg-overlay {
background-color: --alpha(var(--color-gray-100) / 50%);
@ -88,4 +96,4 @@
@utility bg-loader {
@apply bg-surface-inset animate-pulse;
}
}

View file

@ -4,7 +4,14 @@ class RulesController < ApplicationController
before_action :set_rule, only: [ :edit, :update, :destroy, :apply, :confirm ]
def index
@rules = Current.family.rules.order(created_at: :desc)
@sort_by = params[:sort_by] || "name"
@direction = params[:direction] || "asc"
allowed_columns = [ "name", "updated_at" ]
@sort_by = "name" unless allowed_columns.include?(@sort_by)
@direction = "asc" unless [ "asc", "desc" ].include?(@direction)
@rules = Current.family.rules.order(@sort_by => @direction)
render layout: "settings"
end
@ -64,7 +71,7 @@ class RulesController < ApplicationController
def rule_params
params.require(:rule).permit(
:resource_type, :effective_date, :active,
:resource_type, :effective_date, :active, :name,
conditions_attributes: [
:id, :condition_type, :operator, :value, :_destroy,
sub_conditions_attributes: [ :id, :condition_type, :operator, :value, :_destroy ]

View file

@ -21,12 +21,23 @@ export default class extends Controller {
}
remove(e) {
// 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) {

View file

@ -11,11 +11,17 @@ export default class extends Controller {
"effectiveDateInput",
];
connect() {
// Update condition prefixes on first connection (form render on edit)
this.updateConditionPrefixes();
}
addConditionGroup() {
this.#appendTemplate(
this.conditionGroupTemplateTarget,
this.conditionsListTarget,
);
this.updateConditionPrefixes();
}
addCondition() {
@ -23,6 +29,7 @@ export default class extends Controller {
this.conditionTemplateTarget,
this.conditionsListTarget,
);
this.updateConditionPrefixes();
}
addAction() {
@ -45,4 +52,27 @@ export default class extends Controller {
#uniqueKey() {
return Date.now();
}
// 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++;
}
}
});
}
}

View file

@ -8,7 +8,10 @@ class Rule < ApplicationRecord
accepts_nested_attributes_for :conditions, allow_destroy: true
accepts_nested_attributes_for :actions, allow_destroy: true
before_validation :normalize_name
validates :resource_type, presence: true
validates :name, length: { minimum: 1 }, allow_nil: true
validate :no_nested_compound_conditions
# Every rule must have at least 1 action
@ -46,6 +49,18 @@ class Rule < ApplicationRecord
RuleJob.perform_later(self, ignore_attribute_locks: ignore_attribute_locks)
end
def primary_condition_title
return "No conditions" if conditions.none?
first_condition = conditions.first
if first_condition.compound? && first_condition.sub_conditions.any?
first_sub_condition = first_condition.sub_conditions.first
"If #{first_sub_condition.filter.label.downcase} #{first_sub_condition.operator} #{first_sub_condition.value_display}"
else
"If #{first_condition.filter.label.downcase} #{first_condition.operator} #{first_condition.value_display}"
end
end
private
def matching_resources_scope
scope = registry.resource_scope
@ -87,4 +102,8 @@ class Rule < ApplicationRecord
end
end
end
def normalize_name
self.name = nil if name.is_a?(String) && name.strip.empty?
end
end

View file

@ -1,5 +1,5 @@
class Rule::Action < ApplicationRecord
belongs_to :rule
belongs_to :rule, touch: true
validates :action_type, presence: true

View file

@ -1,5 +1,5 @@
class Rule::Condition < ApplicationRecord
belongs_to :rule, optional: -> { where.not(parent_id: nil) }
belongs_to :rule, touch: true, optional: -> { where.not(parent_id: nil) }
belongs_to :parent, class_name: "Rule::Condition", optional: true, inverse_of: :sub_conditions
has_many :sub_conditions, class_name: "Rule::Condition", foreign_key: :parent_id, dependent: :destroy, inverse_of: :parent

View file

@ -16,7 +16,7 @@
<%# Initial rendering based on rule.action_executors.first from the rule form. %>
<%# This is currently always SetTransactionCategory from transaction_resource.rb, which is a select type. %>
<%# Subsequent renders are injected by the Stimulus controller, which uses the templates from below. %>
<span class="font-medium uppercase text-xs">to</span>
<span class="font-medium text-primary uppercase text-xs">to</span>
<%= form.select :value, action.options || [], {} %>
<% end %>
</div>
@ -29,12 +29,12 @@
<%# Templates for different input types - these will be cloned and used by the Stimulus controller %>
<template data-rule--actions-target="selectTemplate">
<span class="font-medium uppercase text-xs">to</span>
<span class="font-medium text-primary uppercase text-xs">to</span>
<%= form.select :value, [], {} %>
</template>
<template data-rule--actions-target="textTemplate">
<span class="font-medium uppercase text-xs">to</span>
<span class="font-medium text-primary uppercase text-xs">to</span>
<%= form.text_field :value, placeholder: "Enter a value" %>
</template>

View file

@ -4,8 +4,11 @@
<% 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">
<% if form.index.to_i > 0 && show_prefix %>
<div class="pl-4">
<%# 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 %>

View file

@ -3,17 +3,16 @@
<% condition = form.object %>
<% rule = condition.rule %>
<li data-controller="rule--conditions element-removal" class="border border-secondary rounded-md p-4 space-y-3">
<li data-controller="rule--conditions" class="border border-secondary rounded-md p-4 space-y-3">
<%= form.hidden_field :condition_type, value: "compound" %>
<div class="flex items-center justify-between gap-2">
<div class="flex items-center gap-2">
<% unless form.index == 0 %>
<div class="pl-2">
<span class="font-medium uppercase text-xs">and</span>
</div>
<% end %>
<%# Show prefix on condition groups, except the first one %>
<div class="pl-2" data-condition-prefix>
<span class="font-medium uppercase text-xs">and</span>
</div>
<p class="text-sm text-secondary">match</p>
<%= form.select :operator, [["all", "and"], ["any", "or"]], { container_class: "w-fit" }, data: { rules_target: "operatorField" } %>
<p class="text-sm text-secondary">of the following conditions</p>
@ -21,16 +20,16 @@
<%= icon(
"trash-2",
size: "sm",
as_button: true,
data: { action: "element-removal#remove" }
size: "sm",
data: { action: "rule--conditions#remove" }
) %>
</div>
<%# Sub-condition template, used by Stimulus controller to add new sub-conditions dynamically %>
<template data-rule--conditions-target="subConditionTemplate">
<%= form.fields_for :sub_conditions, Rule::Condition.new(parent: condition, condition_type: rule.condition_filters.first.key), child_index: "IDX_PLACEHOLDER" do |scf| %>
<%= render "rule/conditions/condition", form: scf %>
<%= render "rule/conditions/condition", form: scf, show_prefix: false %>
<% end %>
</template>
@ -44,6 +43,7 @@
text: "Add condition",
leading_icon: "plus",
variant: "ghost",
type: "button",
data: { action: "rule--conditions#addSubCondition" }
) %>
</li>

View file

@ -1,6 +1,6 @@
<%# locals: (rule:) %>
<%= styled_form_with model: rule, class: "space-y-4",
<%= styled_form_with model: rule, class: "space-y-6",
data: { controller: "rules", rule_registry_value: rule.registry.to_json } do |f| %>
<%= f.hidden_field :resource_type, value: rule.resource_type %>
@ -10,9 +10,21 @@
<% end %>
<section class="space-y-4">
<h3 class="text-sm font-medium text-primary">If <%= rule.resource_type %></h3>
<div class="flex items-center gap-1 text-secondary">
<%= icon "tag", size: "sm" %>
<h3 class="text-sm font-medium text-primary">Rule name (optional)</h3>
</div>
<div class="ml-6">
<%= f.text_field :name, placeholder: "Enter a name for this rule", class: "form-field__input" %>
</div>
</section>
<%# Condition template, used by Stimulus controller to add new conditions dynamically %>
<section class="space-y-4">
<div class="flex items-center gap-1 text-secondary">
<h3 class="text-sm font-medium text-primary">IF</h3>
</div>
<%# Condition Group template, used by Stimulus controller to add new conditions dynamically %>
<template data-rules-target="conditionGroupTemplate">
<%= f.fields_for :conditions, Rule::Condition.new(rule: rule, condition_type: "compound", operator: "and"), child_index: "IDX_PLACEHOLDER" do |cf| %>
<%= render "rule/conditions/condition_group", form: cf %>
@ -26,24 +38,27 @@
<% end %>
</template>
<ul data-rules-target="conditionsList" class="space-y-3 mb-4">
<%= f.fields_for :conditions do |cf| %>
<% if cf.object.compound? %>
<%= render "rule/conditions/condition_group", form: cf %>
<% else %>
<%= render "rule/conditions/condition", form: cf %>
<div class="ml-6 space-y-4">
<ul data-rules-target="conditionsList" class="space-y-3">
<%= f.fields_for :conditions do |cf| %>
<% if cf.object.compound? %>
<%= render "rule/conditions/condition_group", form: cf %>
<% else %>
<%= render "rule/conditions/condition", form: cf %>
<% end %>
<% end %>
<% end %>
</ul>
</ul>
<div class="flex items-center gap-2">
<%= render ButtonComponent.new(text: "Add condition", icon: "plus", variant: "ghost", type: "button", data: { action: "rules#addCondition" }) %>
<%= render ButtonComponent.new(text: "Add condition group", icon: "boxes", variant: "ghost", type: "button", data: { action: "rules#addConditionGroup" }) %>
<%= render ButtonComponent.new(text: "Add condition group", icon: "copy-plus", variant: "ghost", type: "button", data: { action: "rules#addConditionGroup" }) %>
</div>
</section>
<section class="space-y-4">
<h3 class="text-sm font-medium text-primary">Then</h3>
<div class="flex items-center gap-1 text-secondary">
<h3 class="text-sm font-medium text-primary">THEN</h3>
</div>
<%# Action template, used by Stimulus controller to add new actions dynamically %>
<template data-rules-target="actionTemplate">
@ -52,22 +67,25 @@
<% end %>
</template>
<ul data-rules-target="actionsList" class="space-y-3">
<%= f.fields_for :actions do |af| %>
<%= render "rule/actions/action", form: af %>
<% end %>
</ul>
<div class="ml-6 space-y-4">
<ul data-rules-target="actionsList" class="space-y-3">
<%= f.fields_for :actions do |af| %>
<%= render "rule/actions/action", form: af %>
<% end %>
</ul>
<%= render ButtonComponent.new(text: "Add action", icon: "plus", variant: "ghost", type: "button", data: { action: "rules#addAction" }) %>
</section>
<section class="space-y-4">
<h3 class="text-sm font-medium text-primary">Apply this</h3>
<div class="flex items-center gap-1 text-secondary">
<h3 class="text-sm font-medium text-primary">FOR</h3>
</div>
<div class="space-y-2">
<div class="ml-6 space-y-3">
<div class="flex items-center gap-2">
<%= f.radio_button :effective_date_enabled, false, checked: rule.effective_date.nil?, data: { action: "rules#clearEffectiveDate" } %>
<%= f.label :effective_date_enabled_false, "To all past and future #{rule.resource_type}s", class: "text-sm text-primary" %>
<%= f.label :effective_date_enabled_false, "All past and future #{rule.resource_type}s", class: "text-sm text-primary" %>
</div>
<div class="flex items-center gap-2">

View file

@ -1,51 +1,64 @@
<%# locals: (rule:) %>
<div class="flex justify-between items-center gap-4 bg-white shadow-border-xs rounded-md p-4">
<div class="flex justify-between items-center p-4 <%= rule.active? ? 'text-primary' : 'text-secondary' %>">
<div class="text-sm space-y-1.5">
<% if rule.name.present? %>
<h3 class="font-medium text-md"><%= rule.name %></h3>
<% end %>
<% if rule.conditions.any? %>
<p class="flex items-center flex-wrap gap-1.5">
<span class="px-2 py-1 border border-alpha-black-200 rounded-full">
<% if rule.conditions.first.compound? %>
If <%= rule.conditions.first.sub_conditions.first.filter.label %> <%= rule.conditions.first.sub_conditions.first.operator %> <%= rule.conditions.first.sub_conditions.first.value_display %>
<div class="flex items-center gap-2 mt-1">
<div class="flex items-center gap-1 text-secondary w-16 shrink-0">
<span class="font-mono text-xs">IF</span>
</div>
<p class="flex items-center flex-wrap gap-1.5 m-0">
<span class="px-2 py-1 border border-secondary rounded-full">
<% if rule.conditions.first.compound? %>
<%= rule.conditions.first.sub_conditions.first.filter.label %> <%= rule.conditions.first.sub_conditions.first.operator %> <%= rule.conditions.first.sub_conditions.first.value_display %>
<% else %>
<%= rule.conditions.first.filter.label %> <%= rule.conditions.first.operator %> <%= rule.conditions.first.value_display %>
<% end %>
</span>
<% if rule.conditions.count > 1 %>
and <%= rule.conditions.count - 1 %> more <%= rule.conditions.count - 1 == 1 ? "condition" : "conditions" %>
<% end %>
</p>
</div>
<% end %>
<div class="flex items-center gap-2 mt-1">
<div class="flex items-center gap-1 text-secondary w-16 shrink-0">
<span class="font-mono text-xs">THEN</span>
</div>
<p class="flex items-center flex-wrap gap-1.5 m-0">
<span class="px-2 py-1 border border-secondary rounded-full">
<% if rule.actions.first.value && rule.actions.first.options %>
<%= rule.actions.first.executor.label %> to <%= rule.actions.first.value_display %>
<% else %>
If <%= rule.conditions.first.filter.label %> <%= rule.conditions.first.operator %> <%= rule.conditions.first.value_display %>
<%= rule.actions.first.executor.label %>
<% end %>
</span>
<% if rule.conditions.count > 1 %>
and <%= rule.conditions.count - 1 %> more <%= rule.conditions.count - 1 == 1 ? "condition" : "conditions" %>
<% if rule.actions.count > 1 %>
and <%= rule.actions.count - 1 %> more <%= rule.actions.count - 1 == 1 ? "action" : "actions" %>
<% end %>
</p>
<% end %>
<p class="flex items-center flex-wrap gap-1.5">
<span class="px-2 py-1 border border-alpha-black-200 rounded-full">
<% if rule.actions.first.value && rule.actions.first.options %>
<%= rule.actions.first.executor.label %> to <%= rule.actions.first.value_display %>
<% else %>
<%= rule.actions.first.executor.label %>
<% end %>
</span>
<% if rule.actions.count > 1 %>
and <%= rule.actions.count - 1 %> more <%= rule.actions.count - 1 == 1 ? "action" : "actions" %>
<% end %>
</p>
<p class="flex items-center flex-wrap gap-1.5">
<% if rule.effective_date.nil? %>
To all past and future <%= rule.resource_type.pluralize %>
<% else %>
To all <%= rule.resource_type.pluralize %> on or after <%= rule.effective_date %>
<% end %>
</p>
</div>
<div class="flex items-center gap-2 mt-1">
<div class="flex items-center gap-1 text-secondary w-16 shrink-0">
<span class="font-mono text-xs">FOR</span>
</div>
<p class="flex items-center flex-wrap gap-1.5 m-0">
<span class="px-2 py-1 border border-secondary rounded-full">
<% if rule.effective_date.nil? %>
All past and future <%= rule.resource_type.pluralize %>
<% else %>
<%= rule.resource_type.pluralize %> on or after <%= rule.effective_date.strftime('%b %-d, %Y') %>
<% end %>
</span>
</p>
</div>
</div>
<div class="flex items-center gap-4">
<%= styled_form_with model: rule, data: { controller: "auto-submit-form" } do |f| %>
<%= f.toggle :active, { data: { auto_submit_form_target: "auto" } } %>
<% end %>
<%= render MenuComponent.new do |menu| %>
<% menu.with_item(variant: "link", text: "Edit", href: edit_rule_path(rule), icon: "pencil", data: { turbo_frame: "modal" }) %>
<% menu.with_item(variant: "link", text: "Re-apply rule", href: confirm_rule_path(rule), icon: "refresh-cw", data: { turbo_frame: "modal" }) %>

View file

@ -1,6 +1,13 @@
<%= render DialogComponent.new(reload_on_close: true) do |dialog| %>
<% dialog.with_header(title: "Confirm changes") %>
<%
title = if @rule.name.present?
"Confirm changes to \"#{@rule.name}\""
else
"Confirm changes"
end
%>
<% dialog.with_header(title: title) %>
<% dialog.with_body do %>
<p class="text-secondary text-sm mb-4">
You are about to apply this rule to

View file

@ -1,7 +1,15 @@
<%= link_to "Back to rules", rules_path %>
<%= render DialogComponent.new do |dialog| %>
<% dialog.with_header(title: "Edit #{@rule.resource_type} rule") %>
<%
title = if @rule.name.present?
"Edit #{@rule.resource_type} rule \"#{@rule.name}\""
else
"Edit #{@rule.resource_type} rule"
end
%>
<% dialog.with_header(title: title) %>
<% dialog.with_body do %>
<%= render "rules/form", rule: @rule %>
<% end %>

View file

@ -1,6 +1,5 @@
<header class="flex items-center justify-between">
<h1 class="text-primary text-xl font-medium">Rules</h1>
<div class="flex items-center gap-2">
<% if @rules.any? %>
<%= render MenuComponent.new do |menu| %>
@ -13,7 +12,6 @@
confirm: CustomConfirm.for_resource_deletion("all rules", high_severity: true)) %>
<% end %>
<% end %>
<%= render LinkComponent.new(
text: "New rule",
variant: "primary",
@ -23,7 +21,6 @@
) %>
</div>
</header>
<% if self_hosted? %>
<div class="flex items-center gap-2 mb-2 py-4">
<%= icon("circle-alert", size: "sm") %>
@ -32,19 +29,43 @@
</p>
</div>
<% end %>
<div class="bg-container shadow-border-xs rounded-xl p-4">
<% if @rules.any? %>
<div class="rounded-xl bg-gray-25 space-y-1">
<div class="flex items-center gap-1.5 px-4 py-2 text-xs font-medium text-secondary uppercase">
<p>Rules</p>
<span class="text-subdued">&middot;</span>
<p><%= @rules.count %></p>
<div class="bg-container-inset rounded-xl">
<div class="flex justify-between px-4 py-2 text-xs uppercase">
<div class="flex items-center gap-1.5 font-medium text-secondary">
<p>Rules</p>
<span class="text-subdued">&middot;</span>
<p><%= @rules.count %></p>
</div>
<div class="flex items-center gap-1">
<span class="text-secondary">Sort by:</span>
<%= form_with url: rules_path, method: :get, local: true, class: "flex items-center", data: { controller: "auto-submit-form" } do |form| %>
<%= form.select :sort_by,
options_for_select([["Name", "name"], ["Updated At", "updated_at"]], @sort_by),
{},
class: "min-w-[120px] bg-transparent rounded border-none cursor-pointer text-primary uppercase text-xs w-auto",
data: { auto_submit_form_target: "auto", autosubmit_trigger_event: "change" } %>
<%= form.hidden_field :direction, value: @direction %>
<% end %>
<%= render LinkComponent.new(
href: rules_path(direction: @direction == "asc" ? "desc" : "asc", sort_by: @sort_by),
variant: "icon",
icon: "arrow-up-down",
size: :sm,
title: "Toggle sort direction"
) %>
</div>
</div>
<div class="space-y-1 p-1">
<%= render @rules %>
<div class="p-1">
<div class="flex flex-col bg-container rounded-xl shadow-border-xs first_child:rounded-t-xl last_child:rounded-b-xl">
<% @rules.each_with_index do |rule, idx| %>
<%= render "rule", rule: rule%>
<% unless idx == @rules.size - 1 %>
<div class="h-px bg-divider ml-4 mr-6"></div>
<% end %>
<% end %>
</div>
</div>
</div>
<% else %>

View file

@ -0,0 +1,5 @@
class AddNameToRules < ActiveRecord::Migration[7.2]
def change
add_column :rules, :name, :string
end
end

1
db/schema.rb generated
View file

@ -498,6 +498,7 @@ ActiveRecord::Schema[7.2].define(version: 2025_05_13_122703) do
t.boolean "active", default: false, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.index ["family_id"], name: "index_rules_on_family_id"
end