The personal finance app for everyone (by everyone)
Ruby
fix(rules): stop "Dismiss" on the rule prompt silently snoozing all prompts (#3773)
scragnog1d64efef67c5
diff --git a/app/views/rules/_category_rule_cta.html.erb b/app/views/rules/_category_rule_cta.html.erbindex 3c7f2ed417..ee072a5da8 100644--- a/app/views/rules/_category_rule_cta.html.erb+++ b/app/views/rules/_category_rule_cta.html.erb@@ -1,21 +1,21 @@<%# locals: (cta:) %>--<% message = "Updated to #{cta[:category_name]}" %>-<% description = "You can create a rule to automatically categorize transactions like this one" %>--<%= render "shared/notifications/cta", message: message, description: description do %>+<% message = t(".message", category_name: cta[:category_name]) %>+<% description = t(".description") %>+<%# The X closes just this prompt; "Hide for today" is the one that snoozes prompts. %>+<%= render "shared/notifications/cta", message: message, description: description, close_label: t(".close") do %>+ <%# Full i18n keys inside this block: lazy t(".x") would resolve against the shared cta partial. %><%= form_with model: Current.user, url: rule_prompt_settings_user_path(Current.user), method: :patch do |f| %><div class="flex gap-2 items-center mb-3 -mt-1"><%= f.check_box :rule_prompts_disabled, class: "checkbox checkbox--light" %>- <%= f.label :rule_prompts_disabled, "Don't show this again", class: "text-xs text-secondary" %>+ <%= f.label :rule_prompts_disabled, t("rules.category_rule_cta.dont_show_again"), class: "text-xs text-secondary" %></div>-<%= f.hidden_field :rule_prompt_dismissed_at, value: Time.current %>-<%= tag.div class:"flex gap-2 justify-end" do %>- <%= render DS::Button.new(text: "Dismiss", variant: "secondary", type: :submit) %>+ <%= render DS::Button.new(text: t("rules.category_rule_cta.hide_for_today"), variant: "secondary", type: :submit) %><% rule_href = new_rule_path(resource_type: "transaction", action_type: "set_transaction_category", action_value: cta[:category_id], name: cta[:merchant_name]) %>- <%= render DS::Link.new(text: "Create rule", variant: "primary", href: rule_href, frame: :modal) %>+ <%= render DS::Link.new(text: t("rules.category_rule_cta.create_rule"), variant: "primary", href: rule_href, frame: :modal) %><% end %>+ <%# Visible, not a title tooltip, so touch and keyboard users see it too. %>+ <p class="mt-2 text-xs text-secondary text-right"><%= t("rules.category_rule_cta.hide_for_today_hint") %></p><% end %><% end %>diff --git a/app/views/shared/notifications/_cta.html.erb b/app/views/shared/notifications/_cta.html.erbindex 9232ac591f..f7dcb2cff2 100644--- a/app/views/shared/notifications/_cta.html.erb+++ b/app/views/shared/notifications/_cta.html.erb@@ -1,20 +1,32 @@-<%# locals: (message:, description:) %>+<%# locals: (message:, description:, close_label: nil) %><div id="cta"><%= tag.div class: "relative flex gap-3 rounded-lg bg-container p-4 group w-full md:max-w-80 shadow-border-xs", data: { controller: "element-removal" } do %>+ <%# Pass close_label for a plain "close this notification" control. It sits outside the %>+ <%# content's space-y stack so the stack's margins can't push it out of the corner. %>+ <% if close_label %>+ <%= icon "x",+ as_button: true,+ size: "sm",+ class: "absolute top-2 right-2",+ title: close_label,+ aria: { label: close_label },+ data: { action: "element-removal#remove" } %>+ <% end %>+<div class="h-5 w-5 shrink-0 p-px text-primary"><div class="flex h-full items-center justify-center rounded-full bg-success text-inverse"><%= icon "check", size: "xs", color: "current" %></div></div>- <div class="space-y-4">+ <%= tag.div class: class_names("space-y-4 min-w-0", "pr-6": close_label.present?) do %><div class="space-y-1"><%= tag.p message, class: "text-primary text-sm font-medium" %><%= tag.p description, class: "text-secondary text-sm" %></div><%= yield %>- </div>+ <% end %><% end %></div>diff --git a/config/locales/views/rules/en.yml b/config/locales/views/rules/en.ymlindex c2f75cd2dc..b5fe1b1dab 100644--- a/config/locales/views/rules/en.yml+++ b/config/locales/views/rules/en.yml@@ -3,6 +3,14 @@ en:rules:no_action: No Actionno_condition: No condition+ category_rule_cta:+ close: Close+ create_rule: Create rule+ description: You can create a rule to automatically categorize transactions like this one+ dont_show_again: Don't show this again+ hide_for_today: Hide for today+ hide_for_today_hint: "Hide for today pauses these suggestions for 24 hours."+ message: Updated to %{category_name}rule:edit: Editre_apply_rule: Re-apply rulediff --git a/test/system/category_rule_prompt_test.rb b/test/system/category_rule_prompt_test.rbnew file mode 100644index 0000000000..af0be20a33--- /dev/null+++ b/test/system/category_rule_prompt_test.rb@@ -0,0 +1,80 @@+require "application_system_test_case"++class CategoryRulePromptTest < ApplicationSystemTestCase+ include ActionView::RecordIdentifier++ setup do+ sign_in @user = users(:family_admin)+ @user.update!(rule_prompts_disabled: false, rule_prompt_dismissed_at: nil)+ @entry = @user.family.entries.transactions.order(date: :desc).first+ ruled_category_ids = Rule::Action.joins(:rule)+ .where(rules: { family_id: @user.family_id }, action_type: "set_transaction_category")+ .pluck(:value)+ # Filtered in Ruby: rule action values are free-form strings, and a non-UUID one+ # would turn a SQL NOT IN into NOT IN (NULL), which matches nothing.+ @categories = @user.family.categories.alphabetically.reject do |category|+ category.id.in?(ruled_category_ids) || category.id == @entry.entryable.category_id+ end.first(2)+ assert_equal 2, @categories.size, "fixtures need two categories without a rule"+ page.current_window.resize_to(1280, 900)+ end++ test "closing the prompt hides only that prompt and does not snooze future ones" do+ visit transactions_url+ assign_category @categories.first++ within "#cta" do+ assert_text "Updated to #{@categories.first.name}"+ assert_text "Hide for today pauses these suggestions for 24 hours."+ end++ # The X sits in the toast's top-right corner (not pushed down by the content stack).+ offsets = page.evaluate_script(<<~JS)+ (() => {+ const toast = document.querySelector("#cta > div");+ const close = toast.querySelector("button[aria-label='Close']");+ const t = toast.getBoundingClientRect(), c = close.getBoundingClientRect();+ return { top: c.top - t.top, right: t.right - c.right };+ })()+ JS+ assert_operator offsets["top"], :<=, 12+ assert_operator offsets["right"], :<=, 12++ within("#cta") { click_button "Close" }++ assert_no_text "Updated to #{@categories.first.name}"+ assert_nil @user.reload.rule_prompt_dismissed_at++ assign_category @categories.second+ assert_selector "#cta", text: "Updated to #{@categories.second.name}"+ end++ test "hide for today snoozes rule prompts" do+ visit transactions_url+ assign_category @categories.first++ within("#cta") { click_button "Hide for today" }++ assert_no_text "Updated to #{@categories.first.name}"+ assert_not_nil @user.reload.rule_prompt_dismissed_at+ assert_not @user.rule_prompts_disabled++ # While snoozed, the next eligible category change doesn't prompt either.+ assign_category @categories.second+ assert_no_text "Updated to #{@categories.second.name}"+ end++ private+ def assign_category(category)+ within "##{dom_id(@entry.entryable, 'category_menu_desktop')}" do+ find("button", match: :first).click+ end++ within "turbo-frame#category_dropdown" do+ find("input[type='search']").fill_in with: category.name+ find("##{dom_id(category, 'category_option')} button", match: :first).click+ end++ assert_selector "##{dom_id(@entry.entryable, 'category_menu_desktop')} [data-testid='category-name']", text: category.name+ end+end