Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

feat(accounts): filter an account's activity to uncategorized transactions (#3797)

scragnog261350a7c335
diff --git a/app/components/UI/account/activity_feed.html.erb b/app/components/UI/account/activity_feed.html.erb
index 65bc3a6286..354fb6413b 100644
--- a/app/components/UI/account/activity_feed.html.erb
+++ b/app/components/UI/account/activity_feed.html.erb
@@ -99,6 +99,17 @@
onchange: "document.getElementById('entries-search').requestSubmit()" %>
<%= label_tag "q_status_pending", t("accounts.show.activity.pending"), class: "text-sm text-primary" %>
</div>
+ <p class="text-xs font-medium text-secondary uppercase pt-1"><%= t("accounts.show.activity.category") %></p>
+ <div class="flex items-center gap-3">
+ <%= check_box_tag "q[uncategorized]",
+ "1",
+ params.dig(:q, :uncategorized) == "1",
+ id: "q_uncategorized",
+ class: "checkbox checkbox--light",
+ form: "entries-search",
+ onchange: "document.getElementById('entries-search').requestSubmit()" %>
+ <%= label_tag "q_uncategorized", t("accounts.show.activity.uncategorized_only"), class: "text-sm text-primary" %>
+ </div>
</div>
<% end %>
<% end %>
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb
index c29562e550..fe1ed88688 100644
--- a/app/controllers/accounts_controller.rb
+++ b/app/controllers/accounts_controller.rb
@@ -89,7 +89,7 @@ class AccountsController < ApplicationController
# date beside a table shaded against another.
@as_of = Date.current
@accessible_account_ids = Current.user.accessible_accounts.pluck(:id).to_set
- @q = params.fetch(:q, {}).permit(:search, status: [])
+ @q = params.fetch(:q, {}).permit(:search, :uncategorized, status: [])
entries = @account.entries.excluding_split_parents.search(@q).reverse_chronological.includes(:entryable)
if statement_tab_active?
build_statement_tab_data
diff --git a/app/models/entry_search.rb b/app/models/entry_search.rb
index 6cb6fec3bb..6671cbcd86 100644
--- a/app/models/entry_search.rb
+++ b/app/models/entry_search.rb
@@ -7,6 +7,7 @@ class EntrySearch
attribute :amount_operator, :string
attribute :types, :string
attribute :status, array: true
+ attribute :uncategorized, :boolean
attribute :accounts, array: true
attribute :account_ids, array: true
attribute :start_date, :string
@@ -58,6 +59,15 @@ class EntrySearch
query
end
+ # Uncategorized means what it means everywhere else (the Transactions
+ # "Uncategorized" filter, the uncategorized badge, Quick Categorize):
+ # Entry.uncategorized_transactions, which skips transfers and excluded entries.
+ def apply_uncategorized_filter(scope, uncategorized)
+ return scope unless uncategorized
+
+ scope.where(id: Entry.uncategorized_transactions.select(:id))
+ end
+
def apply_status_filter(scope, statuses)
return scope unless statuses.present?
return scope if statuses.uniq.sort == %w[confirmed pending] # Both selected = no filter
@@ -101,6 +111,7 @@ class EntrySearch
query = self.class.apply_amount_filter(query, amount, amount_operator)
query = self.class.apply_accounts_filter(query, accounts, account_ids)
query = self.class.apply_status_filter(query, status)
+ query = self.class.apply_uncategorized_filter(query, uncategorized)
query
end
end
diff --git a/config/locales/views/accounts/en.yml b/config/locales/views/accounts/en.yml
index 19e6b93fe6..5db8310a9e 100644
--- a/config/locales/views/accounts/en.yml
+++ b/config/locales/views/accounts/en.yml
@@ -92,6 +92,7 @@ en:
activity:
amount: Amount
balance: Balance
+ category: Category
confirmed: Confirmed
date: Date
entries: entries
@@ -110,6 +111,7 @@ en:
search_placeholder: Search entries by name
status: Status
title: Activity
+ uncategorized_only: Uncategorized only
chart:
balance: Balance
owed: Amount owed
diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rb
index 0f8fe28138..abbeb44f84 100644
--- a/test/controllers/accounts_controller_test.rb
+++ b/test/controllers/accounts_controller_test.rb
@@ -16,6 +16,18 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
assert_select "p.ml-auto.privacy-sensitive"
end
+ test "show filters account activity to uncategorized transactions" do
+ uncategorized = create_transaction(account: @account, name: "Uncategorized Filter Target", category: nil)
+ categorized = create_transaction(account: @account, name: "Categorized Filter Decoy", category: categories(:food_and_drink))
+
+ get account_url(@account, q: { uncategorized: "1" })
+
+ assert_response :success
+ assert_select "input#q_uncategorized[checked]"
+ assert_match uncategorized.name, response.body
+ assert_no_match categorized.name, response.body
+ end
+
test "index delegates whole-row account clicks to the account link" do
get accounts_url
diff --git a/test/models/account/entry_test.rb b/test/models/account/entry_test.rb
index 5569f4c9aa..05826f8b01 100644
--- a/test/models/account/entry_test.rb
+++ b/test/models/account/entry_test.rb
@@ -67,6 +67,21 @@ class EntryTest < ActiveSupport::TestCase
assert_equal 0, family.entries.search(params).size
end
+ test "uncategorized filter keeps only uncategorized, non-transfer transactions" do
+ account = accounts(:depository)
+ uncategorized = create_transaction(account: account, name: "Needs a category", category: nil)
+ categorized = create_transaction(account: account, name: "Has a category", category: categories(:food_and_drink))
+ transfer_leg = create_transaction(account: account, name: "Transfer leg", category: nil, kind: "funds_movement")
+
+ ids = account.entries.search(uncategorized: true).pluck(:id)
+
+ assert_includes ids, uncategorized.id
+ assert_not_includes ids, categorized.id
+ assert_not_includes ids, transfer_leg.id
+
+ assert_includes account.entries.search(uncategorized: false).pluck(:id), categorized.id
+ end
+
test "status filter matches pending transactions for every supported provider via EntrySearch" do
family = families(:empty)
account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
diff --git a/test/system/account_activity_uncategorized_filter_test.rb b/test/system/account_activity_uncategorized_filter_test.rb
new file mode 100644
index 0000000000..a8c9a3453c
--- /dev/null
+++ b/test/system/account_activity_uncategorized_filter_test.rb
@@ -0,0 +1,26 @@
+require "application_system_test_case"
+
+class AccountActivityUncategorizedFilterTest < ApplicationSystemTestCase
+ include EntriesTestHelper
+ include ActionView::RecordIdentifier
+
+ setup do
+ sign_in @user = users(:family_admin)
+ @account = accounts(:depository)
+ @uncategorized = create_transaction(account: @account, name: "Uncategorized Filter Target", category: nil)
+ @categorized = create_transaction(account: @account, name: "Categorized Filter Decoy", category: categories(:food_and_drink))
+ page.current_window.resize_to(1280, 900)
+ end
+
+ test "the activity filter can narrow the list to uncategorized transactions" do
+ visit account_url(@account, tab: "activity")
+ assert_selector "##{dom_id(@categorized)}"
+ assert_selector "##{dom_id(@uncategorized)}"
+
+ click_button "Filter"
+ check "Uncategorized only"
+
+ assert_no_selector "##{dom_id(@categorized)}"
+ assert_selector "##{dom_id(@uncategorized)}"
+ end
+end