Skip to content

armgit / maybe

Public Imported from GitHub
0

The personal finance app for everyone

Add rule option to change transaction name (#2175)

Alex Hatzenbuhler60c3a04a4883
diff --git a/app/javascript/controllers/rule/actions_controller.js b/app/javascript/controllers/rule/actions_controller.js
index f4de40eb..0ca8a252 100644
--- a/app/javascript/controllers/rule/actions_controller.js
+++ b/app/javascript/controllers/rule/actions_controller.js
@@ -3,7 +3,12 @@ import { Controller } from "@hotwired/stimulus";
// Connects to data-controller="rule--actions"
export default class extends Controller {
static values = { actionExecutors: Array };
- static targets = ["destroyField", "actionValue"];
+ static targets = [
+ "destroyField",
+ "actionValue",
+ "selectTemplate",
+ "textTemplate"
+ ];
remove(e) {
if (e.params.destroy) {
@@ -19,38 +24,67 @@ export default class extends Controller {
(executor) => executor.key === e.target.value,
);
+ // Clear any existing input elements first
+ this.#clearFormFields();
+
if (actionExecutor.type === "select") {
- this.#updateValueSelectFor(actionExecutor);
- this.#showAndEnableValueSelect();
+ this.#buildSelectFor(actionExecutor);
+ } else if (actionExecutor.type === "text") {
+ this.#buildTextInputFor();
} else {
- this.#hideAndDisableValueSelect();
+ // Hide for any type that doesn't need a value (e.g. function)
+ this.#hideActionValue();
}
}
- get valueSelectEl() {
- return this.actionValueTarget.querySelector("select");
+ #hideActionValue() {
+ this.actionValueTarget.classList.add("hidden");
}
- #showAndEnableValueSelect() {
- this.actionValueTarget.classList.remove("hidden");
- this.valueSelectEl.disabled = false;
+ #clearFormFields() {
+ // Remove all children from actionValueTarget
+ this.actionValueTarget.innerHTML = "";
}
- #hideAndDisableValueSelect() {
- this.actionValueTarget.classList.add("hidden");
- this.valueSelectEl.disabled = true;
+ #buildSelectFor(actionExecutor) {
+ // Clone the select template
+ const template = this.selectTemplateTarget.content.cloneNode(true);
+ const selectEl = template.querySelector("select");
+
+ // Add options to the select element
+ if (selectEl) {
+ selectEl.innerHTML = "";
+ if (!actionExecutor.options || actionExecutor.options.length === 0) {
+ selectEl.disabled = true;
+ const optionEl = document.createElement("option");
+ optionEl.textContent = "(none)";
+ selectEl.appendChild(optionEl);
+ } else {
+ selectEl.disabled = false;
+ for (const option of actionExecutor.options) {
+ const optionEl = document.createElement("option");
+ optionEl.value = option[1];
+ optionEl.textContent = option[0];
+ selectEl.appendChild(optionEl);
+ }
+ }
+ }
+
+ // Add the template content to the actionValue target and ensure it's visible
+ this.actionValueTarget.appendChild(template);
+ this.actionValueTarget.classList.remove("hidden");
}
- #updateValueSelectFor(actionExecutor) {
- // Clear existing options
- this.valueSelectEl.innerHTML = "";
+ #buildTextInputFor() {
+ // Clone the text template
+ const template = this.textTemplateTarget.content.cloneNode(true);
- // Add new options
- for (const option of actionExecutor.options) {
- const optionEl = document.createElement("option");
- optionEl.value = option[1];
- optionEl.textContent = option[0];
- this.valueSelectEl.appendChild(optionEl);
- }
+ // Ensure the input is always empty
+ const inputEl = template.querySelector("input");
+ if (inputEl) inputEl.value = "";
+
+ // Add the template content to the actionValue target and ensure it's visible
+ this.actionValueTarget.appendChild(template);
+ this.actionValueTarget.classList.remove("hidden");
}
}
diff --git a/app/models/rule/action_executor.rb b/app/models/rule/action_executor.rb
index 807e6cea..4a9a1185 100644
--- a/app/models/rule/action_executor.rb
+++ b/app/models/rule/action_executor.rb
@@ -1,5 +1,5 @@
class Rule::ActionExecutor
- TYPES = [ "select", "function" ]
+ TYPES = [ "select", "function", "text" ]
def initialize(rule)
@rule = rule
diff --git a/app/models/rule/action_executor/set_transaction_name.rb b/app/models/rule/action_executor/set_transaction_name.rb
new file mode 100644
index 00000000..39f3ee26
--- /dev/null
+++ b/app/models/rule/action_executor/set_transaction_name.rb
@@ -0,0 +1,29 @@
+class Rule::ActionExecutor::SetTransactionName < Rule::ActionExecutor
+ def type
+ "text"
+ end
+
+ def options
+ nil
+ end
+
+ def execute(transaction_scope, value: nil, ignore_attribute_locks: false)
+ return if value.blank?
+
+ scope = transaction_scope
+ unless ignore_attribute_locks
+ scope = scope.enrichable(:name)
+ end
+
+ scope.each do |txn|
+ Rule.transaction do
+ txn.entry.log_enrichment!(
+ attribute_name: "name",
+ attribute_value: value,
+ source: "rule"
+ )
+ txn.entry.update!(name: value)
+ end
+ end
+ end
+end
diff --git a/app/models/rule/registry/transaction_resource.rb b/app/models/rule/registry/transaction_resource.rb
index 628d8cde..15aae288 100644
--- a/app/models/rule/registry/transaction_resource.rb
+++ b/app/models/rule/registry/transaction_resource.rb
@@ -15,7 +15,8 @@ class Rule::Registry::TransactionResource < Rule::Registry
enabled_executors = [
Rule::ActionExecutor::SetTransactionCategory.new(rule),
Rule::ActionExecutor::SetTransactionTags.new(rule),
- Rule::ActionExecutor::SetTransactionMerchant.new(rule)
+ Rule::ActionExecutor::SetTransactionMerchant.new(rule),
+ Rule::ActionExecutor::SetTransactionName.new(rule)
]
if ai_enabled?
diff --git a/app/views/rule/actions/_action.html.erb b/app/views/rule/actions/_action.html.erb
index f41e4857..ed126cfd 100644
--- a/app/views/rule/actions/_action.html.erb
+++ b/app/views/rule/actions/_action.html.erb
@@ -2,7 +2,6 @@
<% action = form.object %>
<% rule = action.rule %>
-<% needs_value = action.executor.type == "select" %>
<li data-controller="rule--actions" data-rule--actions-action-executors-value="<%= rule.action_executors.to_json %>" class="flex items-center gap-3">
<%= form.hidden_field :_destroy, value: false, data: { rule__actions_target: "destroyField" } %>
@@ -12,10 +11,13 @@
<%= form.select :action_type, rule.action_executors.map { |executor| [ executor.label, executor.key ] }, {}, data: { action: "rule--actions#handleActionTypeChange" } %>
</div>
- <%= tag.div class: class_names("min-w-1/2 flex items-center gap-2", "hidden" => !needs_value),
+ <%= tag.div class: class_names("min-w-1/2 flex items-center gap-2"),
data: { rule__actions_target: "actionValue" } do %>
+ <%# 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>
- <%= form.select :value, action.options || [], {}, disabled: !needs_value %>
+ <%= form.select :value, action.options || [], {} %>
<% end %>
</div>
@@ -24,4 +26,17 @@
size: "sm",
as_button: true,
data: { action: "rule--actions#remove", rule__actions_destroy_param: action.persisted? }) %>
+
+ <%# 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>
+ <%= form.select :value, [], {} %>
+ </template>
+
+ <template data-rule--actions-target="textTemplate">
+ <span class="font-medium uppercase text-xs">to</span>
+ <%= form.text_field :value, placeholder: "Enter a value" %>
+ </template>
+
+ <%# The function type doesn't need an input, so no template is required.%>
</li>
diff --git a/test/models/rule/action_test.rb b/test/models/rule/action_test.rb
index 61ee6425..624849fc 100644
--- a/test/models/rule/action_test.rb
+++ b/test/models/rule/action_test.rb
@@ -79,4 +79,25 @@ class Rule::ActionTest < ActiveSupport::TestCase
assert_equal merchant.id, transaction.reload.merchant_id
end
end
+
+ test "set_transaction_name" do
+ new_name = "Renamed Transaction"
+
+ # Does not modify transactions that are locked (user edited them)
+ @txn1.lock!(:name)
+
+ action = Rule::Action.new(
+ rule: @transaction_rule,
+ action_type: "set_transaction_name",
+ value: new_name
+ )
+
+ action.apply(@rule_scope)
+
+ assert_not_equal new_name, @txn1.reload.entry.name
+
+ [ @txn2, @txn3 ].each do |transaction|
+ assert_equal new_name, transaction.reload.entry.name
+ end
+ end
end