The personal finance app for everyone (by everyone)
Ruby
feat(dashboard): unify per-widget period selectors into one picker (#2162)
Guillem Arias Fauste96079188a2a1
diff --git a/app/components/UI/account/chart.html.erb b/app/components/UI/account/chart.html.erbindex c14627575f..8e88e3445c 100644--- a/app/components/UI/account/chart.html.erb+++ b/app/components/UI/account/chart.html.erb@@ -17,23 +17,23 @@</div></div>- <%= form_with url: account_path(account), method: :get, data: { controller: "auto-submit-form" } do |form| %>- <div class="flex items-center gap-2">- <% if account.supports_trades? %>+ <div class="flex items-center gap-2">+ <% if account.supports_trades? %>+ <%= form_with url: account_path(account), method: :get, data: { controller: "auto-submit-form" } do |form| %><%= form.select :chart_view,[[t(".views.total_value"), "balance"], [t(".views.holdings"), "holdings_balance"], [t(".views.cash"), "cash_balance"]],{ selected: view },class: "bg-container border border-secondary rounded-lg text-sm pr-7 cursor-pointer text-primary focus:outline-hidden focus:ring-0",data: { "auto-submit-form-target": "auto" } %><% end %>+ <% end %>- <%= form.select :period,- Period.as_options,- { selected: period.key },- data: { "auto-submit-form-target": "auto" },- class: "bg-container border border-secondary rounded-lg px-3 py-2 text-sm pr-7 cursor-pointer text-primary focus:outline-hidden focus:ring-0" %>- </div>- <% end %>+ <%= render UI::PeriodPicker.new(+ selected: period,+ url: account_path(account),+ extra_params: account.supports_trades? ? { chart_view: view } : {}+ ) %>+ </div></div><%= turbo_frame_tag dom_id(@account, :chart_details) do %>diff --git a/app/components/UI/period_picker.html.erb b/app/components/UI/period_picker.html.erbnew file mode 100644index 0000000000..d7ccbf3024--- /dev/null+++ b/app/components/UI/period_picker.html.erb@@ -0,0 +1,20 @@+<%= render DS::Menu.new(variant: :button, placement: placement) do |menu| %>+ <% menu.with_button(+ type: "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",+ aria: { label: t(".aria_label", period: selected_label, default: "Time period: %{period}") }+ ) do %>+ <span class="whitespace-nowrap"><%= selected_label %></span>+ <%= helpers.icon("chevron-down", size: "sm") %>+ <% end %>++ <% periods.each do |period| %>+ <% menu.with_item(+ variant: :link,+ text: period.label_short,+ href: href_for(period.key),+ frame: frame,+ selected: selected?(period.key)+ ) %>+ <% end %>+<% end %>diff --git a/app/components/UI/period_picker.rb b/app/components/UI/period_picker.rbnew file mode 100644index 0000000000..60d75a459e--- /dev/null+++ b/app/components/UI/period_picker.rb@@ -0,0 +1,46 @@+class UI::PeriodPicker < ApplicationComponent+ # Unified time-range selector shared by the dashboard and account charts.+ #+ # Renders a DS::Menu as a flat list of link items — one per Period. Each item+ # is a GET link to `url` carrying `?period=<key>` (plus any `extra_params`),+ # which re-renders `frame` (a Turbo Frame id) with the chosen period. When+ # `frame` is nil the links fall back to a normal Turbo Drive visit.+ #+ # The selected period is marked with a check icon and `aria-current`, and the+ # trigger button shows its label.+ #+ # NOTE: `url` must be a path without a query string; pass query state via+ # `extra_params` so the picker can compose `?period=…` cleanly.+ attr_reader :selected_key, :url, :frame, :extra_params, :placement++ def initialize(selected:, url:, frame: nil, extra_params: {}, placement: "bottom-end")+ @selected_key = selected.respond_to?(:key) ? selected.key : selected.to_s+ @url = url+ @frame = frame+ @extra_params = (extra_params || {}).symbolize_keys+ @placement = placement+ end++ def periods+ Period.all+ end++ def selected_label+ period_for(selected_key).label_short+ end++ def selected?(key)+ key == selected_key+ end++ def href_for(key)+ "#{url}?#{extra_params.merge(period: key).to_query}"+ end++ private+ def period_for(key)+ Period.from_key(key)+ rescue Period::InvalidKeyError+ Period.last_30_days+ end+enddiff --git a/app/models/period.rb b/app/models/period.rbindex b4857daee4..b5a3c80b6b 100644--- a/app/models/period.rb+++ b/app/models/period.rb@@ -124,7 +124,10 @@ class Perioddef current_month_for(family)return from_key("current_month") unless family&.uses_custom_month_start?- family.current_custom_month_period+ # Keep the semantic key so callers (e.g. the period picker) can identify+ # this as "current month" even though the date range is custom.+ custom_period = family.current_custom_month_period+ new(key: "current_month", start_date: custom_period.start_date, end_date: custom_period.end_date)enddef last_month_for(family)@@ -134,7 +137,7 @@ class Periodlast_month_date = current_start - 1.daystart_date = family.custom_month_start_for(last_month_date)end_date = family.custom_month_end_for(last_month_date)- custom(start_date: start_date, end_date: end_date)+ new(key: "last_month", start_date: start_date, end_date: end_date)endenddiff --git a/app/views/pages/dashboard.html.erb b/app/views/pages/dashboard.html.erbindex bc10221689..af786a77b0 100644--- a/app/views/pages/dashboard.html.erb+++ b/app/views/pages/dashboard.html.erb@@ -29,7 +29,14 @@</div><% end %>-<div class="grid grid-cols-1 <%= "2xl:grid-cols-2" if Current.user.dashboard_two_column? %> gap-6 pb-6 lg:pb-12" data-controller="dashboard-sortable" data-action="dragover->dashboard-sortable#dragOver drop->dashboard-sortable#drop" role="list" aria-label="Dashboard sections">+<%= turbo_frame_tag "dashboard_sections", target: "_top" do %>+ <% if accessible_accounts.any? %>+ <div class="flex items-center justify-end mb-4">+ <%= render UI::PeriodPicker.new(selected: @period, url: root_path, frame: "dashboard_sections") %>+ </div>+ <% end %>++ <div class="grid grid-cols-1 <%= "2xl:grid-cols-2" if Current.user.dashboard_two_column? %> gap-6 pb-6 lg:pb-12" data-controller="dashboard-sortable" data-action="dragover->dashboard-sortable#dragOver drop->dashboard-sortable#drop" role="list" aria-label="Dashboard sections"><% if accessible_accounts.any? %><% @dashboard_sections.each do |section| %><% next unless section[:visible] %>@@ -96,4 +103,5 @@<%= render "pages/dashboard/no_accounts_graph_placeholder" %></section><% end %>-</div>+ </div>+<% end %>diff --git a/app/views/pages/dashboard/_cashflow_sankey.html.erb b/app/views/pages/dashboard/_cashflow_sankey.html.erbindex 201e0c8b98..2b91dbc728 100644--- a/app/views/pages/dashboard/_cashflow_sankey.html.erb+++ b/app/views/pages/dashboard/_cashflow_sankey.html.erb@@ -1,14 +1,5 @@<%# locals: (sankey_data:, period:) %><div id="cashflow-sankey-chart">- <div class="flex justify-end items-center gap-4 px-4 mb-4">- <%= form_with url: root_path, method: :get, data: { controller: "auto-submit-form", turbo_frame: "_top" } do |form| %>- <%= form.select :period,- Period.as_options,- { selected: period.key },- data: { "auto-submit-form-target": "auto" },- class: "bg-container border border-secondary font-medium rounded-lg px-3 py-2 text-sm pr-7 cursor-pointer text-primary focus:outline-hidden focus:ring-0" %>- <% end %>- </div><% if sankey_data[:links].present? %><div class="w-full h-96"><%= render "pages/dashboard/cashflow_sankey_chart", sankey_data: sankey_data %>diff --git a/app/views/pages/dashboard/_net_worth_chart.html.erb b/app/views/pages/dashboard/_net_worth_chart.html.erbindex 1a5f81d685..c9696f1ab2 100644--- a/app/views/pages/dashboard/_net_worth_chart.html.erb+++ b/app/views/pages/dashboard/_net_worth_chart.html.erb@@ -13,14 +13,6 @@<p class="text-sm text-secondary"><%= t(".data_not_available") %></p><% end %></div>-- <%= form_with url: root_path, method: :get, data: { controller: "auto-submit-form", turbo_frame: "_top" } do |form| %>- <%= form.select :period,- Period.as_options,- { selected: period.key },- data: { "auto-submit-form-target": "auto" },- class: "bg-container border border-secondary font-medium rounded-lg px-3 py-2 text-sm pr-7 cursor-pointer text-primary focus:outline-hidden focus:ring-0" %>- <% end %></div><% if series.any? %>diff --git a/app/views/pages/dashboard/_outflows_donut.html.erb b/app/views/pages/dashboard/_outflows_donut.html.erbindex 633689980a..2e430da536 100644--- a/app/views/pages/dashboard/_outflows_donut.html.erb+++ b/app/views/pages/dashboard/_outflows_donut.html.erb@@ -1,15 +1,5 @@<%# locals: (outflows_data:, period:) %><div id="outflows-donut-section" class="@container">- <div class="flex justify-end items-center gap-4 px-4 mb-4">- <%= form_with url: root_path, method: :get, data: { controller: "auto-submit-form", turbo_frame: "_top" } do |form| %>- <%= form.select :period,- Period.as_options,- { selected: period.key },- data: { "auto-submit-form-target": "auto" },- class: "bg-container border border-secondary font-medium rounded-lg px-3 py-2 text-sm pr-7 cursor-pointer text-primary focus:outline-hidden focus:ring-0" %>- <% end %>- </div>-<div class="px-4"><div class="flex flex-col @2xl:flex-row gap-8 items-center"><!-- Donut Chart -->diff --git a/config/locales/views/components/en.yml b/config/locales/views/components/en.ymlindex e0dfca1ab2..a8a96ad681 100644--- a/config/locales/views/components/en.yml+++ b/config/locales/views/components/en.yml@@ -1,6 +1,8 @@---en:UI:+ period_picker:+ aria_label: "Time period: %{period}"account:activity_feed:toggle_selection_checkboxes: Toggle selectiondiff --git a/test/components/UI/period_picker_test.rb b/test/components/UI/period_picker_test.rbnew file mode 100644index 0000000000..ee54db16e7--- /dev/null+++ b/test/components/UI/period_picker_test.rb@@ -0,0 +1,58 @@+require "test_helper"++class UI::PeriodPickerTest < ViewComponent::TestCase+ test "renders one menuitemradio link per period, all carrying the period param and target frame" do+ render_inline(UI::PeriodPicker.new(selected: "last_30_days", url: "/", frame: "dashboard_sections"))++ links = page.all("a[role='menuitemradio']")+ assert_equal Period.all.size, links.size++ links.each do |link|+ assert_match(/period=/, link[:href])+ assert_equal "dashboard_sections", link["data-turbo-frame"]+ end+ end++ test "marks the selected period with aria-checked and a check glyph" do+ render_inline(UI::PeriodPicker.new(selected: "last_90_days", url: "/", frame: "dashboard_sections"))++ selected = page.all("a[role='menuitemradio'][aria-checked='true']")+ assert_equal 1, selected.size+ assert_match(/period=last_90_days/, selected.first[:href])+ # Check icon (svg) renders inside the selected item only.+ assert_selector "a[aria-checked='true'] svg"+ end++ test "trigger button shows the selected period's short label" do+ render_inline(UI::PeriodPicker.new(selected: "last_30_days", url: "/"))++ assert_text Period.from_key("last_30_days").label_short+ end++ test "trigger accessible name announces the selected period" do+ render_inline(UI::PeriodPicker.new(selected: "last_90_days", url: "/"))++ label = Period.from_key("last_90_days").label_short+ assert_selector "button[aria-label='Time period: #{label}']"+ end++ test "extra_params are merged into every option href" do+ render_inline(UI::PeriodPicker.new(+ selected: "last_30_days",+ url: "/accounts/abc",+ extra_params: { chart_view: "balance" }+ ))++ href = page.first("a[role='menuitemradio']")[:href]+ assert_match(%r{\A/accounts/abc\?}, href)+ assert_match(/chart_view=balance/, href)+ assert_match(/period=/, href)+ end++ test "accepts a Period object as selected" do+ render_inline(UI::PeriodPicker.new(selected: Period.last_7_days, url: "/"))++ assert_text Period.from_key("last_7_days").label_short+ assert_equal 1, page.all("a[aria-checked='true']").size+ end+enddiff --git a/test/models/period_test.rb b/test/models/period_test.rbindex fa1221eb94..33e01bb02f 100644--- a/test/models/period_test.rb+++ b/test/models/period_test.rb@@ -63,6 +63,29 @@ class PeriodTest < ActiveSupport::TestCaseassert_equal Date.current, period.end_dateend+ test "current_month_for preserves the current_month key for custom-month families" do+ family = mock("family")+ family.stubs(:uses_custom_month_start?).returns(true)+ family.stubs(:current_custom_month_period).returns(+ Period.custom(start_date: Date.new(2026, 6, 5), end_date: Date.new(2026, 7, 4))+ )++ period = Period.current_month_for(family)+ assert_equal "current_month", period.key+ assert_equal Date.new(2026, 6, 5), period.start_date+ assert_equal Date.new(2026, 7, 4), period.end_date+ end++ test "last_month_for preserves the last_month key for custom-month families" do+ family = mock("family")+ family.stubs(:uses_custom_month_start?).returns(true)+ family.stubs(:custom_month_start_for).returns(Date.new(2026, 5, 5))+ family.stubs(:custom_month_end_for).returns(Date.new(2026, 6, 4))++ period = Period.last_month_for(family)+ assert_equal "last_month", period.key+ end+test "all_time period uses fallback when no family or entries exist" doCurrent.expects(:family).returns(nil)