Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Simplify the rule prompt and add a Rule suggestions setting (#4786)

adam2504264bbf7cb71d
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index bf6eefe334..d024b77b44 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -124,6 +124,7 @@ class UsersController < ApplicationController
params.require(:user).permit(
:first_name, :last_name, :email, :profile_image, :redirect_to, :delete_profile_image, :onboarded_at,
:show_sidebar, :default_period, :default_account_order, :show_ai_sidebar, :ai_enabled, :theme, :set_onboarding_preferences_at, :set_onboarding_goals_at, :locale,
+ :rule_prompts_disabled,
family_attributes: family_attrs,
goals: []
)
diff --git a/app/views/rules/_category_rule_cta.html.erb b/app/views/rules/_category_rule_cta.html.erb
index ee072a5da8..3ef86bbb7a 100644
--- a/app/views/rules/_category_rule_cta.html.erb
+++ b/app/views/rules/_category_rule_cta.html.erb
@@ -1,21 +1,23 @@
<%# locals: (cta:) %>
<% 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. %>
+<%# The X closes just this prompt. "Not today" snoozes rule prompts for 24 hours, the link turns them off. %>
<%= 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, 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: 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: t("rules.category_rule_cta.create_rule"), variant: "primary", href: rule_href, frame: :modal) %>
+ <div>
+ <%= form_with model: Current.user, url: rule_prompt_settings_user_path(Current.user), method: :patch do |f| %>
+ <%= f.hidden_field :rule_prompt_dismissed_at, value: Time.current %>
+ <%= tag.div class: "flex flex-wrap gap-2 justify-end" do %>
+ <%= render DS::Button.new(text: t("rules.category_rule_cta.not_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: t("rules.category_rule_cta.create_rule"), variant: "primary", href: rule_href, frame: :modal) %>
+ <% end %>
<% 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 %>
+ <%# A separate form, so turning suggestions off doesn't also start the snooze: turning them %>
+ <%# back on in Settings > Preferences, whose toggle this is named after, brings prompts back. %>
+ <%= form_with model: Current.user, url: rule_prompt_settings_user_path(Current.user), method: :patch, class: "flex justify-end mt-1" do |f| %>
+ <%= f.hidden_field :rule_prompts_disabled, value: true %>
+ <%= render DS::Button.new(text: t("rules.category_rule_cta.turn_off_suggestions"), variant: "ghost", size: :sm, type: :submit, class: "text-secondary") %>
+ <% end %>
+ </div>
<% end %>
diff --git a/app/views/settings/preferences/show.html.erb b/app/views/settings/preferences/show.html.erb
index a144c6dbae..ead2553844 100644
--- a/app/views/settings/preferences/show.html.erb
+++ b/app/views/settings/preferences/show.html.erb
@@ -26,6 +26,15 @@
{ data: { auto_submit_form_target: "auto" } } %>
<p class="text-xs italic pl-2 text-secondary"><%= t(".translations_notice") %></p>
<% end %>
+ <div class="flex items-center justify-between gap-4">
+ <div class="space-y-1">
+ <p class="text-sm" id="rule_suggestions_label"><%= t(".rule_suggestions") %></p>
+ <p class="text-secondary text-sm"><%= t(".rule_suggestions_hint") %></p>
+ </div>
+ <%= render DS::Toggle.new(id: "user_rule_suggestions", name: "user[rule_prompts_disabled]",
+ checked: [email protected]_prompts_disabled, checked_value: "false", unchecked_value: "true",
+ "aria-labelledby": "rule_suggestions_label", data: { auto_submit_form_target: "auto" }) %>
+ </div>
<% end %>
</div>
<% end %>
diff --git a/app/views/shared/notifications/_cta.html.erb b/app/views/shared/notifications/_cta.html.erb
index f7dcb2cff2..bd909c6a02 100644
--- a/app/views/shared/notifications/_cta.html.erb
+++ b/app/views/shared/notifications/_cta.html.erb
@@ -1,9 +1,9 @@
<%# 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 %>
+ <%= tag.div class: "relative flex flex-col gap-4 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. %>
+ <%# content stack so the stack's gaps can't push it out of the corner. %>
<% if close_label %>
<%= icon "x",
as_button: true,
@@ -14,19 +14,20 @@
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" %>
+ <%= tag.div class: class_names("flex gap-3", "pr-6": close_label.present?) do %>
+ <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>
- <%= tag.div class: class_names("space-y-4 min-w-0", "pr-6": close_label.present?) do %>
- <div class="space-y-1">
+ <div class="space-y-1 min-w-0">
<%= tag.p message, class: "text-primary text-sm font-medium" %>
<%= tag.p description, class: "text-secondary text-sm" %>
</div>
-
- <%= yield %>
<% end %>
+
+ <%# Actions span the full toast width, not just the text column, so longer labels stay on one row. %>
+ <%= yield %>
<% end %>
</div>
diff --git a/config/locales/views/rules/en.yml b/config/locales/views/rules/en.yml
index 04ea084d49..ca28860960 100644
--- a/config/locales/views/rules/en.yml
+++ b/config/locales/views/rules/en.yml
@@ -7,10 +7,9 @@ en:
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."
+ description: "Always categorize similar transactions?"
+ not_today: "Not today"
+ turn_off_suggestions: "Turn off rule suggestions"
message: Updated to %{category_name}
classifier_rules:
category_classifier: Category classifier
diff --git a/config/locales/views/rules/fr.yml b/config/locales/views/rules/fr.yml
index 0a15b3c3d2..91f7ed4b96 100644
--- a/config/locales/views/rules/fr.yml
+++ b/config/locales/views/rules/fr.yml
@@ -52,10 +52,9 @@ fr:
category_rule_cta:
close: Fermer
create_rule: Créer une règle
- description: Vous pouvez créer une règle pour catégoriser automatiquement les transactions comme celle-ci
- dont_show_again: Ne plus afficher
- hide_for_today: Masquer pour aujourd'hui
- hide_for_today_hint: Masquer pour aujourd'hui suspend ces suggestions pendant 24 heures.
+ description: "Toujours catégoriser les transactions similaires ?"
+ not_today: "Pas aujourd'hui"
+ turn_off_suggestions: "Désactiver les suggestions de règles"
message: Catégorie changée en %{category_name}
classifier_rules:
category_classifier: Classifieur de catégories
diff --git a/config/locales/views/rules/nl.yml b/config/locales/views/rules/nl.yml
index 57d82a6b23..cb6e0e1414 100644
--- a/config/locales/views/rules/nl.yml
+++ b/config/locales/views/rules/nl.yml
@@ -7,10 +7,9 @@ nl:
category_rule_cta:
close: "Sluiten"
create_rule: "Regel aanmaken"
- description: "U kunt een regel aanmaken om transacties zoals deze automatisch te categoriseren"
- dont_show_again: "Niet meer tonen"
- hide_for_today: "Vandaag verbergen"
- hide_for_today_hint: "Met Vandaag verbergen worden deze suggesties 24 uur gepauzeerd."
+ description: "Dit soort transacties altijd categoriseren?"
+ not_today: "Niet vandaag"
+ turn_off_suggestions: "Regelsuggesties uitschakelen"
message: "Bijgewerkt naar %{category_name}"
classifier_rules:
category_classifier: "Categorieclassificatie"
diff --git a/config/locales/views/rules/zh-CN.yml b/config/locales/views/rules/zh-CN.yml
index 45ee0d5174..34f794d666 100644
--- a/config/locales/views/rules/zh-CN.yml
+++ b/config/locales/views/rules/zh-CN.yml
@@ -8,9 +8,8 @@ zh-CN:
close: 关闭
create_rule: 创建规则
description: 您可以创建规则来自动分类类似于此交易的交易
- dont_show_again: 不再显示
- hide_for_today: 今日隐藏
- hide_for_today_hint: 今日隐藏将在 24 小时内暂停这些建议。
+ not_today: "今天不要"
+ turn_off_suggestions: "关闭规则建议"
message: 已更新为 %{category_name}
classifier_rules:
category_classifier: 交易分类器
diff --git a/config/locales/views/settings/preferences/en.yml b/config/locales/views/settings/preferences/en.yml
index 36255672e3..4060d0caca 100644
--- a/config/locales/views/settings/preferences/en.yml
+++ b/config/locales/views/settings/preferences/en.yml
@@ -16,6 +16,8 @@ en:
page_title: Preferences
timezone: Timezone
translations_notice: Please note, we are still working on translations for various languages.
+ rule_suggestions: "Rule suggestions"
+ rule_suggestions_hint: "Offer to create a rule after you change a transaction's category"
budget_title: Budget
budget_subtitle: Configure how your budgets behave
month_start_day: Budget month starts on
diff --git a/config/locales/views/settings/preferences/fr.yml b/config/locales/views/settings/preferences/fr.yml
index c4abb20f03..6cd38fa7a1 100644
--- a/config/locales/views/settings/preferences/fr.yml
+++ b/config/locales/views/settings/preferences/fr.yml
@@ -57,6 +57,8 @@ fr:
sharing_title: Partage de %{moniker}
timezone: Fuseau horaire
translations_notice: Veuillez noter que nous travaillons toujours sur des traductions dans différentes langues.
+ rule_suggestions: "Suggestions de règles"
+ rule_suggestions_hint: "Proposer de créer une règle après un changement de catégorie"
default_account_order_hint: Avec Manuel, faites glisser les comptes dans la barre latérale pour définir leur ordre.
investment_contributions_as_spending: Compter l'investissement comme une dépense
investment_contributions_as_spending_hint: L'argent versé sur des comptes d'investissement compte comme une dépense dans les budgets, les rapports et le graphique des flux de trésorerie. Désactivez cette option pour l'afficher plutôt comme investi.
diff --git a/config/locales/views/settings/preferences/nl.yml b/config/locales/views/settings/preferences/nl.yml
index c05ed08d43..616b0265b8 100644
--- a/config/locales/views/settings/preferences/nl.yml
+++ b/config/locales/views/settings/preferences/nl.yml
@@ -16,6 +16,8 @@ nl:
page_title: "Voorkeuren"
timezone: "Tijdzone"
translations_notice: "Let op: we werken nog aan vertalingen voor verschillende talen."
+ rule_suggestions: "Regelsuggesties"
+ rule_suggestions_hint: "Voorstellen een regel te maken nadat je een categorie wijzigt"
budget_title: "Budget"
budget_subtitle: "Stel in hoe uw budgetten werken"
month_start_day: "Budgetmaand begint op"
diff --git a/test/system/category_rule_prompt_test.rb b/test/system/category_rule_prompt_test.rb
index d8dfa25bbd..93bb33eb43 100644
--- a/test/system/category_rule_prompt_test.rb
+++ b/test/system/category_rule_prompt_test.rb
@@ -25,7 +25,7 @@ class CategoryRulePromptTest < ApplicationSystemTestCase
within "#cta" do
assert_text "Updated to #{@categories.first.name}"
- assert_text "Hide for today pauses these suggestions for 24 hours."
+ assert_text "Always categorize similar transactions?"
end
# The X sits in the toast's top-right corner (not pushed down by the content stack).
@@ -49,11 +49,11 @@ class CategoryRulePromptTest < ApplicationSystemTestCase
assert_selector "#cta", text: "Updated to #{@categories.second.name}"
end
- test "hide for today snoozes rule prompts" do
+ test "not today snoozes rule prompts" do
visit transactions_url
assign_category @categories.first
- within("#cta") { click_button "Hide for today" }
+ within("#cta") { click_button "Not today" }
assert_no_text "Updated to #{@categories.first.name}"
assert_not_nil @user.reload.rule_prompt_dismissed_at
@@ -62,6 +62,61 @@ class CategoryRulePromptTest < ApplicationSystemTestCase
# While snoozed, the next eligible category change doesn't prompt either.
assign_category @categories.second
assert_no_text "Updated to #{@categories.second.name}"
+
+ # Once the snooze is over a day old, prompts come back.
+ @user.update!(rule_prompt_dismissed_at: 25.hours.ago)
+ assign_category @categories.first
+ assert_selector "#cta", text: "Updated to #{@categories.first.name}"
+ end
+
+ test "the prompt's buttons stay inside the toast when their labels are long" do
+ # French has the longest labels of the shipped locales, and the desktop tray
+ # is only 20rem wide, so the button row has to wrap rather than overflow.
+ # A user-named category: system names such as "Income" are shown
+ # translated, so assign_category could not search for them in French.
+ category = @user.family.categories.create!(name: "Layout check", color: "#123456")
+ @user.update!(locale: "fr")
+ visit transactions_url
+ assign_category category
+ assert_selector "#cta form button", text: "Pas aujourd'hui"
+ assert_selector "#cta form a", text: "Créer une règle"
+
+ overflow = page.evaluate_script(<<~JS)
+ (() => {
+ const toast = document.querySelector("#cta > div").getBoundingClientRect();
+ return [...document.querySelectorAll("#cta form a, #cta form button")]
+ .map((el) => el.getBoundingClientRect())
+ .filter((r) => r.left < toast.left - 0.5 || r.right > toast.right + 0.5).length;
+ })()
+ JS
+ assert_equal 0, overflow, "every button must stay inside the toast"
+ end
+
+ test "turning rule suggestions off from the prompt can be undone in preferences" do
+ visit transactions_url
+ assign_category @categories.first
+
+ within("#cta") { click_button "Turn off rule suggestions" }
+
+ assert_no_text "Updated to #{@categories.first.name}"
+ assert @user.reload.rule_prompts_disabled
+
+ assign_category @categories.second
+ assert_no_text "Updated to #{@categories.second.name}"
+
+ visit settings_preferences_url
+ assert_not find("#user_rule_suggestions", visible: :all).checked?
+ find("label[for='user_rule_suggestions']").click
+ assert_selector "#user_rule_suggestions:checked", visible: :all
+ # The preferences form saves itself after the change, so give it a moment.
+ Timeout.timeout(5) { sleep 0.1 while @user.reload.rule_prompts_disabled }
+ assert_not @user.rule_prompts_disabled
+ assert_nil @user.rule_prompt_dismissed_at, "turning suggestions off must not start the 24-hour snooze"
+
+ # Back on, suggestions show again right away.
+ visit transactions_url
+ assign_category @categories.first
+ assert_selector "#cta", text: "Updated to #{@categories.first.name}"
end
test "a prompt from the transaction drawer waits until the drawer closes" do