Skip to content

Commit

Permalink
Feature | Filter on uncategorized transactions (#1359)
Browse files Browse the repository at this point in the history
* allow filtering uncategorized transactions

* user can filter uncategorized transactions test

* rubocop linting
  • Loading branch information
bruno-costanzo authored Oct 25, 2024
1 parent aa3342b commit 2cc8919
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/helpers/categories_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ def null_category
name: "Uncategorized",
color: Category::UNCATEGORIZED_COLOR
end

def family_categories
[ null_category ].concat(Current.family.categories.alphabetically)
end
end
14 changes: 13 additions & 1 deletion app/models/account/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ class Account::Transaction < ApplicationRecord
class << self
def search(params)
query = all
query = query.joins(:category).where(categories: { name: params[:categories] }) if params[:categories].present?
if params[:categories].present?
if params[:categories].exclude?("Uncategorized")
query = query
.joins(:category)
.where(categories: { name: params[:categories] })
else
query = query
.left_joins(:category)
.where(categories: { name: params[:categories] })
.or(query.where(category_id: nil))
end
end

query = query.joins(:merchant).where(merchants: { name: params[:merchants] }) if params[:merchants].present?

if params[:tags].present?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<%= lucide_icon("search", class: "w-5 h-5 text-gray-500 absolute inset-y-0 left-2 top-1/2 transform -translate-y-1/2") %>
</div>
<div class="my-2" id="list" data-list-filter-target="list">
<% Current.family.categories.alphabetically.each do |category| %>
<% family_categories.each do |category| %>
<div class="filterable-item flex items-center gap-2 p-2" data-filter-name="<%= category.name %>">
<%= form.check_box :categories,
{
Expand Down
26 changes: 25 additions & 1 deletion test/system/transactions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TransactionsTest < ApplicationSystemTestCase

Account::Entry.delete_all # clean slate

create_transaction("one", 12.days.ago.to_date, 100)
@uncategorized_transaction = create_transaction("one", 12.days.ago.to_date, 100)
create_transaction("two", 10.days.ago.to_date, 100)
create_transaction("three", 9.days.ago.to_date, 100)
create_transaction("four", 8.days.ago.to_date, 100)
Expand Down Expand Up @@ -61,6 +61,30 @@ class TransactionsTest < ApplicationSystemTestCase
end
end

test "can filter uncategorized transactions" do
find("#transaction-filters-button").click

within "#transaction-filters-menu" do
click_button "Category"
check("Uncategorized")
click_button "Apply"
end

assert_selector "#" + dom_id(@uncategorized_transaction), count: 1
assert_no_selector("#" + dom_id(@transaction))

find("#transaction-filters-button").click

within "#transaction-filters-menu" do
click_button "Category"
check(@transaction.account_transaction.category.name)
click_button "Apply"
end

assert_selector "#" + dom_id(@transaction), count: 1
assert_selector "#" + dom_id(@uncategorized_transaction), count: 1
end

test "all filters work and empty state shows if no match" do
find("#transaction-filters-button").click

Expand Down

0 comments on commit 2cc8919

Please sign in to comment.