The personal finance app for everyone (by everyone)
Ruby
Give the bill form a searchable, grouped category picker (#4431)
Shibu M58e84e646f5c
diff --git a/app/views/recurring_transactions/_form.html.erb b/app/views/recurring_transactions/_form.html.erbindex c4e2808e14..18451f7dc2 100644--- a/app/views/recurring_transactions/_form.html.erb+++ b/app/views/recurring_transactions/_form.html.erb@@ -131,7 +131,10 @@</div><% if recurring_transaction.persisted? && %w[bill subscription installment other].include?(recurring_transaction.bill_type) %>- <div class="grid grid-cols-2 gap-3" data-controller="frequency-fields">+ <%# One field per row: the category picker opens a menu that holds a+ search field and the whole category list, and half a dialog is too+ narrow for either. %>+ <div class="space-y-3" data-controller="frequency-fields"><%= f.select :bill_type,%w[bill subscription installment other].map { |type| [ t("bills.all.types.#{type}"), type ] },{ label: t(".bill_type_label") },@@ -140,10 +143,11 @@form: f,categories: Current.family.categories.alphabetically_by_hierarchy.to_a,selected_id: recurring_transaction.category_id,- blank_label: t(".category_blank")+ blank_label: t(".category_blank"),+ locked: Current.family.categories_locked?) %>- <div data-frequency-fields-target="group" data-presets="installment" class="hidden col-span-2">+ <div data-frequency-fields-target="group" data-presets="installment" class="hidden"><%= f.number_field :end_after_count,label: t(".installment_count_label"),min: 2, step: 1 %>diff --git a/test/controllers/recurring_transactions_controller_test.rb b/test/controllers/recurring_transactions_controller_test.rbindex 76350fa191..3264a22a40 100644--- a/test/controllers/recurring_transactions_controller_test.rb+++ b/test/controllers/recurring_transactions_controller_test.rb@@ -64,6 +64,42 @@ class RecurringTransactionsControllerTest < ActionDispatch::IntegrationTestassert_select "input[type=hidden][name=?]", "recurring_transaction[category_id]"end+ # The bill form's category picker groups a category under its parent and+ # finds one by name, the way the transaction form's picker does. A flat list+ # of every category puts a subcategory among unrelated neighbours, and a+ # family with many categories has to scroll to find one.+ test "the bill edit form groups categories under their parent and searches them" do+ get edit_recurring_transaction_url(@recurring_transaction), headers: { "Turbo-Frame" => "modal" }++ assert_response :success++ # The picker searches, so a category is found by name, not by scrolling.+ assert_select "input[type=search][data-category-select-target=?]", "search"++ names = css_select("[data-category-select-target=option]").map { |option| option["data-category-name"] }+ parent = categories(:food_and_drink).name+ child = categories(:subcategory).name++ assert_includes names, parent+ assert_includes names, child+ assert_operator names.index(parent), :<, names.index(child), "a subcategory follows its parent"++ # The child row says so, so the order reads as a hierarchy and not as an+ # alphabetical accident.+ assert_select "[data-category-name=?] [data-testid=?]", child, "category-select-subcategory-indicator"+ end++ # A family that locked its categories cannot create one, whichever form it+ # starts from. The bill form used to offer the create row anyway.+ test "the bill edit form does not offer to create categories when the family locked them" do+ @family.update!(categories_locked: true)++ get edit_recurring_transaction_url(@recurring_transaction), headers: { "Turbo-Frame" => "modal" }++ assert_response :success+ assert_select "[data-category-select-locked-value=?]", "true"+ end+# Assigning the amount would already round it to the column's four places,# so the edit form refuses a finer one instead of saving a different figure.test "update refuses an amount finer than the column keeps" dodiff --git a/test/system/bill_category_select_test.rb b/test/system/bill_category_select_test.rbnew file mode 100644index 0000000000..eee5f7d2ac--- /dev/null+++ b/test/system/bill_category_select_test.rb@@ -0,0 +1,33 @@+require "application_system_test_case"++# The bill form's category picker finds a category by name and keeps a+# subcategory under its parent, the way the transaction form's picker does.+# A flat list of every category puts a subcategory among unrelated+# neighbours, and a family with many categories scrolls to find one.+class BillCategorySelectTest < ApplicationSystemTestCase+ setup do+ sign_in @user = users(:family_admin)+ @user.update!(preferences: (@user.preferences || {}).merge("preview_features_enabled" => true))+ @recurring = recurring_transactions(:netflix_subscription)+ end++ test "the bill edit form finds a category by name and keeps it under its parent" do+ visit edit_recurring_transaction_url(@recurring)++ within "[data-controller='category-select']" do+ find("button", match: :first).click++ # The picker searches, so a category is found by name, not by scrolling.+ fill_in "Search categories", with: "Restaurants"+ assert_selector "[data-category-select-target='option']:not(.hidden)", text: "Restaurants"++ # A subcategory sits under its parent, not among unrelated neighbours.+ names = page.evaluate_script(<<~JS)+ Array.from(document.querySelectorAll("[data-category-select-target='option']"))+ .map((option) => option.dataset.categoryName)+ JS++ assert_operator names.index("Food & Drink"), :<, names.index("Restaurants")+ end+ end+end