The personal finance app for everyone (by everyone)
Ruby
Let users hide dashboard widgets from a Customize mode (#3860)
Guillem Arias Fauste51192d08720f
diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rbindex 665cb2ec22..ea97828d3e 100644--- a/app/controllers/pages_controller.rb+++ b/app/controllers/pages_controller.rb@@ -26,6 +26,11 @@ class PagesController < ApplicationController# bars in the "money_flow" dashboard widget.MONEY_FLOW_CHART_MONTHS = 6+ # Query params that shape what the dashboard shows without being saved+ # anywhere. Customize mode carries them through its links and hide/add+ # buttons so the widgets don't jump back to their defaults.+ DASHBOARD_VIEW_PARAMS = [ :start_date, :end_date, :money_flow_month, :spending_month, { money_flow_account_ids: [] } ].freeze+# Selectable height presets (px) for grow widgets.DASHBOARD_HEIGHT_PRESETS = { "compact" => 208, "auto" => 288, "tall" => 416 }.freezeDEFAULT_HEIGHT_PRESET = "auto"@@ -42,33 +47,11 @@ class PagesController < ApplicationController@investment_statement = Current.family.investment_statement@accounts = Current.user.accessible_accounts.visible.with_attached_logo- family_currency = Current.family.currency-- # Use IncomeStatement for all cashflow data (now includes categorized trades)- income_statement = Current.family.income_statement- income_totals = income_statement.income_totals(period: @period)- expense_totals = income_statement.expense_totals(period: @period)- net_totals = income_statement.net_category_totals(period: @period)-- @cashflow_sankey_data = build_cashflow_sankey_data(net_totals, income_totals, expense_totals, family_currency)- @outflows_data = build_outflows_donut_data(net_totals)- # Preview-gated: skip the query outright rather than loading rows the- # section won't be built from.- @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)-- @spending_trend_month = spending_trend_month_param- @spending_trend_data = build_spending_trend_data(income_statement, @spending_trend_month)-- @dashboard_sections = build_dashboard_sections+ @dashboard_sections, @hidden_dashboard_sections = build_dashboard_sections+ @customizing_dashboard = params[:customize].present?+ @dashboard_view_params = dashboard_view_params+ @just_hidden_section = params[:hidden_section]+ @just_shown_section = params[:shown_section]@breadcrumbs = [ [ t("breadcrumbs.home"), root_path ], [ t("breadcrumbs.dashboard"), nil ] ]end@@ -78,13 +61,27 @@ class PagesController < ApplicationControllerenddef update_preferences- if Current.user.update_dashboard_preferences(preferences_params)+ if Current.user.update_dashboard_preferences(preferences_params, laid_out_order: ordered_dashboard_section_keys)head :okelsehead :unprocessable_entityendend+ def update_section_hidden+ section_key = params[:section_key]+ return head :not_found unless DASHBOARD_SECTION_LAYOUTS.key?(section_key)++ hidden = ActiveModel::Type::Boolean.new.cast(params[:hidden])+ Current.user.update_dashboard_section_hidden(section_key, hidden)+ # Two param names rather than one, so hiding a widget and adding it back+ # never redirect to the same URL: Turbo morphs a same-URL visit as a+ # refresh, and a morph doesn't apply the autofocus that keeps keyboard+ # users on the widget they just changed.+ changed = hidden ? { hidden_section: section_key } : { shown_section: section_key }+ redirect_to root_path(dashboard_view_params.merge(customize: true, **changed)), status: :see_other+ end+def changelog@breadcrumbs = [ [ t("breadcrumbs.home"), root_path ], [ t("breadcrumbs.changelog"), nil ] ]@release_notes = github_provider.fetch_latest_release_notes@@ -130,6 +127,144 @@ class PagesController < ApplicationControllerendend+ def dashboard_view_params+ params.permit(*DASHBOARD_VIEW_PARAMS).to_h+ end++ # Each widget builds its own data, so a hidden widget's builder is never+ # called and its queries never run.+ def dashboard_section_builders+ {+ "insights_feed" => -> { insights_feed_section },+ "cashflow_sankey" => -> {+ {+ key: "cashflow_sankey",+ title: "pages.dashboard.cashflow_sankey.title",+ partial: "pages/dashboard/cashflow_sankey",+ layout: section_layout("cashflow_sankey"),+ locals: { sankey_data: cashflow_sankey_data, period: @period },+ visible: @accounts.any?,+ collapsible: true+ }+ },+ "money_flow" => -> {+ accounts = dashboard_income_statement.eligible_accounts+ {+ key: "money_flow",+ title: "pages.dashboard.money_flow.title",+ partial: "pages/dashboard/money_flow",+ layout: section_layout("money_flow"),+ locals: {+ money_flow_data: build_money_flow_data(dashboard_income_statement, money_flow_month_param, money_flow_account_ids_param(accounts)),+ accounts: accounts,+ # TransactionsController's default (account_ids absent) scopes+ # to this broader set, not `accounts`, so the view needs it to+ # know when the drill-down links can safely omit account_ids.+ accessible_account_ids: Current.user.accessible_accounts.pluck(:id).map(&:to_s),+ col_span: section_layout("money_flow")[:col_span]+ },+ visible: @accounts.any?,+ collapsible: true+ }+ },+ "spending_trend" => -> {+ {+ key: "spending_trend",+ title: "pages.dashboard.spending_trend.title",+ partial: "pages/dashboard/spending_trend",+ layout: section_layout("spending_trend"),+ locals: { spending_trend_data: build_spending_trend_data(dashboard_income_statement, spending_trend_month_param) },+ visible: @accounts.any?,+ collapsible: true+ }+ },+ "outflows_donut" => -> {+ outflows_data = build_outflows_donut_data(dashboard_net_totals)+ {+ key: "outflows_donut",+ title: "pages.dashboard.outflows_donut.title",+ partial: "pages/dashboard/outflows_donut",+ layout: section_layout("outflows_donut"),+ locals: { outflows_data: outflows_data, period: @period },+ visible: @accounts.any? && outflows_data[:categories].present?,+ collapsible: true+ }+ },+ "investment_summary" => -> {+ {+ key: "investment_summary",+ title: "pages.dashboard.investment_summary.title",+ partial: "pages/dashboard/investment_summary",+ layout: section_layout("investment_summary"),+ locals: { investment_statement: @investment_statement, period: @period },+ visible: investment_summary_available?,+ collapsible: true+ }+ },+ "net_worth_chart" => -> {+ {+ key: "net_worth_chart",+ title: "pages.dashboard.net_worth_chart.title",+ partial: "pages/dashboard/net_worth_chart",+ layout: section_layout("net_worth_chart"),+ locals: { balance_sheet: @balance_sheet, period: @period },+ visible: @accounts.any?,+ collapsible: true+ }+ },+ "balance_sheet" => -> {+ {+ key: "balance_sheet",+ title: "pages.dashboard.balance_sheet.title",+ partial: "pages/dashboard/balance_sheet",+ layout: section_layout("balance_sheet"),+ locals: { balance_sheet: @balance_sheet },+ visible: @accounts.any?,+ collapsible: true+ }+ }+ }+ end++ # Just enough for the hidden list, without running the widget's queries.+ # Widgets whose data depends on the period stay on offer, since another+ # period may have something to show. Without investment accounts the+ # investment summary never has anything to show, and without insights+ # neither does the feed.+ def hidden_dashboard_section(key)+ return nil if key == "insights_feed" && !preview_features_enabled?++ visible = case key+ when "investment_summary" then investment_summary_available?+ when "insights_feed" then Current.family.insights.visible.exists?+ else @accounts.any?+ end++ { key: key, title: "pages.dashboard.#{key}.title", visible: visible }+ end++ def investment_summary_available?+ @accounts.any? && @investment_statement.investment_accounts.any?+ end++ # Use IncomeStatement for all cashflow data (now includes categorized trades)+ def dashboard_income_statement+ @dashboard_income_statement ||= Current.family.income_statement+ end++ def dashboard_net_totals+ @dashboard_net_totals ||= dashboard_income_statement.net_category_totals(period: @period)+ end++ def cashflow_sankey_data+ build_cashflow_sankey_data(+ dashboard_net_totals,+ dashboard_income_statement.income_totals(period: @period),+ dashboard_income_statement.expense_totals(period: @period),+ Current.family.currency+ )+ end+# Preview-gated, and omitted from the section list entirely rather than# left in it with `visible: false`. Dropping it here means the two# downstream behaviors fall out for free: the saved-order lookup finds@@ -137,106 +272,42 @@ class PagesController < ApplicationControllerdef insights_feed_sectionreturn nil unless preview_features_enabled?+ insights = Current.family.insights.visible.ordered.limit(Insight::FEED_LIMIT){key: "insights_feed",title: "pages.dashboard.insights_feed.title",partial: "pages/dashboard/insights_feed",layout: section_layout("insights_feed"),- locals: { insights: @feed_insights },- visible: @feed_insights.any?,+ locals: { insights: insights },+ visible: insights.any?,collapsible: true}enddef build_dashboard_sections- all_sections = [- insights_feed_section,- {- key: "cashflow_sankey",- title: "pages.dashboard.cashflow_sankey.title",- partial: "pages/dashboard/cashflow_sankey",- layout: section_layout("cashflow_sankey"),- locals: { sankey_data: @cashflow_sankey_data, period: @period },- visible: @accounts.any?,- collapsible: true- },- {- key: "money_flow",- 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, accessible_account_ids: @money_flow_accessible_account_ids, col_span: section_layout("money_flow")[:col_span] },- visible: @accounts.any?,- collapsible: true- },- {- key: "spending_trend",- title: "pages.dashboard.spending_trend.title",- partial: "pages/dashboard/spending_trend",- layout: section_layout("spending_trend"),- locals: { spending_trend_data: @spending_trend_data },- visible: @accounts.any?,- collapsible: true- },- {- key: "outflows_donut",- title: "pages.dashboard.outflows_donut.title",- partial: "pages/dashboard/outflows_donut",- layout: section_layout("outflows_donut"),- locals: { outflows_data: @outflows_data, period: @period },- visible: @accounts.any? && @outflows_data[:categories].present?,- collapsible: true- },- {- key: "investment_summary",- title: "pages.dashboard.investment_summary.title",- partial: "pages/dashboard/investment_summary",- layout: section_layout("investment_summary"),- locals: { investment_statement: @investment_statement, period: @period },- visible: @accounts.any? && @investment_statement.investment_accounts.any?,- collapsible: true- },- {- key: "net_worth_chart",- title: "pages.dashboard.net_worth_chart.title",- partial: "pages/dashboard/net_worth_chart",- layout: section_layout("net_worth_chart"),- locals: { balance_sheet: @balance_sheet, period: @period },- visible: @accounts.any?,- collapsible: true- },- {- key: "balance_sheet",- title: "pages.dashboard.balance_sheet.title",- partial: "pages/dashboard/balance_sheet",- layout: section_layout("balance_sheet"),- locals: { balance_sheet: @balance_sheet },- visible: @accounts.any?,- collapsible: true- }- ].compact-- # Order sections according to user preference- section_order = Current.user.dashboard_section_order- ordered_sections = section_order.map do |key|- all_sections.find { |s| s[:key] == key }- end.compact-- # Add any new sections that aren't in the saved order (future-proofing).- # The insights feed leads instead of appending: it's a proactive surface,- # and appending would bury it below the fold for every family with a- # saved order. Users can still drag it back down — that choice persists.- all_sections.each do |section|- next if ordered_sections.include?(section)-- if section[:key] == "insights_feed"- ordered_sections.unshift(section)- else- ordered_sections << section- end+ hidden_keys = Current.user.dashboard_hidden_sections+ builders = dashboard_section_builders+ sections = ordered_dashboard_section_keys.filter_map do |key|+ hidden_keys.include?(key) ? hidden_dashboard_section(key) : builders.fetch(key).callend- ordered_sections+ # Returns [shown, hidden]. Sections with nothing to show are dropped+ # first, so the hidden list never offers back a widget that wouldn't+ # appear once re-added.+ hidden, shown = sections.select { |s| s[:visible] }.partition { |s| hidden_keys.include?(s[:key]) }+ [ shown, hidden ]+ end++ # The order the dashboard lays its widgets out in: the user's saved order,+ # then any widget missing from it (future-proofing). The insights feed+ # leads instead of appending: it's a proactive surface, and appending+ # would bury it below the fold for every family with a saved order. Users+ # can still drag it back down — that choice persists.+ def ordered_dashboard_section_keys+ keys = dashboard_section_builders.keys+ saved = Current.user.dashboard_section_order & keys+ unsaved = keys - saved+ (unsaved & %w[insights_feed]) + saved + (unsaved - %w[insights_feed])end# Resolves a section's layout guardrails, applying the user's height preset@@ -463,9 +534,9 @@ class PagesController < ApplicationControllerend# nil means "all accessible accounts" (the widget's default, unfiltered state)- def money_flow_account_ids_param+ def money_flow_account_ids_param(eligible_accounts)ids = Array(params[:money_flow_account_ids]).reject(&:blank?)- eligible_ids = @money_flow_accounts.map { |a| a.id.to_s }+ eligible_ids = eligible_accounts.map { |a| a.id.to_s }ids &= eligible_idsids.presenceenddiff --git a/app/javascript/controllers/dashboard_sortable_controller.js b/app/javascript/controllers/dashboard_sortable_controller.jsindex e9ba9badf2..64fb845e47 100644--- a/app/javascript/controllers/dashboard_sortable_controller.js+++ b/app/javascript/controllers/dashboard_sortable_controller.js@@ -204,6 +204,9 @@ export default class extends Controller {// ===== Keyboard Navigation =====handleKeyDown(event) {const currentSection = event.currentTarget;+ // Keys pressed on a control inside the card (a header button, a widget's+ // own inputs) belong to that control, not to the reorder grab.+ if (event.target !== currentSection) return;switch (event.key) {case "ArrowUp":diff --git a/app/models/user.rb b/app/models/user.rbindex befa3d0e23..1e7da71e8e 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -628,6 +628,22 @@ class User < ApplicationRecordpreferences&.[]("section_order") || default_dashboard_section_orderend+ def dashboard_hidden_sections+ preferences&.[]("hidden_sections") || []+ end++ # Adds or removes one key rather than taking the whole list, so a stale tab+ # hiding one widget can't bring back another hidden elsewhere.+ def update_dashboard_section_hidden(section_key, hidden)+ transaction do+ lock!++ keys = dashboard_hidden_sections - [ section_key ]+ keys << section_key if hidden+ update!(preferences: (preferences || {}).merge("hidden_sections" => keys))+ end+ end+# Per-widget height preset override ("compact" | "auto" | "tall"); nil = use default.def dashboard_section_height(section_key)preferences&.dig("dashboard_section_layout", section_key, "height")@@ -638,13 +654,18 @@ class User < ApplicationRecordpreferences&.dig("dashboard_section_layout", section_key, "col_span")end- def update_dashboard_preferences(prefs)+ # laid_out_order is the order the dashboard laid its widgets out in, which+ # also places widgets the saved order doesn't list yet.+ def update_dashboard_preferences(prefs, laid_out_order: nil)# Use pessimistic locking to ensure atomic read-modify-write# This prevents race conditions when multiple sections are collapsed quicklytransaction dolock! # Acquire row-level lock (SELECT FOR UPDATE)updated_prefs = (preferences || {}).deep_dup+ if prefs["section_order"]+ prefs = prefs.merge("section_order" => keep_hidden_sections_in_order(prefs["section_order"], laid_out_order || dashboard_section_order))+ endprefs.each do |key, value|if value.is_a?(Hash)updated_prefs[key] ||= {}@@ -762,6 +783,16 @@ class User < ApplicationRecordskip_password_validation == trueend+ # The dashboard only sends the order of the widgets it rendered, so put+ # each hidden widget back after the one it followed before.+ def keep_hidden_sections_in_order(new_order, old_order)+ hidden = dashboard_hidden_sections+ (old_order & hidden).reduce(new_order - hidden) do |order, key|+ predecessor = old_order[0...old_order.index(key)].reverse.find { |k| order.include?(k) }+ order.insert(predecessor ? order.index(predecessor) + 1 : 0, key)+ end+ end+def default_dashboard_section_order%w[insights_feed cashflow_sankey outflows_donut net_worth_chart balance_sheet]enddiff --git a/app/views/pages/dashboard.html.erb b/app/views/pages/dashboard.html.erbindex fb5639cc39..e50aa75697 100644--- a/app/views/pages/dashboard.html.erb+++ b/app/views/pages/dashboard.html.erb@@ -10,6 +10,34 @@</div><div class="flex items-center gap-2">+ <% if accessible_accounts.any? %>+ <% if @customizing_dashboard %>+ <%= render DS::Link.new(+ variant: "outline",+ icon: "check",+ text: t("pages.dashboard.customize.done"),+ href: root_path(@dashboard_view_params)+ ) %>+ <% else %>+ <%= render DS::Link.new(+ variant: "outline",+ icon: "layout-grid",+ href: root_path(@dashboard_view_params.merge(customize: true)),+ aria: { label: t("pages.dashboard.customize.start") },+ title: t("pages.dashboard.customize.start"),+ class: "hidden lg:inline-flex"+ ) %>++ <%= render DS::Link.new(+ variant: "icon",+ icon: "layout-grid",+ href: root_path(@dashboard_view_params.merge(customize: true)),+ aria: { label: t("pages.dashboard.customize.start") },+ class: "rounded-full lg:hidden"+ ) %>+ <% end %>+ <% end %>+<%= render DS::Link.new(icon: "plus",text: t("pages.dashboard.new"),@@ -36,16 +64,62 @@</div><% end %>+ <% if @customizing_dashboard && accessible_accounts.any? %>+ <%= render DS::Card.new(class: "mb-6 gap-3") do %>+ <div class="space-y-1">+ <h2 class="text-base font-medium text-primary"><%= t("pages.dashboard.customize.hidden_title") %></h2>+ <p class="text-sm text-secondary">+ <%= @hidden_dashboard_sections.any? ? t("pages.dashboard.customize.hidden_description") : t("pages.dashboard.customize.none_hidden") %>+ </p>+ </div>+ <% if @hidden_dashboard_sections.any? %>+ <div class="flex flex-wrap gap-2">+ <% @hidden_dashboard_sections.each do |section| %>+ <%= render DS::Button.new(+ variant: "outline",+ size: "sm",+ icon: "plus",+ text: t(section[:title]),+ href: dashboard_section_hidden_path(section[:key]),+ method: :patch,+ params: @dashboard_view_params.merge(hidden: false),+ autofocus: @just_hidden_section == section[:key]+ ) %>+ <% end %>+ </div>+ <% end %>+ <% end %>+ <% end %>++ <% if accessible_accounts.any? && @dashboard_sections.empty? && !@customizing_dashboard %>+ <%= render DS::Card.new(class: "mb-6") do %>+ <%= render DS::EmptyState.new(+ icon: "layout-grid",+ title: t("pages.dashboard.customize.all_hidden_title"),+ description: t("pages.dashboard.customize.all_hidden_description")+ ) do |empty_state| %>+ <% empty_state.with_action do %>+ <%= render DS::Link.new(+ variant: "outline",+ icon: "layout-grid",+ text: t("pages.dashboard.customize.start"),+ href: root_path(@dashboard_view_params.merge(customize: true))+ ) %>+ <% end %>+ <% end %>+ <% end %>+ <% end %>+<% two_column = Current.user.dashboard_two_column? %><div class="grid grid-cols-1 <%= "2xl:grid-cols-2 2xl:items-start 2xl:gap-y-0 2xl:[grid-auto-flow:row_dense] 2xl:[grid-auto-rows:1px]" if two_column %> gap-6 pb-6 lg:pb-12" data-controller="dashboard-sortable dashboard-masonry" data-action="dragover->dashboard-sortable#dragOver drop->dashboard-sortable#drop" role="list" aria-label="<%= t("pages.dashboard.sections_aria_label") %>"><% if accessible_accounts.any? %><% @dashboard_sections.each do |section| %>- <% next unless section[:visible] %><sectionclass="bg-container rounded-xl shadow-border-xs transition-all group focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2<%= " 2xl:col-span-2" if two_column && section[:layout][:col_span] == "full" %>"style="<%= "--dash-widget-h: #{section[:layout][:height_px]}px;" if section[:layout][:grow] %>"data-dashboard-sortable-target="section"data-section-key="<%= section[:key] %>"+ <%= "autofocus" if @just_shown_section == section[:key] %>data-controller="dashboard-section<%= " cashflow-expand" if section[:key] == "cashflow_sankey" %>"data-dashboard-section-section-key-value="<%= section[:key] %>"data-dashboard-section-collapsed-value="<%= Current.user.dashboard_section_collapsed?(section[:key]) %>"@@ -103,6 +177,19 @@<%= icon("maximize-2", size: "sm", class: "!w-3.5 !h-3.5") %></button><% end %>+ <% if @customizing_dashboard %>+ <% hide_label = t("pages.dashboard.customize.hide", title: t(section[:title])) %>+ <%= render DS::Button.new(+ variant: "icon",+ size: "sm",+ icon: "eye-off",+ href: dashboard_section_hidden_path(section[:key]),+ method: :patch,+ params: @dashboard_view_params.merge(hidden: true),+ aria_label: hide_label,+ title: hide_label+ ) %>+ <% end %><% layout = section[:layout] %><% show_width = layout[:width_toggle] && two_column %><% if layout[:grow] || show_width %>diff --git a/config/locales/views/pages/en.yml b/config/locales/views/pages/en.ymlindex f7be00eb89..95f083ed42 100644--- a/config/locales/views/pages/en.yml+++ b/config/locales/views/pages/en.yml@@ -44,6 +44,15 @@ en:sections_aria_label: "Dashboard sections"drag_to_reorder: "Drag to reorder section"toggle_section: "Toggle section visibility"+ customize:+ start: "Customize"+ done: "Done"+ hide: "Hide %{title}"+ hidden_title: "Hidden widgets"+ hidden_description: "Add a widget back to the dashboard."+ none_hidden: "Every widget is shown. Hide one with its eye button."+ all_hidden_title: "Every widget is hidden"+ all_hidden_description: "Customize the dashboard to add widgets back."widget_size:label: "Adjust size"width_label: "Width"diff --git a/config/routes.rb b/config/routes.rbindex 33fc7102fb..5fb0acf03d 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -336,6 +336,7 @@ Rails.application.routes.draw doget "feedback", to: "pages#feedback"get "dashboard/cash_flow", to: "cash_flows#show", as: :dashboard_cash_flowpatch "dashboard/preferences", to: "pages#update_preferences"+ patch "dashboard/sections/:section_key/hidden", to: "pages#update_section_hidden", as: :dashboard_section_hiddenresource :current_session, only: %i[update]diff --git a/test/controllers/pages_controller_test.rb b/test/controllers/pages_controller_test.rbindex f3c5b614c9..e1da9aacc5 100644--- a/test/controllers/pages_controller_test.rb+++ b/test/controllers/pages_controller_test.rb@@ -49,6 +49,149 @@ class PagesControllerTest < ActionDispatch::IntegrationTestassert_equal "single", @user.reload.dashboard_section_width("cashflow_sankey")end+ test "the Customize button is an icon named for screen readers" do+ get root_path++ assert_select "a[href='#{root_path(customize: true)}']", count: 2 do |links|+ links.each do |link|+ assert_equal I18n.t("pages.dashboard.customize.start"), link["aria-label"]+ assert_empty link.text.strip+ end+ end+ end++ test "dashboard leaves hidden sections out" do+ @user.update_dashboard_section_hidden("net_worth_chart", true)++ get root_path++ assert_response :ok+ assert_select "section[data-section-key='net_worth_chart']", count: 0+ assert_select "section[data-section-key='balance_sheet']"+ assert_select "form[action='#{dashboard_section_hidden_path("balance_sheet")}']", count: 0+ end++ test "customize mode offers hiding shown sections and re-adding hidden ones" do+ @user.update_dashboard_section_hidden("net_worth_chart", true)++ get root_path(customize: true)++ assert_response :ok+ assert_select "section[data-section-key='balance_sheet'] form[action='#{dashboard_section_hidden_path("balance_sheet")}'] input[name='hidden'][value='true']"+ assert_select "form[action='#{dashboard_section_hidden_path("net_worth_chart")}'] input[name='hidden'][value='false']"+ assert_select "button", text: I18n.t("pages.dashboard.net_worth_chart.title")+ end++ test "customize mode does not offer back a hidden section with nothing to show" do+ @user.update_dashboard_section_hidden("investment_summary", true)+ InvestmentStatement.any_instance.stubs(:investment_accounts).returns(Account.none)++ get root_path(customize: true)++ assert_response :ok+ assert_select "form[action='#{dashboard_section_hidden_path("investment_summary")}']", count: 0+ end++ test "customize mode offers back a hidden insights feed only while it has insights" do+ @user.update!(preferences: (@user.preferences || {}).merge("preview_features_enabled" => true))+ @user.update_dashboard_section_hidden("insights_feed", true)++ get root_path(customize: true)+ assert_select "form[action='#{dashboard_section_hidden_path("insights_feed")}']"++ @family.insights.update_all(status: "dismissed")++ get root_path(customize: true)+ assert_select "form[action='#{dashboard_section_hidden_path("insights_feed")}']", count: 0+ end++ # A user who never reordered has only the default order saved, which leaves+ # out widgets like money_flow that render after it.+ test "update_preferences keeps a hidden widget's place when the saved order never listed it" do+ @user.update_dashboard_section_hidden("money_flow", true)++ patch "/dashboard/preferences", params: {+ preferences: { section_order: %w[cashflow_sankey outflows_donut net_worth_chart balance_sheet spending_trend investment_summary] }+ }, as: :json++ assert_response :ok+ assert_equal %w[cashflow_sankey outflows_donut net_worth_chart balance_sheet money_flow spending_trend investment_summary],+ @user.reload.dashboard_section_order+ end++ test "update_section_hidden hides and re-adds a section" do+ patch dashboard_section_hidden_path("net_worth_chart"), params: { hidden: true }++ assert_redirected_to root_path(customize: true, hidden_section: "net_worth_chart")+ assert_equal %w[net_worth_chart], @user.reload.dashboard_hidden_sections++ patch dashboard_section_hidden_path("net_worth_chart"), params: { hidden: false }++ assert_redirected_to root_path(customize: true, shown_section: "net_worth_chart")++ assert_equal [], @user.reload.dashboard_hidden_sections+ end++ test "update_section_hidden keeps the dashboard's view params and focuses the changed widget" do+ view_params = {+ start_date: "2026-08-01", end_date: "2026-08-31",+ money_flow_month: "2026-07-01", money_flow_account_ids: [ accounts(:depository).id ],+ spending_month: "2026-06-01"+ }++ patch dashboard_section_hidden_path("net_worth_chart"), params: view_params.merge(hidden: true)++ assert_redirected_to root_path(view_params.merge(customize: true, hidden_section: "net_worth_chart"))+ end++ test "customize mode carries the dashboard's view params through its links and buttons" do+ get root_path(customize: true, start_date: "2026-08-01", end_date: "2026-08-31", spending_month: "2026-06-01")++ assert_select "a[href='#{root_path(start_date: "2026-08-01", end_date: "2026-08-31", spending_month: "2026-06-01")}']", text: I18n.t("pages.dashboard.customize.done")+ assert_select "form[action='#{dashboard_section_hidden_path("balance_sheet")}'] input[name='spending_month'][value='2026-06-01']"+ end++ test "customize mode focuses the widget that was just added back, or its re-add button once hidden" do+ @user.update_dashboard_section_hidden("net_worth_chart", true)++ get root_path(customize: true, hidden_section: "net_worth_chart")+ assert_select "form[action='#{dashboard_section_hidden_path("net_worth_chart")}'] button[autofocus]"++ get root_path(customize: true, shown_section: "balance_sheet")+ assert_select "section[data-section-key='balance_sheet'][autofocus]"+ end++ test "dashboard points to Customize when every widget is hidden" do+ PagesController::DASHBOARD_SECTION_LAYOUTS.each_key { |key| @user.update_dashboard_section_hidden(key, true) }++ get root_path++ assert_response :ok+ assert_select "section[data-section-key]", count: 0+ assert_select "p", text: I18n.t("pages.dashboard.customize.all_hidden_title")+ end++ test "dashboard skips the data queries of hidden widgets" do+ %w[cashflow_sankey outflows_donut money_flow spending_trend].each { |key| @user.update_dashboard_section_hidden(key, true) }+ IncomeStatement.any_instance.expects(:income_totals).never+ IncomeStatement.any_instance.expects(:expense_totals).never+ IncomeStatement.any_instance.expects(:net_category_totals).never+ PagesController.any_instance.expects(:build_money_flow_data).never+ PagesController.any_instance.expects(:build_spending_trend_data).never++ get root_path++ assert_response :ok+ assert_select "section[data-section-key='net_worth_chart']"+ end++ test "update_section_hidden rejects unknown sections" do+ patch dashboard_section_hidden_path("not_a_widget"), params: { hidden: true }++ assert_response :not_found+ assert_equal [], @user.reload.dashboard_hidden_sections+ end+test "update_preferences ignores malformed dashboard_section_layout without erroring" doprevious_height = @user.reload.dashboard_section_height("net_worth_chart")diff --git a/test/models/user_test.rb b/test/models/user_test.rbindex ee739b3165..4d951e7a1c 100644--- a/test/models/user_test.rb+++ b/test/models/user_test.rb@@ -682,6 +682,31 @@ class UserTest < ActiveSupport::TestCaseassert_equal new_order, @user.dashboard_section_orderend+ test "update_dashboard_section_hidden hides and re-adds a section without touching other preferences" do+ @user.update!(preferences: { "section_order" => %w[balance_sheet net_worth_chart] })+ assert_equal [], @user.dashboard_hidden_sections++ @user.update_dashboard_section_hidden("net_worth_chart", true)+ @user.update_dashboard_section_hidden("net_worth_chart", true)+ @user.update_dashboard_section_hidden("balance_sheet", true)+ assert_equal %w[net_worth_chart balance_sheet], @user.reload.dashboard_hidden_sections++ @user.update_dashboard_section_hidden("net_worth_chart", false)+ assert_equal %w[balance_sheet], @user.reload.dashboard_hidden_sections+ assert_equal %w[balance_sheet net_worth_chart], @user.dashboard_section_order+ end++ test "update_dashboard_preferences keeps a hidden section's place when the rest are reordered" do+ @user.update!(preferences: {+ "section_order" => %w[cashflow_sankey net_worth_chart balance_sheet outflows_donut],+ "hidden_sections" => %w[net_worth_chart]+ })++ @user.update_dashboard_preferences({ "section_order" => %w[outflows_donut cashflow_sankey balance_sheet] })++ assert_equal %w[outflows_donut cashflow_sankey net_worth_chart balance_sheet], @user.reload.dashboard_section_order+ end+test "dashboard_section_height returns stored preset or nil" do@user.update!(preferences: {})assert_nil @user.dashboard_section_height("net_worth_chart")diff --git a/test/system/dashboard_customize_test.rb b/test/system/dashboard_customize_test.rbnew file mode 100644index 0000000000..21d049eed0--- /dev/null+++ b/test/system/dashboard_customize_test.rb@@ -0,0 +1,25 @@+require "application_system_test_case"++class DashboardCustomizeTest < ApplicationSystemTestCase+ setup do+ @user = users(:family_admin)+ end++ test "hides a widget from the keyboard and adds it back" do+ sign_in @user+ net_worth = I18n.t("pages.dashboard.net_worth_chart.title")++ click_on I18n.t("pages.dashboard.customize.start"), match: :first+ find_button(I18n.t("pages.dashboard.customize.hide", title: net_worth)).send_keys(:enter)++ assert_no_selector "section[data-section-key='net_worth_chart']"+ assert_equal net_worth, page.evaluate_script("document.activeElement.textContent").strip++ click_on net_worth+ assert_selector "section[data-section-key='net_worth_chart']"+ assert_equal "net_worth_chart", page.evaluate_script("document.activeElement.dataset.sectionKey")++ click_on I18n.t("pages.dashboard.customize.done")+ assert_no_button I18n.t("pages.dashboard.customize.hide", title: net_worth)+ end+end