Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

perf: lazy-load the transactions filter menu (index view −34ms/request) (#2559)

Juan José Mata27332f0a976b
diff --git a/app/controllers/transactions/filters_controller.rb b/app/controllers/transactions/filters_controller.rb
new file mode 100644
index 0000000000..ea7388fe42
--- /dev/null
+++ b/app/controllers/transactions/filters_controller.rb
@@ -0,0 +1,37 @@
+class Transactions::FiltersController < ApplicationController
+ # Renders the transactions search filter menu, lazy-loaded into the filter
+ # popover so the index doesn't pay for the account/category/merchant/tag
+ # lists on every request while the popover is closed.
+ def show
+ @q = search_params
+ render layout: false if turbo_frame_request?
+ end
+
+ private
+ def search_params
+ cleaned_params = params.fetch(:q, {})
+ .permit(
+ :start_date, :end_date, :search, :amount,
+ :amount_operator, :active_accounts_only,
+ accounts: [], account_ids: [],
+ categories: [], merchants: [], types: [], tags: [], status: [], ai_status: [],
+ attachment_status: []
+ )
+ .to_h
+ .compact_blank
+
+ cleaned_params.delete(:amount_operator) unless cleaned_params[:amount].present?
+
+ if cleaned_params[:ai_status]
+ cleaned_params[:ai_status] &= Transaction::Search::AI_STATUSES
+ cleaned_params.delete(:ai_status) if cleaned_params[:ai_status].empty?
+ end
+
+ if cleaned_params[:attachment_status]
+ cleaned_params[:attachment_status] &= Transaction::Search::ATTACHMENT_STATUSES
+ cleaned_params.delete(:attachment_status) if cleaned_params[:attachment_status].empty?
+ end
+
+ cleaned_params
+ end
+end
diff --git a/app/views/transactions/filters/show.html.erb b/app/views/transactions/filters/show.html.erb
new file mode 100644
index 0000000000..f332e4b5df
--- /dev/null
+++ b/app/views/transactions/filters/show.html.erb
@@ -0,0 +1,8 @@
+<%= turbo_frame_tag "transaction-filters" do %>
+ <%# fields (not form_with) because this frame lives inside the search form
+ in the DOM - a nested <form> would be invalid HTML. The inputs still
+ submit with the outer form. %>
+ <%= fields :q do |form| %>
+ <%= render "transactions/searches/menu", form: form %>
+ <% end %>
+<% end %>
diff --git a/app/views/transactions/searches/_form.html.erb b/app/views/transactions/searches/_form.html.erb
index 93fda30b3d..bff403ff7e 100644
--- a/app/views/transactions/searches/_form.html.erb
+++ b/app/views/transactions/searches/_form.html.erb
@@ -27,7 +27,11 @@
) %>
<% popover.with_custom_content do %>
- <%= render "transactions/searches/menu", form: form %>
+ <%= turbo_frame_tag "transaction-filters", src: filters_transactions_path(q: @q), loading: :lazy do %>
+ <div class="flex items-center justify-center h-[50vh] md:h-80 w-full md:w-[540px] p-6">
+ <p class="text-sm text-secondary animate-pulse"><%= t("transactions.searches.menu.loading") %></p>
+ </div>
+ <% end %>
<% end %>
<% end %>
diff --git a/app/views/transactions/searches/_menu.html.erb b/app/views/transactions/searches/_menu.html.erb
index 13d0601788..1db57f86cf 100644
--- a/app/views/transactions/searches/_menu.html.erb
+++ b/app/views/transactions/searches/_menu.html.erb
@@ -33,6 +33,7 @@
text: t(".clear_filters"),
variant: "ghost",
href: clear_filters_path || transactions_path(clear_filters: true),
+ frame: "_top",
) %>
<% end %>
</div>
diff --git a/config/locales/views/transactions/en.yml b/config/locales/views/transactions/en.yml
index 488b18dd8c..c05aca3b68 100644
--- a/config/locales/views/transactions/en.yml
+++ b/config/locales/views/transactions/en.yml
@@ -401,6 +401,7 @@ en:
category_filter: Category
clear_filters: Clear filters
date_filter: Date
+ loading: Loading filters...
merchant_filter: Merchant
reconcile_status_filter: Reconciliation
status_filter: Status
diff --git a/config/routes.rb b/config/routes.rb
index c17c5d18b5..dc5755dcff 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -620,6 +620,7 @@ Rails.application.routes.draw do
collection do
delete :clear_filter
get :name_suggestions
+ get :filters, to: "transactions/filters#show"
end
member do
diff --git a/test/controllers/transactions/filters_controller_test.rb b/test/controllers/transactions/filters_controller_test.rb
new file mode 100644
index 0000000000..6a8c27d0e4
--- /dev/null
+++ b/test/controllers/transactions/filters_controller_test.rb
@@ -0,0 +1,88 @@
+require "test_helper"
+
+class Transactions::FiltersControllerTest < ActionDispatch::IntegrationTest
+ setup do
+ sign_in @user = users(:family_admin)
+ end
+
+ test "renders the filter menu inside the lazy-loaded turbo frame" do
+ get filters_transactions_url, headers: { "Turbo-Frame" => "transaction-filters" }
+
+ assert_response :success
+ assert_select "turbo-frame#transaction-filters" do
+ assert_select "#transaction-filters-menu"
+ end
+ end
+
+ test "preserves active filters as checked state" do
+ account = @user.accessible_accounts.alphabetically.first
+
+ get filters_transactions_url(q: { accounts: [ account.name ] }),
+ headers: { "Turbo-Frame" => "transaction-filters" }
+
+ assert_response :success
+ assert_select "input[type=checkbox][name='q[accounts][]'][value='#{account.name}'][checked]"
+ end
+
+ test "groups subcategories immediately after their parent in the category filter" do
+ get filters_transactions_url, headers: { "Turbo-Frame" => "transaction-filters" }
+ assert_response :success
+
+ doc = Nokogiri::HTML::Document.parse(response.body)
+ checkbox_values = doc.css("input[name='q[categories][]']").map { |node| node["value"] }
+
+ parent_index = checkbox_values.index(categories(:food_and_drink).name)
+ child_index = checkbox_values.index(categories(:subcategory).name)
+
+ assert_not_nil parent_index
+ assert_not_nil child_index
+ assert_equal parent_index + 1, child_index
+ end
+
+ test "offers the attachment filter in the filter menu" do
+ get filters_transactions_url, headers: { "Turbo-Frame" => "transaction-filters" }
+
+ assert_response :success
+ assert_select "#transaction-filters-menu input[type=checkbox][name='q[attachment_status][]']", count: 2
+ end
+
+ test "preserves attachment and AI filter checked state" do
+ get filters_transactions_url(q: { attachment_status: [ "with" ], ai_status: [ "history" ] }),
+ headers: { "Turbo-Frame" => "transaction-filters" }
+
+ assert_response :success
+ assert_select "input[name='q[attachment_status][]'][value='with'][checked]"
+ assert_select "input[name='q[attachment_status][]'][value='without'][checked]", count: 0
+ assert_select "input[name='q[ai_status][]'][value='history'][checked]"
+ assert_select "input[name='q[ai_status][]'][value='current'][checked]", count: 0
+ end
+
+ test "clear filters navigates the whole page outside the lazy frame" do
+ get filters_transactions_url(q: { attachment_status: [ "with" ] }),
+ headers: { "Turbo-Frame" => "transaction-filters" }
+
+ assert_response :success
+ assert_select "turbo-frame#transaction-filters" do
+ assert_select "a[href=?][data-turbo-frame='_top']", transactions_path(clear_filters: true)
+ end
+ end
+
+ test "filter menu loads the merchant list on demand and keeps selected merchants in the form" do
+ get filters_transactions_url(q: { merchants: [ "Amazon" ] }),
+ headers: { "Turbo-Frame" => "transaction-filters" }
+
+ assert_response :success
+ assert_select "turbo-frame#transaction-filters 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 "requires authentication" do
+ delete session_url(sessions(:one)) rescue nil
+ reset!
+
+ get filters_transactions_url
+ assert_redirected_to new_session_url
+ end
+end
diff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rb
index 2292db5da0..c9ef120189 100644
--- a/test/controllers/transactions_controller_test.rb
+++ b/test/controllers/transactions_controller_test.rb
@@ -12,13 +12,12 @@ 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
+ test "index lazy loads filters with selected merchants in the frame URL" 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 "#transactions-search turbo-frame#transaction-filters[loading=lazy][src=?]",
+ filters_transactions_path(q: { merchants: [ "Amazon" ] })
assert_select "input[type=checkbox][name='q[merchants][]']", count: 0
end
@@ -220,21 +219,6 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
assert_select "button[data-label='Sweep In']", text: "Автоперевод (зачисление)"
end
- test "index groups subcategories immediately after their parent in the category filter" do
- get transactions_url
- assert_response :success
-
- doc = Nokogiri::HTML::Document.parse(response.body)
- checkbox_values = doc.css("input[name='q[categories][]']").map { |node| node["value"] }
-
- parent_index = checkbox_values.index(categories(:food_and_drink).name)
- child_index = checkbox_values.index(categories(:subcategory).name)
-
- assert_not_nil parent_index
- assert_not_nil child_index
- assert_equal parent_index + 1, child_index
- end
-
# The form passes its own change action on the currency select; the money
# field must keep its handleCurrencyChange alongside it, or switching the
# currency never refreshes the amount's step and precision.
@@ -2427,13 +2411,6 @@ end
assert_response :success
end
- test "index offers the attachment filter in the filter menu" do
- get transactions_url
-
- assert_response :success
- assert_select "#transaction-filters-menu input[type=checkbox][name='q[attachment_status][]']", count: 2
- end
-
test "index ignores unsupported attachment_status values without rendering a badge" do
get transactions_url(q: { attachment_status: [ "bogus" ] })