Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Pre-fill rule suggestion with transaction name and category (#497)

Carlos Adames104324a82b49
diff --git a/app/controllers/rules_controller.rb b/app/controllers/rules_controller.rb
index 65e02ded10..24927b2952 100644
--- a/app/controllers/rules_controller.rb
+++ b/app/controllers/rules_controller.rb
@@ -29,6 +29,22 @@ class RulesController < ApplicationController
@rule = Current.family.rules.build(
resource_type: params[:resource_type] || "transaction",
)
+
+ if params[:name].present?
+ @rule.name = params[:name]
+ @rule.conditions.build(
+ condition_type: "transaction_name",
+ operator: "like",
+ value: params[:name]
+ )
+ end
+
+ if params[:action_type].present? && params[:action_value].present?
+ @rule.actions.build(
+ action_type: params[:action_type],
+ value: params[:action_value]
+ )
+ end
end
def create
diff --git a/app/controllers/transaction_categories_controller.rb b/app/controllers/transaction_categories_controller.rb
index dac6b5be0d..4ad75db591 100644
--- a/app/controllers/transaction_categories_controller.rb
+++ b/app/controllers/transaction_categories_controller.rb
@@ -11,7 +11,8 @@ class TransactionCategoriesController < ApplicationController
flash[:cta] = {
type: "category_rule",
category_id: transaction.category_id,
- category_name: transaction.category.name
+ category_name: transaction.category.name,
+ merchant_name: @entry.name
}
end
diff --git a/app/views/rules/_category_rule_cta.html.erb b/app/views/rules/_category_rule_cta.html.erb
index fb601b2d50..ff3481f4d8 100644
--- a/app/views/rules/_category_rule_cta.html.erb
+++ b/app/views/rules/_category_rule_cta.html.erb
@@ -14,7 +14,7 @@
<%= tag.div class:"flex gap-2 justify-end" do %>
<%= render DS::Button.new(text: "Dismiss", variant: "secondary") %>
- <% rule_href = new_rule_path(resource_type: "transaction", action_type: "set_transaction_category", action_value: cta[:category_id]) %>
+ <% rule_href = new_rule_path(resource_type: "transaction", action_type: "set_transaction_category", action_value: cta[:category_id], name: cta[:merchant_name]) %>
<%= render DS::Link.new(text: "Create rule", variant: "primary", href: rule_href, frame: :modal) %>
<% end %>
<% end %>
diff --git a/test/controllers/rules_controller_test.rb b/test/controllers/rules_controller_test.rb
index 30432520b6..a948f399d7 100644
--- a/test/controllers/rules_controller_test.rb
+++ b/test/controllers/rules_controller_test.rb
@@ -10,6 +10,23 @@ class RulesControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end
+ test "should get new with pre-filled name and action" do
+ category = categories(:food_and_drink)
+ get new_rule_url(
+ resource_type: "transaction",
+ name: "Starbucks",
+ action_type: "set_transaction_category",
+ action_value: category.id
+ )
+ assert_response :success
+
+ assert_select "input[name='rule[name]'][value='Starbucks']"
+ assert_select "input[name*='[value]'][value='Starbucks']"
+ assert_select "select[name*='[condition_type]'] option[selected][value='transaction_name']"
+ assert_select "select[name*='[action_type]'] option[selected][value='set_transaction_category']"
+ assert_select "select[name*='[value]'] option[selected][value='#{category.id}']"
+ end
+
test "should get edit" do
get edit_rule_url(rules(:one))
assert_response :success