The personal finance app for everyone (by everyone)
Ruby
feat(dashboard): make the cashflow preview chart opt-in (#4540)
gus3298245a1a78
diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rbindex 14678bdda5..5403a16731 100644--- a/app/controllers/pages_controller.rb+++ b/app/controllers/pages_controller.rb@@ -62,8 +62,15 @@ class PagesController < ApplicationControllerenddef update_preferences- if Current.user.update_dashboard_preferences(preferences_params, laid_out_order: ordered_dashboard_section_keys)- head :ok+ saved = Current.user.update_dashboard_preferences(preferences_params, laid_out_order: ordered_dashboard_section_keys)++ # Grouping, collapse, and layout save with a JSON PATCH and expect an empty+ # 200. The comparison toggle is an HTML form so the dashboard can re-render+ # the chart that the preference reveals.+ if dashboard_preference_json_request?+ head(saved ? :ok : :unprocessable_entity)+ elsif saved+ redirect_back fallback_location: root_path, status: :see_otherelsehead :unprocessable_entityend@@ -129,9 +136,16 @@ class PagesController < ApplicationControllerpermitted["section_order"] = prefs[:section_order] if prefs[:section_order].is_a?(Array)permitted["dashboard_section_layout"] = prefs[:dashboard_section_layout].to_unsafe_h if prefs[:dashboard_section_layout].respond_to?(:to_unsafe_h)permitted["cashflow_sankey_group_by"] = prefs[:cashflow_sankey_group_by] if User::CASHFLOW_SANKEY_GROUPINGS.include?(prefs[:cashflow_sankey_group_by])+ permitted["cashflow_preview_enabled"] = ActiveModel::Type::Boolean.new.cast(prefs[:cashflow_preview_enabled]) if prefs.key?(:cashflow_preview_enabled)endend+ # Fetch callers send JSON without an Accept header, which Rails would+ # otherwise treat as HTML and redirect.+ def dashboard_preference_json_request?+ request.format.json? || request.media_type == "application/json"+ end+# The spending filter is carried as the widget resolved it: a selection# that covers every category with spending is the unfiltered state, so it# is dropped here rather than re-sent by every other control, where itdiff --git a/app/models/user.rb b/app/models/user.rbindex cccf97f414..62dafc8c1c 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -807,6 +807,13 @@ class User < ApplicationRecordpreferences&.dig("preview_features_enabled") == trueend+ # The experimental Sankey comparison chart (and its feedback prompt) is+ # opt-in on top of preview features, so a user can keep preview features+ # enabled without a second chart rendering underneath the cash flow widget.+ def cashflow_preview_enabled?+ preferences&.dig("cashflow_preview_enabled") == true+ end+privatedef apply_ui_layout_defaultsself.ui_layout = (ui_layout.presence || self.class.default_ui_layout)diff --git a/app/views/pages/dashboard.html.erb b/app/views/pages/dashboard.html.erbindex 4286f9b319..e4850ca484 100644--- a/app/views/pages/dashboard.html.erb+++ b/app/views/pages/dashboard.html.erb@@ -259,8 +259,13 @@</div><div class="py-4" data-dashboard-section-target="content"><%= render partial: section[:partial], locals: section[:locals] %>+ <%# Preview features reveal the opt-in. The comparison chart itself+ stays hidden until this user turns that preference on. %><% if section[:key] == "cashflow_sankey" && preview_features_enabled? %>- <%= render "pages/dashboard/cashflow_preview", period: @period, legacy_data: section[:locals][:sankey_data] %>+ <%= render "pages/dashboard/cashflow_preview_opt_in" %>+ <% if Current.user.cashflow_preview_enabled? %>+ <%= render "pages/dashboard/cashflow_preview", period: @period, legacy_data: section[:locals][:sankey_data] %>+ <% end %><% end %></div></section>diff --git a/app/views/pages/dashboard/_cashflow_preview.html.erb b/app/views/pages/dashboard/_cashflow_preview.html.erbindex 71bf2d183d..08f248ab40 100644--- a/app/views/pages/dashboard/_cashflow_preview.html.erb+++ b/app/views/pages/dashboard/_cashflow_preview.html.erb@@ -3,7 +3,7 @@<% if feedback[:api_key].present? %><%= render "shared/posthog_bootstrap" %><% end %>-<section id="cashflow-preview" class="mt-6 border-t border-primary pt-4 space-y-4 ph-no-capture group/sankey-preview"+<section id="cashflow-preview" class="mt-4 space-y-4 ph-no-capture group/sankey-preview"data-controller="sankey-preview"data-sankey-preview-sure-version-value="<%= Sure.version.to_s %>"data-sankey-preview-legacy-data-value="<%= legacy_data.to_json %>"diff --git a/app/views/pages/dashboard/_cashflow_preview_opt_in.html.erb b/app/views/pages/dashboard/_cashflow_preview_opt_in.html.erbnew file mode 100644index 0000000000..cd5b383d9f--- /dev/null+++ b/app/views/pages/dashboard/_cashflow_preview_opt_in.html.erb@@ -0,0 +1,19 @@+<%# Posts to the same dashboard preferences endpoint as Sankey grouping.+ Auto-submit matches the Settings preference toggles; the HTML response+ redirects back so this section re-renders with or without the chart. %>+<%= form_with url: dashboard_preferences_path, method: :patch,+ class: "mt-4 border-t border-primary px-4 pt-4",+ data: { controller: "auto-submit-form" } do %>+ <label for="cashflow_preview_enabled" class="flex cursor-pointer items-center gap-4 justify-between">+ <div class="text-sm space-y-1">+ <p class="text-primary font-medium"><%= t("pages.dashboard.cashflow_preview.opt_in_title") %></p>+ <p class="text-secondary"><%= t("pages.dashboard.cashflow_preview.opt_in_description") %></p>+ </div>+ <%= render DS::Toggle.new(+ id: "cashflow_preview_enabled",+ name: "preferences[cashflow_preview_enabled]",+ checked: Current.user.cashflow_preview_enabled?,+ data: { auto_submit_form_target: "auto", autosubmit_debounce_timeout: "0" }+ ) %>+ </label>+<% end %>diff --git a/config/locales/views/pages/en.yml b/config/locales/views/pages/en.ymlindex 78f9757764..68200a55e9 100644--- a/config/locales/views/pages/en.yml+++ b/config/locales/views/pages/en.yml@@ -87,6 +87,8 @@ en:insights_feed:title: "Insights"cashflow_preview:+ opt_in_title: "Show the new chart"+ opt_in_description: "Compare an experimental Sankey with the chart above. You can turn this off at any time."node_labels:cash_flow: "Cash Flow"surplus: "Surplus"diff --git a/test/controllers/cash_flow_preview_test.rb b/test/controllers/cash_flow_preview_test.rbindex 678adbf290..82bf4bdcfe 100644--- a/test/controllers/cash_flow_preview_test.rb+++ b/test/controllers/cash_flow_preview_test.rb@@ -15,10 +15,18 @@ class CashFlowPreviewTest < ActionDispatch::IntegrationTestlegacy = css_select("[data-controller='sankey-chart']").map { |chart| chart["data-sankey-chart-data-value"] }assert_equal 2, legacy.length+ # Preview features alone no longer render the comparison chart: it takes the+ # explicit per-user opt-in on top of them.@user.update!(preferences: @user.preferences.merge("preview_features_enabled" => true))get root_pathassert_response :success- assert_select "#cashflow-sankey-chart + #cashflow-preview", count: 1+ assert_select "#cashflow-preview", count: 0++ enable_preview_chart++ get root_path+ assert_response :success+ assert_select "#cashflow-sankey-chart + form[action='#{dashboard_preferences_path}'] + #cashflow-preview", count: 1assert_select "#cashflow-preview[data-sankey-preview-sure-version-value=?]", Rails.root.join(".sure-version").read.stripassert_select "#cashflow-preview [data-controller='preview-sankey-chart']", count: 2assert_select "#cashflow-preview [data-cash-flow-url-value*='/dashboard/cash_flow?']", count: 1@@ -27,14 +35,16 @@ class CashFlowPreviewTest < ActionDispatch::IntegrationTesttest "a family member's preview opt-in does not expose the preview to this user" do@user.update!(preferences: @user.preferences.merge("preview_features_enabled" => false))- @user.family.users.where.not(id: @user.id).first.update!(preferences: { "preview_features_enabled" => true })+ enable_preview_chart(@user.family.users.where.not(id: @user.id).first)+get root_path+assert_response :successassert_select "#cashflow-preview", count: 0endtest "self-hosted production preview needs no operator key or survey configuration" do- @user.update!(preferences: @user.preferences.merge("preview_features_enabled" => true))+ enable_preview_chartconfig = Rails.configuration.x.posthogconfig.stubs(:api_key).returns(nil)stub_managed_survey(nil)@@ -56,7 +66,7 @@ class CashFlowPreviewTest < ActionDispatch::IntegrationTestendtest "self-hosted development renders bundled feedback only with testing override" do- @user.update!(preferences: @user.preferences.merge("preview_features_enabled" => true))+ enable_preview_chartconfig = Rails.configuration.x.posthogconfig.stubs(:api_key).returns(nil)config.stubs(:feedback_enabled).returns(true)@@ -87,7 +97,7 @@ class CashFlowPreviewTest < ActionDispatch::IntegrationTestendtest "managed app and demo use the configured environment survey without the shared feedback client" do- @user.update!(preferences: @user.preferences.merge("preview_features_enabled" => true))+ enable_preview_chartRails.configuration.stubs(:app_mode).returns("managed".inquiry)Rails.env.stubs(:production?).returns(true)[ "app-survey", "demo-survey" ].each do |survey_id|@@ -98,6 +108,15 @@ class CashFlowPreviewTest < ActionDispatch::IntegrationTestendprivate+ # The experimental Sankey comparison chart is opt-in on top of preview+ # features, so it renders only when the user has both enabled.+ def enable_preview_chart(user = @user)+ user.update!(preferences: (user.preferences || {}).merge(+ "preview_features_enabled" => true,+ "cashflow_preview_enabled" => true+ ))+ end+def stub_managed_survey(survey_id)config = Rails.configuration.x.posthogsurveys = config.feedback_surveysdiff --git a/test/controllers/pages_controller_test.rb b/test/controllers/pages_controller_test.rbindex d744d61e86..2846a94efa 100644--- a/test/controllers/pages_controller_test.rb+++ b/test/controllers/pages_controller_test.rb@@ -1224,7 +1224,84 @@ class PagesControllerTest < ActionDispatch::IntegrationTestassert_redirected_to root_path(spending_month: selected_month.iso8601, customize: true, hidden_section: "spending_trend")end+ test "dashboard hides the experimental Sankey comparison unless the user opts in" do+ @user.update!(preferences: (@user.preferences || {}).merge("preview_features_enabled" => true))++ get root_path++ assert_response :ok+ assert_select "section[data-section-key='cashflow_sankey']"+ assert_select "#cashflow-preview", count: 0+ assert_cashflow_preview_opt_in checked: false+ end++ test "dashboard renders the experimental Sankey comparison once the user opts in" do+ @user.update!(preferences: (@user.preferences || {}).merge(+ "preview_features_enabled" => true,+ "cashflow_preview_enabled" => true+ ))++ get root_path++ assert_response :ok+ assert_select "#cashflow-preview"+ assert_cashflow_preview_opt_in checked: true+ end++ test "dashboard hides the Sankey comparison opt-in without preview features" do+ @user.update!(preferences: (@user.preferences || {}).merge(+ "preview_features_enabled" => false,+ "cashflow_preview_enabled" => true+ ))++ get root_path++ assert_response :ok+ assert_select "#cashflow-preview", count: 0+ assert_select "input[name='preferences[cashflow_preview_enabled]']", count: 0+ end++ test "update_preferences persists the Sankey comparison opt-in" do+ patch "/dashboard/preferences", params: { preferences: { cashflow_preview_enabled: true } }, as: :json+ assert_response :ok+ assert_equal true, @user.reload.cashflow_preview_enabled?++ patch "/dashboard/preferences", params: { preferences: { cashflow_preview_enabled: false } }, as: :json+ assert_response :ok+ assert_equal false, @user.reload.cashflow_preview_enabled?+ end++ test "an html Sankey comparison toggle redirects back to the dashboard" do+ patch "/dashboard/preferences",+ params: { preferences: { cashflow_preview_enabled: "1" } },+ headers: { "HTTP_REFERER" => root_url(period: "last_30_days") }++ assert_redirected_to root_url(period: "last_30_days")+ assert_equal true, @user.reload.cashflow_preview_enabled?++ patch "/dashboard/preferences",+ params: { preferences: { cashflow_preview_enabled: "0" } },+ headers: { "HTTP_REFERER" => root_url(period: "last_30_days") }++ assert_redirected_to root_url(period: "last_30_days")+ assert_equal false, @user.reload.cashflow_preview_enabled?+ end+private+ def assert_cashflow_preview_opt_in(checked:)+ assert_select "form[action='#{dashboard_preferences_path}'][data-controller='auto-submit-form']" do+ assert_select "label[for='cashflow_preview_enabled']", text: /#{Regexp.escape(I18n.t("pages.dashboard.cashflow_preview.opt_in_title"))}/+ assert_select "input[type=checkbox][name='preferences[cashflow_preview_enabled]'][role=switch][data-auto-submit-form-target=auto]" do |inputs|+ assert_equal 1, inputs.size+ if checked+ assert inputs.first["checked"].present?+ else+ assert_nil inputs.first["checked"]+ end+ end+ end+ end+def money_flow_barsJSON.parse(css_select("[data-controller='bar-chart']").first["data-bar-chart-data-value"])enddiff --git a/test/system/cash_flow_group_by_test.rb b/test/system/cash_flow_group_by_test.rbindex db48831bce..6c0e72b959 100644--- a/test/system/cash_flow_group_by_test.rb+++ b/test/system/cash_flow_group_by_test.rb@@ -6,7 +6,7 @@ class CashFlowGroupByTest < ApplicationSystemTestCasesetup do@user = users(:family_admin)@month = Date.current.beginning_of_month- @user.update!(preferences: @user.preferences.merge("preview_features_enabled" => true))+ @user.update!(preferences: @user.preferences.merge("preview_features_enabled" => true, "cashflow_preview_enabled" => true))category = @user.family.categories.create!(name: "Group By Shopping", color: "#123456")account = @user.family.accounts.create!(name: "Group By Checking", currency: "USD", balance: 0, accountable: Depository.new)create_transaction(account: account, category: category, amount: 100, date: @month)diff --git a/test/system/cash_flow_preview_opt_in_test.rb b/test/system/cash_flow_preview_opt_in_test.rbnew file mode 100644index 0000000000..cd36a421e6--- /dev/null+++ b/test/system/cash_flow_preview_opt_in_test.rb@@ -0,0 +1,28 @@+require "application_system_test_case"++class CashFlowPreviewOptInTest < ApplicationSystemTestCase+ setup do+ @user = users(:family_admin)+ @user.update!(preferences: (@user.preferences || {}).merge(+ "preview_features_enabled" => true,+ "cashflow_preview_enabled" => false+ ))+ end++ test "a preview user turns the cashflow comparison on and off from the dashboard" do+ sign_in @user+ visit root_path++ assert_no_selector "#cashflow-preview"+ opt_in = find("label[for='cashflow_preview_enabled']", text: I18n.t("pages.dashboard.cashflow_preview.opt_in_title"))+ opt_in.click++ assert_selector "#cashflow-preview"+ assert @user.reload.cashflow_preview_enabled?++ find("label[for='cashflow_preview_enabled']", text: I18n.t("pages.dashboard.cashflow_preview.opt_in_title")).click++ assert_no_selector "#cashflow-preview"+ assert_not @user.reload.cashflow_preview_enabled?+ end+enddiff --git a/test/system/cash_flow_sort_test.rb b/test/system/cash_flow_sort_test.rbindex bd8ff7faff..4d2d665327 100644--- a/test/system/cash_flow_sort_test.rb+++ b/test/system/cash_flow_sort_test.rb@@ -5,7 +5,7 @@ class CashFlowSortTest < ApplicationSystemTestCasesetup dosign_in @user = users(:family_admin)- @user.update!(preferences: @user.preferences.merge("preview_features_enabled" => true))+ @user.update!(preferences: @user.preferences.merge("preview_features_enabled" => true, "cashflow_preview_enabled" => true))@month = Date.current.beginning_of_monthaccount = @user.family.accounts.first{ "Sort Small" => 30, "Sort Large" => 3000, "Sort Medium" => 300 }.each do |name, amount|diff --git a/test/system/cash_flow_test.rb b/test/system/cash_flow_test.rbindex 0e05196aa6..1b445d4768 100644--- a/test/system/cash_flow_test.rb+++ b/test/system/cash_flow_test.rb@@ -6,7 +6,7 @@ class CashFlowTest < ApplicationSystemTestCasesetup do@user = users(:family_admin)@month = Date.current.beginning_of_month- @user.update!(preferences: @user.preferences.merge("preview_features_enabled" => true))+ @user.update!(preferences: @user.preferences.merge("preview_features_enabled" => true, "cashflow_preview_enabled" => true))parent = @user.family.categories.create!(name: "Sankey Shopping", color: "#123456")child = @user.family.categories.create!(name: "Sankey Groceries", parent: parent)account = @user.family.accounts.create!(name: "Sankey checking", currency: "USD", balance: 0, accountable: Depository.new)