Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(dashboard): shorten money-flow drill-down URLs when possible (#3018)

GFR455316c96b66
diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb
index 2a25747948..6a8f62d66d 100644
--- a/app/controllers/pages_controller.rb
+++ b/app/controllers/pages_controller.rb
@@ -56,6 +56,10 @@ class PagesController < ApplicationController
@feed_insights = preview_features_enabled? ? Current.family.insights.visible.ordered.limit(Insight::FEED_LIMIT) : Insight.none
@money_flow_accounts = income_statement.eligible_accounts
+ # TransactionsController's default (account_ids absent) scopes to this
+ # broader set, not @money_flow_accounts, so the view needs it to know
+ # when the drill-down links can safely omit account_ids.
+ @money_flow_accessible_account_ids = Current.user.accessible_accounts.pluck(:id).map(&:to_s)
@money_flow_month = money_flow_month_param
@money_flow_account_ids = money_flow_account_ids_param
@money_flow_data = build_money_flow_data(income_statement, @money_flow_month, @money_flow_account_ids)
@@ -157,7 +161,7 @@ class PagesController < ApplicationController
title: "pages.dashboard.money_flow.title",
partial: "pages/dashboard/money_flow",
layout: section_layout("money_flow"),
- locals: { money_flow_data: @money_flow_data, accounts: @money_flow_accounts, col_span: section_layout("money_flow")[:col_span] },
+ locals: { money_flow_data: @money_flow_data, accounts: @money_flow_accounts, accessible_account_ids: @money_flow_accessible_account_ids, col_span: section_layout("money_flow")[:col_span] },
visible: @accounts.any?,
collapsible: true
},
diff --git a/app/views/pages/dashboard/_money_flow.html.erb b/app/views/pages/dashboard/_money_flow.html.erb
index 1d8bd490b5..0c84b9b429 100644
--- a/app/views/pages/dashboard/_money_flow.html.erb
+++ b/app/views/pages/dashboard/_money_flow.html.erb
@@ -1,8 +1,14 @@
-<%# locals: (money_flow_data:, accounts:, col_span: "single") %>
+<%# locals: (money_flow_data:, accounts:, accessible_account_ids:, col_span: "single") %>
<%
pill_button_class = "inline-flex items-center gap-1.5 bg-container border border-secondary font-medium rounded-lg pl-3 pr-2 py-2 text-sm cursor-pointer text-primary hover:bg-container-inset-hover focus:outline-hidden focus:ring-0 whitespace-nowrap"
selected_account_ids = money_flow_data[:account_ids] || accounts.map { |a| a.id.to_s }
all_accounts_selected = selected_account_ids.size == accounts.size
+ # TransactionsController falls back to the user's full accessible-accounts
+ # set (broader than this widget's eligible accounts) when account_ids is
+ # absent, so we can only drop the param when that default matches this
+ # widget's selection exactly — otherwise the drill-down would silently show
+ # transactions outside what these totals represent.
+ drilldown_account_ids = selected_account_ids.sort == accessible_account_ids.sort ? nil : selected_account_ids
period = money_flow_data[:period]
# At full width the chart and summary sit side by side; at single/half width
# they stack so the chart gets the full column and the month labels don't crowd.
@@ -107,7 +113,7 @@
</span>
</div>
- <%= link_to transactions_path(q: { types: [ "income" ], status: [ "confirmed" ], start_date: period.date_range.begin, end_date: period.date_range.end, account_ids: selected_account_ids }.compact),
+ <%= link_to transactions_path(q: { types: [ "income" ], status: [ "confirmed" ], start_date: period.date_range.begin, end_date: period.date_range.end, account_ids: drilldown_account_ids }.compact),
class: "flex items-center justify-between p-3 border border-secondary rounded-lg hover:bg-container-inset-hover transition-colors group",
data: { turbo_frame: "_top" } do %>
<span class="flex items-center gap-2 text-sm text-primary">
@@ -124,7 +130,7 @@
</span>
<% end %>
- <%= link_to transactions_path(q: { types: [ "expense" ], status: [ "confirmed" ], start_date: period.date_range.begin, end_date: period.date_range.end, account_ids: selected_account_ids }.compact),
+ <%= link_to transactions_path(q: { types: [ "expense" ], status: [ "confirmed" ], start_date: period.date_range.begin, end_date: period.date_range.end, account_ids: drilldown_account_ids }.compact),
class: "flex items-center justify-between p-3 border border-secondary rounded-lg hover:bg-container-inset-hover transition-colors group",
data: { turbo_frame: "_top" } do %>
<span class="flex items-center gap-2 text-sm text-primary">
diff --git a/test/controllers/pages_controller_test.rb b/test/controllers/pages_controller_test.rb
index c0f149194c..434b761923 100644
--- a/test/controllers/pages_controller_test.rb
+++ b/test/controllers/pages_controller_test.rb
@@ -250,9 +250,47 @@ class PagesControllerTest < ActionDispatch::IntegrationTest
# accessible-accounts set transactions_path defaults to when account_ids
# is absent.
assert_includes income_href, "q%5Baccount_ids%5D%5B%5D="
- assert_not_includes income_href, excluded_account.id
+ assert_not_includes income_href, excluded_account.id.to_s
assert_includes expense_href, "q%5Baccount_ids%5D%5B%5D="
- assert_not_includes expense_href, excluded_account.id
+ assert_not_includes expense_href, excluded_account.id.to_s
+ end
+
+ test "dashboard money flow income/expense links omit account_ids when the default selection matches all accessible accounts" do
+ # Plain @family fixture: every account is owned outright by family_admin,
+ # none excluded from reports or tax-advantaged, so the widget's eligible
+ # accounts exactly match Current.user.accessible_accounts (see #2955).
+ get root_path
+
+ assert_response :ok
+ income_href = css_select("a[href*='q%5Btypes%5D%5B%5D=income']").first["href"]
+ expense_href = css_select("a[href*='q%5Btypes%5D%5B%5D=expense']").first["href"]
+
+ # With nothing to scope down from the transactions page's own default,
+ # the link should skip enumerating every account id so the URL stays
+ # short (long q[account_ids][] lists break forward-auth proxies in front
+ # of self-hosted deployments, see #2955).
+ assert_not_includes income_href, "q%5Baccount_ids%5D"
+ assert_not_includes expense_href, "q%5Baccount_ids%5D"
+ end
+
+ test "dashboard money flow income/expense links keep account_ids when a subset of accounts is explicitly selected" do
+ account = @family.accounts.first
+
+ get root_path, params: { money_flow_account_ids: [ account.id ] }
+
+ assert_response :ok
+ income_href = css_select("a[href*='q%5Btypes%5D%5B%5D=income']").first["href"]
+ expense_href = css_select("a[href*='q%5Btypes%5D%5B%5D=expense']").first["href"]
+
+ # A deliberate, narrower selection never matches the full
+ # accessible-accounts set, so the links must keep scoping to it instead
+ # of silently falling back to "all accounts".
+ assert_includes income_href, "q%5Baccount_ids%5D%5B%5D=#{account.id}"
+ assert_includes expense_href, "q%5Baccount_ids%5D%5B%5D=#{account.id}"
+
+ account_filter = "q%5Baccount_ids%5D%5B%5D="
+ assert_equal 1, income_href.scan(account_filter).length
+ assert_equal 1, expense_href.scan(account_filter).length
end
test "changelog" do