Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Load the merchant filter list when its tab opens (#4676)

Nikolay Kopylov8ef4aa179034
diff --git a/app/controllers/transactions/merchant_filters_controller.rb b/app/controllers/transactions/merchant_filters_controller.rb
new file mode 100644
index 0000000000..54da52d878
--- /dev/null
+++ b/app/controllers/transactions/merchant_filters_controller.rb
@@ -0,0 +1,6 @@
+class Transactions::MerchantFiltersController < ApplicationController
+ def show
+ @selected = Array(params[:merchants])
+ @merchants = [ Merchant.no_merchant ] + Current.family.assigned_merchants_for(Current.user).with_attached_logo.alphabetically
+ end
+end
diff --git a/app/views/transactions/merchant_filters/show.html.erb b/app/views/transactions/merchant_filters/show.html.erb
new file mode 100644
index 0000000000..52bf462238
--- /dev/null
+++ b/app/views/transactions/merchant_filters/show.html.erb
@@ -0,0 +1,34 @@
+<%= turbo_frame_tag "merchant_filter_options" do %>
+ <%= fields :q do |form| %>
+ <% @merchants.each do |merchant| %>
+ <div class="filterable-item flex items-center gap-2 p-2" data-filter-name="<%= merchant.name %>">
+ <%= form.check_box :merchants,
+ {
+ multiple: true,
+ checked: @selected.include?(merchant.filter_value),
+ class: "checkbox checkbox--light"
+ },
+ merchant.filter_value,
+ nil %>
+ <%= form.label :merchants, value: merchant.filter_value, class: "text-sm text-primary flex items-center gap-2" do %>
+ <div class="hidden lg:flex">
+ <% if merchant.logo_url.present? %>
+ <%= image_tag Setting.transform_brand_fetch_url(merchant.logo_url),
+ class: "w-6 h-6 rounded-full border border-secondary",
+ loading: "lazy" %>
+ <% else %>
+ <%= render DS::FilledIcon.new(
+ variant: :text,
+ hex_color: merchant.color,
+ text: merchant.name,
+ size: "sm",
+ rounded: true
+ ) %>
+ <% end %>
+ </div>
+ <%= merchant.name %>
+ <% end %>
+ </div>
+ <% end %>
+ <% end %>
+<% end %>
diff --git a/app/views/transactions/searches/filters/_merchant_filter.html.erb b/app/views/transactions/searches/filters/_merchant_filter.html.erb
index ad143bb4ab..306dca603b 100644
--- a/app/views/transactions/searches/filters/_merchant_filter.html.erb
+++ b/app/views/transactions/searches/filters/_merchant_filter.html.erb
@@ -1,4 +1,5 @@
<%# locals: (form:) %>
+<%# The list loads when the tab opens; until then hidden inputs keep the selected merchants in the form. %>
<div data-controller="list-filter">
<%= render DS::SearchInput.new(
variant: :embedded,
@@ -10,35 +11,13 @@
}
) %>
<div class="my-2" id="list" data-list-filter-target="list">
- <% ([ Merchant.no_merchant ] + Current.family.assigned_merchants_for(Current.user).with_attached_logo.alphabetically).each do |merchant| %>
- <div class="filterable-item flex items-center gap-2 p-2" data-filter-name="<%= merchant.name %>">
- <%= form.check_box :merchants,
- {
- multiple: true,
- checked: @q[:merchants]&.include?(merchant.filter_value),
- class: "checkbox checkbox--light"
- },
- merchant.filter_value,
- nil %>
- <%= form.label :merchants, value: merchant.filter_value, class: "text-sm text-primary flex items-center gap-2" do %>
- <div class="hidden lg:flex">
- <% if merchant.logo_url.present? %>
- <%= image_tag Setting.transform_brand_fetch_url(merchant.logo_url),
- class: "w-6 h-6 rounded-full border border-secondary",
- loading: "lazy" %>
- <% else %>
- <%= render DS::FilledIcon.new(
- variant: :text,
- hex_color: merchant.color,
- text: merchant.name,
- size: "sm",
- rounded: true
- ) %>
- <% end %>
- </div>
- <%= merchant.name %>
- <% end %>
- </div>
+ <%= turbo_frame_tag "merchant_filter_options",
+ src: transactions_merchant_filter_path(merchants: @q[:merchants]),
+ loading: :lazy,
+ data: { action: "turbo:frame-load->list-filter#filter" } do %>
+ <% Array(@q[:merchants]).each do |value| %>
+ <%= hidden_field_tag "q[merchants][]", value, id: nil %>
+ <% end %>
<% end %>
</div>
</div>
diff --git a/config/routes.rb b/config/routes.rb
index 960bfbe1d8..51f65bb72b 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -577,6 +577,7 @@ Rails.application.routes.draw do
namespace :transactions do
resource :bulk_deletion, only: :create
+ resource :merchant_filter, only: :show
resource :bulk_update, only: %i[new create]
resource :categorize, only: %i[show create] do
patch :assign_entry, on: :collection
diff --git a/test/controllers/transactions/merchant_filters_controller_test.rb b/test/controllers/transactions/merchant_filters_controller_test.rb
new file mode 100644
index 0000000000..caee58b8c6
--- /dev/null
+++ b/test/controllers/transactions/merchant_filters_controller_test.rb
@@ -0,0 +1,17 @@
+require "test_helper"
+
+class Transactions::MerchantFiltersControllerTest < ActionDispatch::IntegrationTest
+ setup do
+ sign_in users(:family_admin)
+ end
+
+ test "lists the family's merchants and checks the selected ones" do
+ get transactions_merchant_filter_url(merchants: [ "Amazon" ])
+
+ assert_response :success
+ assert_select "turbo-frame#merchant_filter_options" do
+ assert_select "input[type=checkbox][name='q[merchants][]'][value=?][checked]", "Amazon"
+ assert_select "input[type=checkbox][name='q[merchants][]'][value=?]:not([checked])", Merchant::NO_MERCHANT_FILTER_VALUE
+ end
+ end
+end
diff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rb
index 89697b69e5..2ca5c0decf 100644
--- a/test/controllers/transactions_controller_test.rb
+++ b/test/controllers/transactions_controller_test.rb
@@ -12,6 +12,16 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
# Bills has always linked out to transactions. Until now nothing linked back,
# so a transaction that settled a bill was a dead end. The link-back is part
# of the preview-gated bills surface, so the viewer needs the flag.
+ test "index loads the merchant list on demand and keeps selected merchants in the form" do
+ get transactions_url(q: { merchants: [ "Amazon" ] })
+
+ assert_response :success
+ assert_select "#transactions-search turbo-frame#merchant_filter_options[loading=lazy][data-action=?]", "turbo:frame-load->list-filter#filter" do
+ assert_select "input[type=hidden][name='q[merchants][]'][value=?]", "Amazon"
+ end
+ assert_select "input[type=checkbox][name='q[merchants][]']", count: 0
+ end
+
test "a German transaction shows the bill it paid with localized copy" do
@user.update!(locale: "de")
@user.update!(preferences: (@user.preferences || {}).merge("preview_features_enabled" => true))