The personal finance app for everyone (by everyone)
Ruby
feat(transactions): surface recently-used categories in the category picker (#2829)
Guillem Arias Fauste23e7db4f4d80
diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rbindex 2f1a1cbaa8..90cb3bd9a7 100644--- a/app/controllers/categories_controller.rb+++ b/app/controllers/categories_controller.rb@@ -29,7 +29,10 @@ class CategoriesController < ApplicationController@category = Current.family.categories.new(category_params)if @category.save- @transaction.update(category_id: @category.id) if @transaction+ if @transaction+ @transaction.update(category_id: @category.id)+ @transaction.record_category_usage!+ endflash[:notice] = t(".success")diff --git a/app/controllers/category/dropdowns_controller.rb b/app/controllers/category/dropdowns_controller.rbindex b8d8e69aa6..98a75b7ee9 100644--- a/app/controllers/category/dropdowns_controller.rb+++ b/app/controllers/category/dropdowns_controller.rb@@ -3,6 +3,7 @@ class Category::DropdownsController < ApplicationControllerdef show@categories = categories_scope.to_a.excluding(@selected_category).prepend(@selected_category).compact+ @recent_categories = Category.recently_used_for(family: Current.family, excluding: @selected_category).to_aendprivatediff --git a/app/controllers/transaction_categories_controller.rb b/app/controllers/transaction_categories_controller.rbindex d9daf436aa..95c5dd7314 100644--- a/app/controllers/transaction_categories_controller.rb+++ b/app/controllers/transaction_categories_controller.rb@@ -9,6 +9,8 @@ class TransactionCategoriesController < ApplicationControllertransaction = @entry.transaction+ transaction.record_category_usage!+if needs_rule_notification?(transaction)flash[:cta] = {type: "category_rule",@@ -54,6 +56,7 @@ class TransactionCategoriesController < ApplicationControllerdef needs_rule_notification?(transaction)return false if Current.user.rule_prompts_disabled+ return false if transaction.category_id.blank?if Current.user.rule_prompt_dismissed_at.present?time_since_last_rule_prompt = Time.current - Current.user.rule_prompt_dismissed_atdiff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rbindex b57a106547..49a61b50bb 100644--- a/app/controllers/transactions_controller.rb+++ b/app/controllers/transactions_controller.rb@@ -124,6 +124,7 @@ class TransactionsController < ApplicationControllerdef updateif @entry.update(permitted_entry_params)transaction = @entry.transaction+ transaction.record_category_usage!if needs_rule_notification?(transaction)flash[:cta] = {diff --git a/app/javascript/controllers/list_filter_controller.js b/app/javascript/controllers/list_filter_controller.jsindex c6938a4178..b012f26e56 100644--- a/app/javascript/controllers/list_filter_controller.js+++ b/app/javascript/controllers/list_filter_controller.js@@ -2,7 +2,7 @@ import { Controller } from "@hotwired/stimulus";// Basic functionality to filter a list based on a provided text attribute.export default class extends Controller {- static targets = ["input", "list", "emptyMessage"];+ static targets = ["input", "list", "emptyMessage", "recentSection"];connect() {this.inputTarget.focus();@@ -15,11 +15,29 @@ export default class extends Controller {const items = this.listTarget.querySelectorAll(".filterable-item");let noMatchFound = true;+ // "Recent" is a pre-search shortcut only — once the user is actively+ // searching, show canonical filtered results, not a duplicate row. Its+ // items are hidden outright (not text-matched) so the now-ancestor-hidden+ // section can't leave a row with display:"" that arrow-key nav would+ // still treat as visible.+ const recentItems = this.hasRecentSectionTarget+ ? new Set(this.recentSectionTarget.querySelectorAll(".filterable-item"))+ : new Set();++ if (this.hasRecentSectionTarget) {+ this.recentSectionTarget.classList.toggle("hidden", filterValue.length > 0);+ }+if (this.hasEmptyMessageTarget) {this.emptyMessageTarget.classList.add("hidden");}items.forEach((item) => {+ if (filterValue.length > 0 && recentItems.has(item)) {+ item.style.display = "none";+ return;+ }+const text = item.getAttribute("data-filter-name").toLowerCase();const shouldDisplay = text.includes(filterValue);item.style.display = shouldDisplay ? "" : "none";diff --git a/app/models/category.rb b/app/models/category.rbindex 7bad10cd51..d57a8905e5 100644--- a/app/models/category.rb+++ b/app/models/category.rb@@ -21,6 +21,7 @@ class Category < ApplicationRecordbefore_save :inherit_color_from_parentscope :alphabetically, -> { order(:name) }+ scope :recently_used, -> { where.not(last_used_at: nil).order(last_used_at: :desc) }scope :alphabetically_by_hierarchy, -> {left_joins(:parent).order(Arel.sql("COALESCE(parents_categories.name, categories.name)"))@@ -148,6 +149,17 @@ class Category < ApplicationRecord.index_with(true)end+ # Categories a family has manually assigned recently — a shortcut above the+ # alphabetical list, not a replacement for it. See Transaction#record_category_usage!+ # for where last_used_at is touched (only on a real human pick via one of the+ # manual assignment controllers, not rule/import auto-assignment).+ def recently_used_for(family:, excluding: [], limit: 4)+ family.categories+ .recently_used+ .excluding(Array(excluding).compact)+ .limit(limit)+ end+def suggested_icon(name)name_down = name.to_s.downcasediff --git a/app/models/entry.rb b/app/models/entry.rbindex a29b3b2bd7..44ffa15f49 100644--- a/app/models/entry.rb+++ b/app/models/entry.rb@@ -487,6 +487,7 @@ class Entry < ApplicationRecordattrs[:entryable_attributes] = attrs[:entryable_attributes].dup if attrs[:entryable_attributes].present?attrs[:entryable_attributes][:id] = entry.entryable_id if attrs[:entryable_attributes].present?entry.update! attrs+ entry.transaction.record_category_usage! if entry.transaction?changed = trueendenddiff --git a/app/models/transaction.rb b/app/models/transaction.rbindex 1aec5ef96e..0148616f66 100644--- a/app/models/transaction.rb+++ b/app/models/transaction.rb@@ -144,6 +144,16 @@ class Transaction < ApplicationRecordupdate!(category: category)end+ # Marks a category as recently used. Called explicitly from the manual+ # category-assignment controllers (picker, edit form, categorization+ # wizard, create-and-assign) after a successful save — not wired to a+ # blanket after_save callback because rule and import auto-assignment+ # also go through `category_id=`, and those shouldn't count as a "recent"+ # pick. See Category.recently_used_for.+ def record_category_usage!+ category.touch(:last_used_at) if saved_change_to_category_id? && category.present?+ end+def pending?extra_data = extra.is_a?(Hash) ? extra : {}PENDING_PROVIDERS.any? do |provider|diff --git a/app/views/category/dropdowns/_row.html.erb b/app/views/category/dropdowns/_row.html.erbindex 8f133c14be..0a0f41e540 100644--- a/app/views/category/dropdowns/_row.html.erb+++ b/app/views/category/dropdowns/_row.html.erb@@ -1,8 +1,8 @@-<%# locals: (category:) %>+<%# locals: (category:, id_prefix: "category_option") %><% is_selected = category.id === @selected_category&.id %><%= content_tag :div,- id: dom_id(category, "category_option"),+ id: dom_id(category, local_assigns.fetch(:id_prefix, "category_option")),role: "option",aria_selected: is_selected.to_s,class: ["filterable-item flex justify-between items-center border-none rounded-lg px-2 py-1 group w-full hover:bg-container-inset-hover",diff --git a/app/views/category/dropdowns/show.html.erb b/app/views/category/dropdowns/show.html.erbindex 27a272ea41..b514dc1082 100644--- a/app/views/category/dropdowns/show.html.erb+++ b/app/views/category/dropdowns/show.html.erb@@ -20,6 +20,15 @@<div class="pb-2 pl-4 mr-2 text-secondary hidden" data-list-filter-target="emptyMessage"><%= t(".no_categories") %></div>+ <% if @recent_categories.any? %>+ <div data-list-filter-target="recentSection">+ <p class="pl-2 pb-1 text-xs font-medium text-secondary uppercase tracking-wide"><%= t(".recent") %></p>+ <% @recent_categories.each do |category| %>+ <%= render "category/dropdowns/row", category: category, id_prefix: "recent_category_option" %>+ <% end %>+ <%= render "shared/ruler", classes: "my-2" %>+ </div>+ <% end %><% if @categories.any? %><% Category::Group.for(@categories).each do |group| %><%= render "category/dropdowns/row", category: group.category %>diff --git a/config/locales/views/category/dropdowns/ca.yml b/config/locales/views/category/dropdowns/ca.ymlindex 6cfdb1b6cc..dd79b0cb9a 100644--- a/config/locales/views/category/dropdowns/ca.yml+++ b/config/locales/views/category/dropdowns/ca.yml@@ -8,4 +8,5 @@ ca:show:clear: Esborra la categoriano_categories: No s'han trobat categories+ recent: Recentssearch_placeholder: Cercadiff --git a/config/locales/views/category/dropdowns/de.yml b/config/locales/views/category/dropdowns/de.ymlindex 1e1d79fddd..fe77f2a78e 100644--- a/config/locales/views/category/dropdowns/de.yml+++ b/config/locales/views/category/dropdowns/de.yml@@ -8,4 +8,5 @@ de:show:clear: Kategorie löschenno_categories: Keine Kategorien gefunden+ recent: Zuletzt verwendetsearch_placeholder: Suchendiff --git a/config/locales/views/category/dropdowns/en.yml b/config/locales/views/category/dropdowns/en.ymlindex 511e86a9a9..f384041cd6 100644--- a/config/locales/views/category/dropdowns/en.yml+++ b/config/locales/views/category/dropdowns/en.yml@@ -8,4 +8,5 @@ en:show:clear: Clear categoryno_categories: No categories found+ recent: Recentsearch_placeholder: Searchdiff --git a/config/locales/views/category/dropdowns/es.yml b/config/locales/views/category/dropdowns/es.ymlindex e7be1bd162..4f15601301 100644--- a/config/locales/views/category/dropdowns/es.yml+++ b/config/locales/views/category/dropdowns/es.yml@@ -8,4 +8,5 @@ es:show:clear: Limpiar categoríano_categories: No se encontraron categorías+ recent: Recientessearch_placeholder: Buscardiff --git a/config/locales/views/category/dropdowns/fr.yml b/config/locales/views/category/dropdowns/fr.ymlindex e577feee23..12af5568a6 100644--- a/config/locales/views/category/dropdowns/fr.yml+++ b/config/locales/views/category/dropdowns/fr.yml@@ -8,4 +8,5 @@ fr:show:clear: Effacer la catégorieno_categories: Aucune catégorie trouvée+ recent: Récentssearch_placeholder: Rechercherdiff --git a/config/locales/views/category/dropdowns/hu.yml b/config/locales/views/category/dropdowns/hu.ymlindex 70be94cf5b..390bf389c7 100644--- a/config/locales/views/category/dropdowns/hu.yml+++ b/config/locales/views/category/dropdowns/hu.yml@@ -8,4 +8,5 @@ hu:show:clear: Kategória törléseno_categories: Nem található kategória+ recent: Legutóbbiaksearch_placeholder: Keresésdiff --git a/config/locales/views/category/dropdowns/it.yml b/config/locales/views/category/dropdowns/it.ymlindex 9e7b7bcc25..2a29301bb4 100644--- a/config/locales/views/category/dropdowns/it.yml+++ b/config/locales/views/category/dropdowns/it.yml@@ -8,4 +8,5 @@ it:show:clear: Cancella categoriano_categories: Nessuna categoria trovata+ recent: Recentisearch_placeholder: Cercadiff --git a/config/locales/views/category/dropdowns/nb.yml b/config/locales/views/category/dropdowns/nb.ymlindex aa2cfa98b6..5304b55b21 100644--- a/config/locales/views/category/dropdowns/nb.yml+++ b/config/locales/views/category/dropdowns/nb.yml@@ -8,4 +8,5 @@ nb:show:clear: Fjern kategorino_categories: Ingen kategorier funnet+ recent: Nyligesearch_placeholder: Søk\ No newline at end of filediff --git a/config/locales/views/category/dropdowns/nl.yml b/config/locales/views/category/dropdowns/nl.ymlindex 9ae74d6a8e..eb04ecb142 100644--- a/config/locales/views/category/dropdowns/nl.yml+++ b/config/locales/views/category/dropdowns/nl.yml@@ -8,4 +8,5 @@ nl:show:clear: Categorie wissenno_categories: Geen categorieën gevonden+ recent: Recentsearch_placeholder: Zoekendiff --git a/config/locales/views/category/dropdowns/pl.yml b/config/locales/views/category/dropdowns/pl.ymlindex 74380bb48f..438283f987 100644--- a/config/locales/views/category/dropdowns/pl.yml+++ b/config/locales/views/category/dropdowns/pl.yml@@ -8,4 +8,5 @@ pl:show:clear: Wyczyść kategorięno_categories: Nie znaleziono kategorii+ recent: Ostatniesearch_placeholder: Szukajdiff --git a/config/locales/views/category/dropdowns/pt-BR.yml b/config/locales/views/category/dropdowns/pt-BR.ymlindex 6cde2c64ab..adb8c6fe2a 100644--- a/config/locales/views/category/dropdowns/pt-BR.yml+++ b/config/locales/views/category/dropdowns/pt-BR.yml@@ -8,4 +8,5 @@ pt-BR:show:clear: Limpar categoriano_categories: Nenhuma categoria encontrada+ recent: Recentessearch_placeholder: Buscardiff --git a/config/locales/views/category/dropdowns/ro.yml b/config/locales/views/category/dropdowns/ro.ymlindex 7b0a5bbf74..8617adb4b9 100644--- a/config/locales/views/category/dropdowns/ro.yml+++ b/config/locales/views/category/dropdowns/ro.yml@@ -8,4 +8,5 @@ ro:show:clear: Golește categoriano_categories: Nu s-au găsit categorii+ recent: Recentesearch_placeholder: Cautădiff --git a/config/locales/views/category/dropdowns/ru.yml b/config/locales/views/category/dropdowns/ru.ymlindex a0f008f821..cb00b95cc7 100644--- a/config/locales/views/category/dropdowns/ru.yml+++ b/config/locales/views/category/dropdowns/ru.yml@@ -8,4 +8,5 @@ ru:show:clear: Очистить категориюno_categories: Категории не найдены+ recent: Недавниеsearch_placeholder: Поискdiff --git a/config/locales/views/category/dropdowns/tr.yml b/config/locales/views/category/dropdowns/tr.ymlindex 3b645f4488..0c1eea6046 100644--- a/config/locales/views/category/dropdowns/tr.yml+++ b/config/locales/views/category/dropdowns/tr.yml@@ -8,4 +8,5 @@ tr:show:clear: Kategoriyi temizleno_categories: Hiç kategori bulunamadı+ recent: Son kullanılanlarsearch_placeholder: Aradiff --git a/config/locales/views/category/dropdowns/vi.yml b/config/locales/views/category/dropdowns/vi.ymlindex 04cf039475..11bd4d2ede 100644--- a/config/locales/views/category/dropdowns/vi.yml+++ b/config/locales/views/category/dropdowns/vi.yml@@ -8,4 +8,5 @@ vi:show:clear: Xóa danh mụcno_categories: Không tìm thấy danh mục nào+ recent: Gần đâysearch_placeholder: Tìm kiếmdiff --git a/config/locales/views/category/dropdowns/zh-CN.yml b/config/locales/views/category/dropdowns/zh-CN.ymlindex 3db41514c0..b4da34709a 100644--- a/config/locales/views/category/dropdowns/zh-CN.yml+++ b/config/locales/views/category/dropdowns/zh-CN.yml@@ -8,4 +8,5 @@ zh-CN:show:clear: 清空分类no_categories: 暂无分类+ recent: 最近使用search_placeholder: 搜索分类diff --git a/config/locales/views/category/dropdowns/zh-TW.yml b/config/locales/views/category/dropdowns/zh-TW.ymlindex 9970c4bd46..70c0a728ea 100644--- a/config/locales/views/category/dropdowns/zh-TW.yml+++ b/config/locales/views/category/dropdowns/zh-TW.yml@@ -8,4 +8,5 @@ zh-TW:show:clear: 清空分類no_categories: 暫無分類+ recent: 最近使用search_placeholder: 搜尋分類diff --git a/db/migrate/20260727111051_add_last_used_at_to_categories.rb b/db/migrate/20260727111051_add_last_used_at_to_categories.rbnew file mode 100644index 0000000000..35089c385e--- /dev/null+++ b/db/migrate/20260727111051_add_last_used_at_to_categories.rb@@ -0,0 +1,6 @@+class AddLastUsedAtToCategories < ActiveRecord::Migration[7.2]+ def change+ add_column :categories, :last_used_at, :datetime+ add_index :categories, [ :family_id, :last_used_at ]+ end+enddiff --git a/db/schema.rb b/db/schema.rbindex 7e94f1dce2..c4346da7fd 100644--- a/db/schema.rb+++ b/db/schema.rb@@ -10,7 +10,7 @@## It's strongly recommended that you check this file into your version control system.-ActiveRecord::Schema[7.2].define(version: 2026_07_25_000000) do+ActiveRecord::Schema[7.2].define(version: 2026_07_27_111051) do# These are extensions that must be enabled in order to support this databaseenable_extension "pgcrypto"enable_extension "plpgsql"@@ -394,7 +394,9 @@ ActiveRecord::Schema[7.2].define(version: 2026_07_25_000000) dot.uuid "parent_id"t.string "classification_unused", default: "expense", null: falset.string "lucide_icon", default: "shapes", null: false+ t.datetime "last_used_at"t.index ["family_id"], name: "index_categories_on_family_id"+ t.index ["family_id", "last_used_at"], name: "index_categories_on_family_id_and_last_used_at"endcreate_table "chats", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|diff --git a/test/controllers/category/dropdowns_controller_test.rb b/test/controllers/category/dropdowns_controller_test.rbnew file mode 100644index 0000000000..d8e1eb61f4--- /dev/null+++ b/test/controllers/category/dropdowns_controller_test.rb@@ -0,0 +1,50 @@+require "test_helper"++class Category::DropdownsControllerTest < ActionDispatch::IntegrationTest+ include ActionView::RecordIdentifier++ setup do+ sign_in users(:family_admin)+ @transaction = transactions(:one)+ ensure_tailwind_build+ end++ test "shows a recent section for categories used recently" do+ recent = categories(:income)+ recent.update!(last_used_at: 1.day.ago)++ get category_dropdown_url(transaction_id: @transaction.id)++ assert_response :success+ assert_select "[data-list-filter-target='recentSection']"+ assert_select "[data-list-filter-target='recentSection']", text: /#{Regexp.escape(recent.name)}/+ end++ test "recent and canonical rows for the same category have distinct DOM ids" do+ recent = categories(:income)+ recent.update!(last_used_at: 1.day.ago)++ get category_dropdown_url(transaction_id: @transaction.id)++ assert_response :success+ assert_select "##{dom_id(recent, 'recent_category_option')}", count: 1+ assert_select "##{dom_id(recent, 'category_option')}", count: 1+ end++ test "excludes the currently selected category from the recent section" do+ selected = categories(:food_and_drink)+ selected.update!(last_used_at: 1.day.ago)++ get category_dropdown_url(category_id: selected.id, transaction_id: @transaction.id)++ assert_response :success+ assert_select "[data-list-filter-target='recentSection']", false+ end++ test "omits the recent section entirely when nothing has been used yet" do+ get category_dropdown_url(transaction_id: @transaction.id)++ assert_response :success+ assert_select "[data-list-filter-target='recentSection']", false+ end+enddiff --git a/test/controllers/transaction_categories_controller_test.rb b/test/controllers/transaction_categories_controller_test.rbnew file mode 100644index 0000000000..116907edc2--- /dev/null+++ b/test/controllers/transaction_categories_controller_test.rb@@ -0,0 +1,33 @@+require "test_helper"++class TransactionCategoriesControllerTest < ActionDispatch::IntegrationTest+ setup do+ sign_in users(:family_admin)+ @entry = entries(:transaction)+ @transaction = transactions(:one)+ end++ test "assigning a category touches its last_used_at" do+ category = categories(:income)+ assert_nil category.last_used_at++ patch transaction_category_url(@entry),+ params: { entry: { entryable_type: "Transaction", entryable_attributes: { id: @transaction.id, category_id: category.id } } },+ as: :turbo_stream++ assert_not_nil category.reload.last_used_at+ end++ test "clearing a category does not touch any category's last_used_at" do+ category = @transaction.category+ assert_nil category.last_used_at++ patch transaction_category_url(@entry),+ params: { entry: { entryable_type: "Transaction", entryable_attributes: { id: @transaction.id, category_id: nil } } },+ as: :turbo_stream++ assert_response :success+ assert_nil @transaction.reload.category_id+ assert_nil category.reload.last_used_at+ end+enddiff --git a/test/models/category_test.rb b/test/models/category_test.rbindex a12b9790aa..637546a56c 100644--- a/test/models/category_test.rb+++ b/test/models/category_test.rb@@ -159,4 +159,32 @@ class CategoryTest < ActiveSupport::TestCaseassert lookup.key?(category.id)assert_not lookup.key?(0)end++ test "recently_used_for orders by last_used_at, most recent first" do+ older = categories(:income)+ newer = categories(:food_and_drink)+ older.update!(last_used_at: 2.days.ago)+ newer.update!(last_used_at: 1.day.ago)++ assert_equal [ newer, older ], Category.recently_used_for(family: @family).to_a+ end++ test "recently_used_for excludes categories with no usage yet" do+ categories(:food_and_drink).update!(last_used_at: 1.day.ago)++ assert_not_includes Category.recently_used_for(family: @family).to_a, categories(:income)+ end++ test "recently_used_for excludes given categories and respects limit" do+ a = categories(:income)+ b = categories(:food_and_drink)+ c = categories(:subcategory)+ a.update!(last_used_at: 3.days.ago)+ b.update!(last_used_at: 2.days.ago)+ c.update!(last_used_at: 1.day.ago)++ result = Category.recently_used_for(family: @family, excluding: b, limit: 1)++ assert_equal [ c ], result.to_a+ endenddiff --git a/test/models/entry_test.rb b/test/models/entry_test.rbindex bcf820d4db..77c4b036b3 100644--- a/test/models/entry_test.rb+++ b/test/models/entry_test.rb@@ -22,4 +22,14 @@ class EntryTest < ActiveSupport::TestCaseassert_equal entry_ids.sort, Entry.where(id: entry_ids).chronological.pluck(:id)assert_equal entry_ids.sort.reverse, Entry.where(id: entry_ids).reverse_chronological.pluck(:id)end++ test "bulk_update! touches the assigned category's last_used_at" do+ entry = create_transaction(account: accounts(:depository))+ category = categories(:income)+ assert_nil category.last_used_at++ Entry.where(id: entry.id).bulk_update!({ category_id: category.id })++ assert_not_nil category.reload.last_used_at+ endenddiff --git a/test/models/transaction_test.rb b/test/models/transaction_test.rbindex dfbf8e96b4..560d1040f8 100644--- a/test/models/transaction_test.rb+++ b/test/models/transaction_test.rb@@ -179,4 +179,44 @@ class TransactionTest < ActiveSupport::TestCaseassert_equal securities(:msft), transaction.activity_securityend++ test "record_category_usage! touches the new category's last_used_at" do+ transaction = transactions(:one)+ category = categories(:income)+ assert_nil category.last_used_at++ transaction.update!(category: category)+ transaction.record_category_usage!++ assert_not_nil category.reload.last_used_at+ end++ test "record_category_usage! does nothing when category_id did not change" do+ transaction = transactions(:one)+ category = transaction.category+ assert_nil category.last_used_at++ transaction.reload+ transaction.record_category_usage!++ assert_nil category.reload.last_used_at+ end++ test "record_category_usage! does nothing when category is cleared" do+ transaction = transactions(:one)++ transaction.update!(category: nil)++ assert_nothing_raised { transaction.record_category_usage! }+ end++ test "record_category_usage! is not invoked by rule-driven category enrichment" do+ transaction = transactions(:one)+ category = categories(:income)+ assert_nil category.last_used_at++ transaction.enrich_attribute(:category_id, category.id, source: "rule")++ assert_nil category.reload.last_used_at+ endend