Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Feature/category hierarchy and account search (#2845)

Shibu Meb498cfa0ce3
diff --git a/app/components/DS/category_select.html.erb b/app/components/DS/category_select.html.erb
index 41bb5d4ee8..206edb6d41 100644
--- a/app/components/DS/category_select.html.erb
+++ b/app/components/DS/category_select.html.erb
@@ -15,7 +15,7 @@
class: "form-field__label" %>
<button type="button"
- id="<%= field_id %>"
+ id="<%= trigger_id %>"
class="form-field__input min-h-7 flex items-center gap-2 pr-8"
data-category-select-target="button"
data-action="click->category-select#toggle"
diff --git a/app/components/DS/category_select.rb b/app/components/DS/category_select.rb
index f09efa4fa0..cf84f3bf0e 100644
--- a/app/components/DS/category_select.rb
+++ b/app/components/DS/category_select.rb
@@ -25,6 +25,10 @@ class DS::CategorySelect < DesignSystemComponent
form.field_id(:category_id)
end
+ def trigger_id
+ "category_id_trigger"
+ end
+
def menu_id
"#{field_id}_menu"
end
diff --git a/app/components/DS/select.html.erb b/app/components/DS/select.html.erb
index 6999d697bb..3bd6600af6 100644
--- a/app/components/DS/select.html.erb
+++ b/app/components/DS/select.html.erb
@@ -49,7 +49,7 @@
role="listbox" tabindex="-1">
<% items.each do |item| %>
<% is_selected = item[:value] == selected_value %>
- <% obj = item[:object] %>
+ <% is_child = child?(item) %>
<%# Options use tabindex="-1" always — keyboard focus is managed
programmatically so listbox items never appear in the Tab
@@ -67,6 +67,10 @@
<%= helpers.icon("check") %>
</span>
+ <% if is_child %>
+ <%= helpers.icon("corner-down-right", size: "sm") %>
+ <% end %>
+
<% case variant %>
<% when :simple %>
<%= item[:label] %>
diff --git a/app/components/DS/select.rb b/app/components/DS/select.rb
index b19166bfb2..a9a9342198 100644
--- a/app/components/DS/select.rb
+++ b/app/components/DS/select.rb
@@ -61,6 +61,14 @@ module DS
obj&.respond_to?(:logo_url) && obj.logo_url.present? ? Setting.transform_brand_fetch_url(obj.logo_url) : nil
end
+ # Returns true if the item represents a child/subcategory of another item
+ # in the list (i.e. has a parent). Used to visually indent hierarchical
+ # items such as categories with subcategories.
+ def child?(item)
+ obj = item[:object]
+ obj&.respond_to?(:parent_id) && obj.parent_id.present?
+ end
+
private
def normalize_menu_placement(value)
diff --git a/app/controllers/splits_controller.rb b/app/controllers/splits_controller.rb
index e31b64596b..1d0e201451 100644
--- a/app/controllers/splits_controller.rb
+++ b/app/controllers/splits_controller.rb
@@ -3,7 +3,7 @@ class SplitsController < ApplicationController
before_action :require_split_write_permission!, only: %i[create update destroy]
def new
- @categories = Current.family.categories.alphabetically
+ @categories = grouped_categories
end
def create
@@ -35,7 +35,7 @@ class SplitsController < ApplicationController
return
end
- @categories = Current.family.categories.alphabetically
+ @categories = grouped_categories
@children = @entry.child_entries.includes(:entryable)
end
@@ -97,4 +97,8 @@ class SplitsController < ApplicationController
def split_params
params.require(:split).permit(splits: [ :name, :amount, :category_id, :excluded ])
end
+
+ def grouped_categories
+ Current.family.categories.alphabetically_by_hierarchy
+ end
end
diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb
index c21d03a9b4..23fdf90bf7 100644
--- a/app/controllers/transactions_controller.rb
+++ b/app/controllers/transactions_controller.rb
@@ -583,7 +583,7 @@ class TransactionsController < ApplicationController
.alphabetically
.includes(:account_providers, logo_attachment: :blob)
.to_a
- @categories = Current.family.categories.alphabetically.to_a
+ @categories = Current.family.categories.alphabetically_by_hierarchy.to_a
@merchants = Current.family.available_merchants_for(Current.user).alphabetically.to_a
@tags = Current.family.tags.alphabetically.to_a
end
diff --git a/app/controllers/transfers_controller.rb b/app/controllers/transfers_controller.rb
index eae79b8747..5a7f7e54eb 100644
--- a/app/controllers/transfers_controller.rb
+++ b/app/controllers/transfers_controller.rb
@@ -11,7 +11,7 @@ class TransfersController < ApplicationController
end
def show
- @categories = Current.family.categories.alphabetically
+ @categories = Current.family.categories.alphabetically_by_hierarchy
@tags = Current.family.tags.alphabetically
# Whether the current user can hit `mark_as_recurring`: feature flag on,
diff --git a/app/helpers/categories_helper.rb b/app/helpers/categories_helper.rb
index 458dae432b..87c2086658 100644
--- a/app/helpers/categories_helper.rb
+++ b/app/helpers/categories_helper.rb
@@ -20,6 +20,6 @@ module CategoriesHelper
end
def family_categories
- [ Category.uncategorized ].concat(Current.family.categories.alphabetically)
+ [ Category.uncategorized ].concat(Current.family.categories.alphabetically_by_hierarchy)
end
end
diff --git a/app/models/category.rb b/app/models/category.rb
index 89163f24aa..bad3148a1f 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -123,11 +123,35 @@ class Category < ApplicationRecord
delegate :name, :color, to: :category
+ # NOTE: if `categories` is a filtered/partial collection, any child whose
+ # parent isn't included is silently dropped (it's grouped under its
+ # parent_id, but that parent never appears in `roots`). Every current
+ # call site passes the full family category list, so this is latent
+ # today — pass a filtered scope with care.
def self.for(categories)
categories_by_parent_id = categories.to_a.group_by(&:parent_id)
- categories_by_parent_id[nil].to_a.map do |category|
- new(category, categories_by_parent_id[category.id].to_a)
+ roots = categories_by_parent_id[nil].to_a.sort_by { |category| category.name.downcase }
+
+ roots.map do |category|
+ subcategories = categories_by_parent_id[category.id].to_a.sort_by { |sub| sub.name.downcase }
+ new(category, subcategories)
+ end
+ end
+
+ # Builds [label, id] pairs for plain HTML <select> elements, ordered
+ # parent-then-children with children visually indented. Native <select>
+ # options can't render icons, so we use a unicode arrow prefix (regular
+ # leading spaces collapse in <option> text).
+ #
+ # Pass indent: false when the result is used as a display-label lookup
+ # rather than rendered as actual <select> options (e.g. Rule::Action and
+ # Rule::Condition#value_display), so the cosmetic arrow doesn't leak into
+ # plain-text summaries.
+ def self.select_options(categories, indent: true)
+ self.for(categories).flat_map do |group|
+ [ [ group.category.name, group.category.id ] ] +
+ group.subcategories.map { |sub| [ indent ? "↳ #{sub.name}" : sub.name, sub.id ] }
end
end
diff --git a/app/models/import/category_mapping.rb b/app/models/import/category_mapping.rb
index 56e7fbdd4f..a625aa1d47 100644
--- a/app/models/import/category_mapping.rb
+++ b/app/models/import/category_mapping.rb
@@ -25,7 +25,7 @@ class Import::CategoryMapping < Import::Mapping
end
def selectable_values
- family_categories = import.family.categories.alphabetically.map { |category| [ category.name, category.id ] }
+ family_categories = Category::Group.select_options(import.family.categories)
unless key.blank?
family_categories.unshift [ "Add as new category", CREATE_NEW_KEY ]
diff --git a/app/models/rule/action_executor/set_transaction_category.rb b/app/models/rule/action_executor/set_transaction_category.rb
index 689372f00d..97d58c5a1e 100644
--- a/app/models/rule/action_executor/set_transaction_category.rb
+++ b/app/models/rule/action_executor/set_transaction_category.rb
@@ -4,7 +4,7 @@ class Rule::ActionExecutor::SetTransactionCategory < Rule::ActionExecutor
end
def options
- family.categories.alphabetically.pluck(:name, :id)
+ Category::Group.select_options(family.categories, indent: false)
end
def execute(transaction_scope, value: nil, ignore_attribute_locks: false, rule_run: nil)
diff --git a/app/models/rule/condition_filter/transaction_category.rb b/app/models/rule/condition_filter/transaction_category.rb
index 71f87bbdfd..3e74b9d6dd 100644
--- a/app/models/rule/condition_filter/transaction_category.rb
+++ b/app/models/rule/condition_filter/transaction_category.rb
@@ -4,7 +4,7 @@ class Rule::ConditionFilter::TransactionCategory < Rule::ConditionFilter
end
def options
- family.categories.alphabetically.pluck(:name, :id)
+ Category::Group.select_options(family.categories, indent: false)
end
def prepare(scope)
diff --git a/app/views/DS/category_select/_option.html.erb b/app/views/DS/category_select/_option.html.erb
index 763ad6a300..d860c57746 100644
--- a/app/views/DS/category_select/_option.html.erb
+++ b/app/views/DS/category_select/_option.html.erb
@@ -4,6 +4,7 @@
role="option"
aria-selected="<%= selected %>"
data-category-select-target="option"
+ data-value="<%= category.id %>"
data-category-id="<%= category.id %>"
data-category-name="<%= category.name %>"
data-category-display-label="<%= category.name %>"
diff --git a/app/views/splits/_category_select.html.erb b/app/views/splits/_category_select.html.erb
index 02d6ab4bb5..f9f81952df 100644
--- a/app/views/splits/_category_select.html.erb
+++ b/app/views/splits/_category_select.html.erb
@@ -62,6 +62,7 @@
</div>
<% categories.each do |category| %>
+ <% is_child = category.parent_id.present? %>
<% is_selected = category.id == selected_id %>
<div class="filterable-item text-sm cursor-pointer flex items-center gap-2 px-3 py-2 rounded-lg hover:bg-container-inset-hover <%= "bg-container-inset" if is_selected %>"
role="option"
@@ -73,6 +74,9 @@
<span class="check-icon <%= "hidden" unless is_selected %>">
<%= icon("check") %>
</span>
+ <% if is_child %>
+ <%= icon("corner-down-right", size: "sm") %>
+ <% end %>
<%= render partial: "categories/badge", locals: { category: category } %>
</div>
<% end %>
diff --git a/app/views/transactions/_form.html.erb b/app/views/transactions/_form.html.erb
index dd38d09d44..02eb479de2 100644
--- a/app/views/transactions/_form.html.erb
+++ b/app/views/transactions/_form.html.erb
@@ -18,7 +18,7 @@
<% if @entry.account_id %>
<%= f.hidden_field :account_id, data: { transaction_form_target: "account" } %>
<% else %>
- <%= f.collection_select :account_id, manual_accounts, :id, :name, { prompt: t(".account_prompt"), label: t(".account"), selected: Current.user.default_account_for_transactions&.id, variant: :logo }, required: true, class: "form-field__input text-ellipsis", data: { transaction_form_target: "account", action: "change->transaction-form#checkCurrencyDifference" } %>
+ <%= f.collection_select :account_id, manual_accounts, :id, :name, { prompt: t(".account_prompt"), label: t(".account"), selected: Current.user.default_account_for_transactions&.id, variant: :logo, searchable: true }, required: true, class: "form-field__input text-ellipsis", data: { transaction_form_target: "account", action: "change->transaction-form#checkCurrencyDifference" } %>
<% end %>
<%= f.money_field :amount,
diff --git a/app/views/transactions/bulk_updates/new.html.erb b/app/views/transactions/bulk_updates/new.html.erb
index f899da713b..df7d55b37c 100644
--- a/app/views/transactions/bulk_updates/new.html.erb
+++ b/app/views/transactions/bulk_updates/new.html.erb
@@ -11,7 +11,7 @@
<%= render DS::Disclosure.new(title: t(".transactions_section"), open: true) do %>
<div class="space-y-2">
<%= form.text_field :name, label: t(".name_label"), placeholder: t(".name_placeholder") %>
- <%= form.collection_select :category_id, Current.family.categories.alphabetically, :id, :name, { prompt: t(".category_prompt"), label: t(".category_label"), class: "text-subdued", variant: :badge, searchable: true } %>
+ <%= form.collection_select :category_id, Current.family.categories.alphabetically_by_hierarchy, :id, :name, { prompt: t(".category_prompt"), label: t(".category_label"), class: "text-subdued", variant: :badge, searchable: true } %>
<%= form.collection_select :merchant_id, Current.family.available_merchants_for(Current.user).alphabetically, :id, :name, { prompt: t(".merchant_prompt"), label: t(".merchant_label"), class: "text-subdued" } %>
<%= form.select :tag_ids, Current.family.tags.alphabetically.pluck(:name, :id), { include_blank: t(".none"), multiple: true, label: t(".tags_label"), include_hidden: false } %>
<%= form.text_area :notes, label: t(".notes_label"), placeholder: t(".notes_placeholder"), rows: 5 %>
diff --git a/app/views/transactions/categorizes/_entry_row.html.erb b/app/views/transactions/categorizes/_entry_row.html.erb
index 16180bdc33..ff3a0d2a5f 100644
--- a/app/views/transactions/categorizes/_entry_row.html.erb
+++ b/app/views/transactions/categorizes/_entry_row.html.erb
@@ -11,7 +11,7 @@
<%= format_money(entry.amount_money.abs) %>
</span>
<%= select_tag "category_id",
- options_from_collection_for_select(categories, :id, :name),
+ options_for_select(Category::Group.select_options(categories)),
prompt: t("transactions.categorizes.show.assign_category_prompt"),
aria: { label: t("transactions.categorizes.entry_row.assign_category_select", name: entry.name) },
class: "w-full text-xs border border-primary rounded-lg px-1.5 py-0.5 bg-container text-secondary",
diff --git a/app/views/transactions/searches/filters/_category_filter.html.erb b/app/views/transactions/searches/filters/_category_filter.html.erb
index 080bbe3ce7..877cde855e 100644
--- a/app/views/transactions/searches/filters/_category_filter.html.erb
+++ b/app/views/transactions/searches/filters/_category_filter.html.erb
@@ -11,6 +11,7 @@
) %>
<div class="my-2" id="list" data-list-filter-target="list">
<% family_categories.each do |category| %>
+ <% is_child = category.parent_id.present? %>
<div class="filterable-item flex items-center gap-2 p-2" data-filter-name="<%= category.display_name %>">
<%= form.check_box :categories,
{
@@ -20,7 +21,10 @@
},
category.name,
nil %>
- <%= form.label :categories, category.display_name, value: category.name, class: "text-sm text-primary cursor-pointer" do %>
+ <%= form.label :categories, category.display_name, value: category.name, class: "text-sm text-primary cursor-pointer flex items-center gap-1" do %>
+ <% if is_child %>
+ <%= icon("corner-down-right", size: "sm") %>
+ <% end %>
<%= render partial: "categories/badge", locals: { category: category } %>
<% end %>
</div>
diff --git a/app/views/transactions/show.html.erb b/app/views/transactions/show.html.erb
index 77a00aa5f9..132ffff80e 100644
--- a/app/views/transactions/show.html.erb
+++ b/app/views/transactions/show.html.erb
@@ -85,7 +85,7 @@
<%= f.fields_for :entryable do |ef| %>
<%= render DS::CategorySelect.new(
form: ef,
- categories: Current.family.categories.alphabetically,
+ categories: Current.family.categories.alphabetically_by_hierarchy,
selected_id: ef.object.category_id,
disabled: @entry.split_child? || annotate_locked,
auto_submit: true,
diff --git a/app/views/transfers/show.html.erb b/app/views/transfers/show.html.erb
index 42399a8ce4..2da9e71ba3 100644
--- a/app/views/transfers/show.html.erb
+++ b/app/views/transfers/show.html.erb
@@ -84,7 +84,7 @@
<%= styled_form_with model: @transfer,
data: { controller: "auto-submit-form" }, class: "space-y-2" do |f| %>
<% if @transfer.categorizable? %>
- <%= f.collection_select :category_id, @categories.alphabetically, :id, :name, { label: t(".category"), include_blank: t(".uncategorized"), selected: @transfer.outflow_transaction.category&.id, variant: :badge, searchable: true }, "data-auto-submit-form-target": "auto" %>
+ <%= f.collection_select :category_id, @categories, :id, :name, { label: t(".category"), include_blank: t(".uncategorized"), selected: @transfer.outflow_transaction.category&.id, variant: :badge, searchable: true }, "data-auto-submit-form-target": "auto" %>
<% end %>
<%= render DS::TagSelect.new(
form: f,
diff --git a/test/components/DS/select_test.rb b/test/components/DS/select_test.rb
new file mode 100644
index 0000000000..f3011bc71e
--- /dev/null
+++ b/test/components/DS/select_test.rb
@@ -0,0 +1,49 @@
+require "test_helper"
+
+class DS::SelectTest < ViewComponent::TestCase
+ test "child? is true for an item whose object has a present parent_id" do
+ select = DS::Select.new(
+ form: nil,
+ method: :category_id,
+ items: [ categories(:subcategory) ]
+ )
+
+ item = select.items.first
+ assert select.child?(item)
+ end
+
+ test "child? is false for an item whose object has no parent_id" do
+ select = DS::Select.new(
+ form: nil,
+ method: :category_id,
+ items: [ categories(:food_and_drink) ]
+ )
+
+ item = select.items.first
+ assert_not select.child?(item)
+ end
+
+ test "child? is false for objects that don't respond to parent_id" do
+ select = DS::Select.new(
+ form: nil,
+ method: :merchant_id,
+ items: [ merchants(:netflix) ]
+ )
+
+ item = select.items.first
+ assert_not select.child?(item)
+ end
+
+ test "child? is false for the include_blank placeholder item" do
+ select = DS::Select.new(
+ form: nil,
+ method: :category_id,
+ items: [ categories(:subcategory) ],
+ include_blank: "Uncategorized"
+ )
+
+ blank_item = select.items.first
+ assert_nil blank_item[:object]
+ assert_not select.child?(blank_item)
+ end
+end
diff --git a/test/controllers/splits_controller_test.rb b/test/controllers/splits_controller_test.rb
index 5cb89a9ff6..412ef7aef6 100644
--- a/test/controllers/splits_controller_test.rb
+++ b/test/controllers/splits_controller_test.rb
@@ -17,6 +17,21 @@ class SplitsControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end
+ test "new groups subcategories immediately after their parent in the category selector" do
+ get new_transaction_split_path(@entry)
+ assert_response :success
+
+ doc = Nokogiri::HTML::Document.parse(response.body)
+ category_values = doc.css(".category-select-container [data-value]").map { |node| node["data-value"] }
+
+ parent_index = category_values.index(categories(:food_and_drink).id)
+ child_index = category_values.index(categories(:subcategory).id)
+
+ assert_not_nil parent_index
+ assert_not_nil child_index
+ assert_equal parent_index + 1, child_index
+ end
+
test "create with valid params splits transaction" do
assert_difference "Entry.count", 2 do
post transaction_split_path(@entry), params: {
diff --git a/test/controllers/transactions/categorizes_controller_test.rb b/test/controllers/transactions/categorizes_controller_test.rb
index 1e74d8bbb1..8c40f32861 100644
--- a/test/controllers/transactions/categorizes_controller_test.rb
+++ b/test/controllers/transactions/categorizes_controller_test.rb
@@ -26,6 +26,23 @@ class Transactions::CategorizesControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end
+ test "show groups subcategories immediately after their parent in the category select" do
+ create_transaction(account: @account, name: "Starbucks")
+ get transactions_categorize_url
+
+ assert_response :success
+
+ doc = Nokogiri::HTML::Document.parse(response.body)
+ option_values = doc.css("select[name='category_id'] option").map { |node| node["value"] }
+
+ parent_index = option_values.index(categories(:food_and_drink).id)
+ child_index = option_values.index(categories(:subcategory).id)
+
+ assert_not_nil parent_index
+ assert_not_nil child_index
+ assert_equal parent_index + 1, child_index
+ end
+
test "show renders full dates so multi-year lists are unambiguous" do
create_transaction(account: @account, name: "Starbucks", date: Date.new(2024, 7, 8))
diff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rb
index 045a54b109..2ee4d14146 100644
--- a/test/controllers/transactions_controller_test.rb
+++ b/test/controllers/transactions_controller_test.rb
@@ -8,6 +8,21 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
@entry = entries(:transaction)
end
+ test "index groups subcategories immediately after their parent in the category filter" do
+ get transactions_url
+ assert_response :success
+
+ doc = Nokogiri::HTML::Document.parse(response.body)
+ checkbox_values = doc.css("input[name='q[categories][]']").map { |node| node["value"] }
+
+ parent_index = checkbox_values.index(categories(:food_and_drink).name)
+ child_index = checkbox_values.index(categories(:subcategory).name)
+
+ assert_not_nil parent_index
+ assert_not_nil child_index
+ assert_equal parent_index + 1, child_index
+ end
+
test "creates with transaction details" do
assert_difference [ "Entry.count", "Transaction.count" ], 1 do
post transactions_url, params: {
@@ -698,6 +713,37 @@ end
assert_not entry.protected_from_sync?
end
+ test "new groups subcategories immediately after their parent in the category select" do
+ get new_transaction_url
+ assert_response :success
+
+ doc = Nokogiri::HTML::Document.parse(response.body)
+ trigger = doc.at_css("#category_id_trigger")
+ assert_not_nil trigger, "expected the category select trigger button to render"
+
+ wrapper = trigger.ancestors(".relative").first
+ category_values = wrapper.css("[data-value]").map { |node| node["data-value"] }
+
+ parent_index = category_values.index(categories(:food_and_drink).id)
+ child_index = category_values.index(categories(:subcategory).id)
+
+ assert_not_nil parent_index
+ assert_not_nil child_index
+ assert_equal parent_index + 1, child_index
+ end
+
+ test "new renders a search box for account selection" do
+ get new_transaction_url
+ assert_response :success
+
+ doc = Nokogiri::HTML::Document.parse(response.body)
+ trigger = doc.at_css("#account_id_trigger")
+ assert_not_nil trigger, "expected the account select trigger button to render"
+
+ wrapper = trigger.ancestors(".relative").first
+ assert_not_nil wrapper.at_css("input[type='search']"), "expected a search input inside the account select"
+ end
+
test "new with duplicate_entry_id pre-fills form from source transaction" do
@entry.reload
diff --git a/test/models/category/group_test.rb b/test/models/category/group_test.rb
new file mode 100644
index 0000000000..d41de57264
--- /dev/null
+++ b/test/models/category/group_test.rb
@@ -0,0 +1,58 @@
+require "test_helper"
+
+class Category::GroupTest < ActiveSupport::TestCase
+ setup do
+ @family = families(:dylan_family)
+ end
+
+ test "for groups categories by parent, root categories without children get an empty subcategories array" do
+ groups = Category::Group.for(@family.categories)
+
+ food_group = groups.find { |g| g.category == categories(:food_and_drink) }
+ assert_equal [ categories(:subcategory) ], food_group.subcategories
+
+ income_group = groups.find { |g| g.category == categories(:income) }
+ assert_equal [], income_group.subcategories
+ end
+
+ test "for does not include a category as its own root when it is a subcategory" do
+ groups = Category::Group.for(@family.categories)
+
+ assert_nil groups.find { |g| g.category == categories(:subcategory) }
+ end
+
+ test "select_options orders parent immediately followed by its children" do
+ options = Category::Group.select_options(@family.categories)
+
+ parent_index = options.index { |label, id| id == categories(:food_and_drink).id }
+ child_index = options.index { |label, id| id == categories(:subcategory).id }
+
+ assert_not_nil parent_index
+ assert_not_nil child_index
+ assert_equal parent_index + 1, child_index, "subcategory should immediately follow its parent in the options list"
+ end
+
+ test "select_options indents subcategory labels so they're visually distinguishable from parents" do
+ options = Category::Group.select_options(@family.categories)
+
+ parent_label, = options.find { |label, id| id == categories(:food_and_drink).id }
+ child_label, = options.find { |label, id| id == categories(:subcategory).id }
+
+ assert_equal categories(:food_and_drink).name, parent_label
+ assert_equal "↳ #{categories(:subcategory).name}", child_label
+ end
+
+ test "select_options orders roots and subcategories alphabetically regardless of input order" do
+ reversed_options = Category::Group.select_options(@family.categories.to_a.reverse)
+ forward_options = Category::Group.select_options(@family.categories)
+
+ assert_equal forward_options, reversed_options
+ end
+
+ test "select_options returns [label, id] pairs usable directly by options_for_select / form.select" do
+ options = Category::Group.select_options(@family.categories)
+
+ assert options.all? { |pair| pair.is_a?(Array) && pair.size == 2 }
+ assert options.all? { |_label, id| id.present? }
+ end
+end
diff --git a/test/models/category_test.rb b/test/models/category_test.rb
index 637546a56c..c227b1ebc3 100644
--- a/test/models/category_test.rb
+++ b/test/models/category_test.rb
@@ -160,6 +160,17 @@ class CategoryTest < ActiveSupport::TestCase
assert_not lookup.key?(0)
end
+ test "alphabetically_by_hierarchy orders a category's subcategories immediately after it" do
+ ordered = @family.categories.alphabetically_by_hierarchy.to_a
+
+ parent_index = ordered.index(categories(:food_and_drink))
+ child_index = ordered.index(categories(:subcategory))
+
+ assert_not_nil parent_index
+ assert_not_nil child_index
+ assert_equal parent_index + 1, child_index
+ end
+
test "recently_used_for orders by last_used_at, most recent first" do
older = categories(:income)
newer = categories(:food_and_drink)
diff --git a/test/models/import/category_mapping_test.rb b/test/models/import/category_mapping_test.rb
new file mode 100644
index 0000000000..4d20966b1d
--- /dev/null
+++ b/test/models/import/category_mapping_test.rb
@@ -0,0 +1,25 @@
+require "test_helper"
+
+class Import::CategoryMappingTest < ActiveSupport::TestCase
+ setup do
+ @mapping = import_mappings(:one)
+ end
+
+ test "selectable_values groups subcategories immediately after their parent" do
+ options = @mapping.selectable_values
+
+ parent_index = options.index { |_label, id| id == categories(:food_and_drink).id }
+ child_index = options.index { |_label, id| id == categories(:subcategory).id }
+
+ assert_not_nil parent_index
+ assert_not_nil child_index
+ assert_equal parent_index + 1, child_index
+ end
+
+ test "selectable_values still prepends 'Add as new category' when key is present" do
+ options = @mapping.selectable_values
+
+ assert_equal "Add as new category", options.first.first
+ assert_equal Import::Mapping::CREATE_NEW_KEY, options.first.last
+ end
+end
diff --git a/test/models/rule/action_executor/set_transaction_category_test.rb b/test/models/rule/action_executor/set_transaction_category_test.rb
new file mode 100644
index 0000000000..415c52fe6c
--- /dev/null
+++ b/test/models/rule/action_executor/set_transaction_category_test.rb
@@ -0,0 +1,29 @@
+require "test_helper"
+
+class Rule::ActionExecutor::SetTransactionCategoryTest < ActiveSupport::TestCase
+ setup do
+ @rule = rules(:one)
+ @executor = Rule::ActionExecutor::SetTransactionCategory.new(@rule)
+ end
+
+ test "options groups subcategories immediately after their parent" do
+ options = @executor.options
+
+ parent_index = options.index { |_label, id| id == categories(:food_and_drink).id }
+ child_index = options.index { |_label, id| id == categories(:subcategory).id }
+
+ assert_not_nil parent_index
+ assert_not_nil child_index
+ assert_equal parent_index + 1, child_index
+ end
+
+ test "options does not indent subcategory labels, so value_display doesn't show the arrow" do
+ action = Rule::Action.new(
+ rule: @rule,
+ action_type: "set_transaction_category",
+ value: categories(:subcategory).id
+ )
+
+ assert_equal categories(:subcategory).name, action.value_display
+ end
+end
diff --git a/test/models/rule/condition_filter/transaction_category_test.rb b/test/models/rule/condition_filter/transaction_category_test.rb
new file mode 100644
index 0000000000..b458dad3bb
--- /dev/null
+++ b/test/models/rule/condition_filter/transaction_category_test.rb
@@ -0,0 +1,27 @@
+require "test_helper"
+
+class Rule::ConditionFilter::TransactionCategoryTest < ActiveSupport::TestCase
+ setup do
+ @rule = rules(:one)
+ @filter = Rule::ConditionFilter::TransactionCategory.new(@rule)
+ end
+
+ test "options groups subcategories immediately after their parent" do
+ options = @filter.options
+
+ parent_index = options.index { |_label, id| id == categories(:food_and_drink).id }
+ child_index = options.index { |_label, id| id == categories(:subcategory).id }
+
+ assert_not_nil parent_index
+ assert_not_nil child_index
+ assert_equal parent_index + 1, child_index
+ end
+
+ test "options does not indent subcategory labels, since this array doubles as the value_display lookup" do
+ options = @filter.options
+
+ child_label, = options.find { |_label, id| id == categories(:subcategory).id }
+
+ assert_equal categories(:subcategory).name, child_label
+ end
+end