fix: Handle uncategorized transactions filter correctly (#802)

* fix: Handle uncategorized transactions filter correctly

When filtering for 'Uncategorized' transactions, the filter was not working
because 'Uncategorized' is a virtual category (Category.uncategorized returns
a non-persisted Category object) and does not exist in the database.

The filter was attempting to match 'categories.name IN (Uncategorized)' which
returned zero results.

This fix removes 'Uncategorized' from the category names array before querying
the database, allowing the existing 'category_id IS NULL' condition to work
correctly.

Fixes filtering for uncategorized transactions while maintaining backward
compatibility with all other category filters.

* test: Add comprehensive tests for Uncategorized filter

- Test filtering for only uncategorized transactions
- Test combining uncategorized with real categories
- Test excluding uncategorized when not in filter
- Ensures fix prevents regression

* refactor: Use Category.uncategorized.name for i18n support

- Replace hard-coded 'Uncategorized' string with Category.uncategorized.name
- Conditionally build SQL query based on include_uncategorized flag
- Avoid adding category_id IS NULL clause when not needed
- Update tests to use Category.uncategorized.name for consistency
- Cleaner logic: only include uncategorized condition when requested

Addresses code review feedback on i18n support and query optimization.

* test: Fix travel category fixture error

Create travel category dynamically instead of using non-existent fixture

* style: Fix rubocop spacing in array brackets

---------

Co-authored-by: Charsel <charsel@charsel.com>
This commit is contained in:
charsel
2026-01-27 12:28:33 +01:00
committed by GitHub
parent 51c7f7a3f0
commit 33df3b781e
2 changed files with 112 additions and 18 deletions

View File

@@ -102,28 +102,38 @@ class Transaction::Search
def apply_category_filter(query, categories)
return query unless categories.present?
# Remove "Uncategorized" from category names to query the database
uncategorized_name = Category.uncategorized.name
include_uncategorized = categories.include?(uncategorized_name)
real_categories = categories - [ uncategorized_name ]
# Get parent category IDs for the given category names
parent_category_ids = family.categories.where(name: categories).pluck(:id)
parent_category_ids = family.categories.where(name: real_categories).pluck(:id)
uncategorized_condition = "(categories.id IS NULL AND transactions.kind NOT IN ('funds_movement', 'cc_payment'))"
# Build condition based on whether parent_category_ids is empty
if parent_category_ids.empty?
query = query.left_joins(:category).where(
"categories.name IN (?) OR (
categories.id IS NULL AND (transactions.kind NOT IN ('funds_movement', 'cc_payment'))
)",
categories
)
if include_uncategorized
query = query.left_joins(:category).where(
"categories.name IN (?) OR #{uncategorized_condition}",
real_categories.presence || []
)
else
query = query.left_joins(:category).where(categories: { name: real_categories })
end
else
query = query.left_joins(:category).where(
"categories.name IN (?) OR categories.parent_id IN (?) OR (
categories.id IS NULL AND (transactions.kind NOT IN ('funds_movement', 'cc_payment'))
)",
categories, parent_category_ids
)
end
if categories.exclude?("Uncategorized")
query = query.where.not(category_id: nil)
if include_uncategorized
query = query.left_joins(:category).where(
"categories.name IN (?) OR categories.parent_id IN (?) OR #{uncategorized_condition}",
real_categories, parent_category_ids
)
else
query = query.left_joins(:category).where(
"categories.name IN (?) OR categories.parent_id IN (?)",
real_categories, parent_category_ids
)
end
end
query