The personal finance app for everyone (by everyone)
Ruby
Quick Categorize Wizard — follow-up fixes (#1393)
Mikael Møller5cb474d61cc9
diff --git a/app/controllers/transactions/categorizes_controller.rb b/app/controllers/transactions/categorizes_controller.rbindex 38a5050295..b65ebea649 100644--- a/app/controllers/transactions/categorizes_controller.rb+++ b/app/controllers/transactions/categorizes_controller.rb@@ -18,7 +18,7 @@ class Transactions::CategorizesController < ApplicationController@group = groups.first@categories = Current.family.categories.alphabetically- @total_uncategorized = Entry.uncategorized_count(Current.accessible_entries)+ @total_uncategorized = uncategorized_countenddef create@@ -60,7 +60,7 @@ class Transactions::CategorizesController < ApplicationControllerendstreams << turbo_stream.replace("categorize_remaining",partial: "transactions/categorizes/remaining_count",- locals: { total_uncategorized: Entry.uncategorized_count(Current.accessible_entries) })+ locals: { total_uncategorized: uncategorized_count })streams << turbo_stream.replace("categorize_group_summary",partial: "transactions/categorizes/group_summary",locals: { entries: remaining_entries })@@ -98,7 +98,7 @@ class Transactions::CategorizesController < ApplicationControllerall_entry_ids = Array.wrap(params[:all_entry_ids]).reject(&:blank?)remaining_ids = all_entry_ids - [ entry.id.to_s ]- Entry.where(id: entry.id).bulk_update!({ category_id: category.id })+ Current.accessible_entries.where(id: entry.id).bulk_update!({ category_id: category.id })remaining_entries = uncategorized_entries_for(remaining_ids)remaining_ids = remaining_entries.map { |e| e.id.to_s }@@ -109,7 +109,7 @@ class Transactions::CategorizesController < ApplicationControllerelsestreams << turbo_stream.replace("categorize_remaining",partial: "transactions/categorizes/remaining_count",- locals: { total_uncategorized: Entry.uncategorized_count(Current.accessible_entries) })+ locals: { total_uncategorized: uncategorized_count })streams << turbo_stream.replace("categorize_group_summary",partial: "transactions/categorizes/group_summary",locals: { entries: remaining_entries })@@ -119,13 +119,16 @@ class Transactions::CategorizesController < ApplicationControllerprivate+ def uncategorized_count+ Current.accessible_entries.uncategorized_transactions.count+ end+def uncategorized_entries_for(ids)return [] if ids.blank?Current.accessible_entries.excluding_split_parents.where(id: ids)- .joins("INNER JOIN transactions ON transactions.id = entries.entryable_id AND entries.entryable_type = 'Transaction'")- .where(transactions: { category_id: nil })+ .uncategorized_transactions.to_aendenddiff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rbindex 876e7d848f..6548d26edf 100644--- a/app/controllers/transactions_controller.rb+++ b/app/controllers/transactions_controller.rb@@ -52,7 +52,7 @@ class TransactionsController < ApplicationControllerSet.newend- @uncategorized_count = Current.family.uncategorized_transaction_count+ @uncategorized_count = Current.accessible_entries.uncategorized_transactions.count# Load projected recurring transactions for next 10 days@projected_recurring = Current.family.recurring_transactionsdiff --git a/app/models/entry.rb b/app/models/entry.rbindex 2a3de89ad3..48b216f36e 100644--- a/app/models/entry.rb+++ b/app/models/entry.rb@@ -91,19 +91,16 @@ class Entry < ApplicationRecordjoins(:account).where(accounts: { family_id: family.id })end- # Counts uncategorized, non-transfer entries in the given scope.- # Used by the Quick Categorize Wizard to show the remaining count.- # @param entries [ActiveRecord::Relation] pre-scoped entries (caller controls authorization)- def self.uncategorized_count(entries)- entries- .joins(:account)+ # Uncategorized, non-transfer transaction entries on draft or active accounts.+ # Caller is responsible for scoping to accessible entries before applying this scope.+ scope :uncategorized_transactions, -> {+ joins(:account).joins("INNER JOIN transactions ON transactions.id = entries.entryable_id AND entries.entryable_type = 'Transaction'").where(accounts: { status: %w[draft active] }).where(transactions: { category_id: nil }).where.not(transactions: { kind: Transaction::TRANSFER_KINDS }).where(entries: { excluded: false })- .count- end+ }# Returns uncategorized, non-transfer entries whose name matches the given filter string.# Used by the Quick Categorize Wizard to preview which transactions a rule would affect.@@ -111,13 +108,8 @@ class Entry < ApplicationRecorddef self.uncategorized_matching(entries, filter, transaction_type = nil)sanitized = sanitize_sql_like(filter.gsub(/\s+/, " ").strip)scope = entries- .joins(:account)- .joins("INNER JOIN transactions ON transactions.id = entries.entryable_id AND entries.entryable_type = 'Transaction'")- .where(accounts: { status: %w[draft active] })- .where(transactions: { category_id: nil })- .where.not(transactions: { kind: Transaction::TRANSFER_KINDS })- .where(entries: { excluded: false })- .where("BTRIM(REGEXP_REPLACE(entries.name, '[[:space:]]+', ' ', 'g')) ILIKE ?", "%#{sanitized}%")+ .uncategorized_transactions+ .where("BTRIM(REGEXP_REPLACE(entries.name, '[[:space:]]+', ' ', 'g')) ILIKE ?", "%#{sanitized}%")scope = case transaction_typewhen "income" then scope.where("entries.amount < 0")diff --git a/app/models/family.rb b/app/models/family.rbindex 2f7ad1b50c..f5eceb93f2 100644--- a/app/models/family.rb+++ b/app/models/family.rb@@ -144,17 +144,6 @@ class Family < ApplicationRecordAutoMerchantDetector.new(self, transaction_ids: transaction_ids).auto_detectend- def uncategorized_transaction_count- Transaction- .joins("INNER JOIN entries ON entries.entryable_id = transactions.id AND entries.entryable_type = 'Transaction'")- .joins("INNER JOIN accounts ON accounts.id = entries.account_id")- .where(accounts: { family_id: id, status: %w[draft active] })- .where(transactions: { category_id: nil })- .where.not(transactions: { kind: Transaction::TRANSFER_KINDS })- .where(entries: { excluded: false })- .count- end-def balance_sheet(user: Current.user)BalanceSheet.new(self, user: user)enddiff --git a/app/models/transaction/grouper/by_merchant_or_name.rb b/app/models/transaction/grouper/by_merchant_or_name.rbindex 3618f65dee..8a80a83885 100644--- a/app/models/transaction/grouper/by_merchant_or_name.rb+++ b/app/models/transaction/grouper/by_merchant_or_name.rb@@ -22,12 +22,7 @@ class Transaction::Grouper::ByMerchantOrName < Transaction::Grouperdef uncategorized_entriesentries- .joins(:account)- .joins("INNER JOIN transactions ON transactions.id = entries.entryable_id AND entries.entryable_type = 'Transaction'")- .where(accounts: { status: %w[draft active] })- .where(transactions: { category_id: nil })- .where.not(transactions: { kind: Transaction::TRANSFER_KINDS })- .where(entries: { excluded: false })+ .uncategorized_transactions.includes(entryable: :merchant).order(entries: { date: :desc })enddiff --git a/app/views/transactions/categorizes/_entry_row.html.erb b/app/views/transactions/categorizes/_entry_row.html.erbindex 5321079bce..daba95b7f4 100644--- a/app/views/transactions/categorizes/_entry_row.html.erb+++ b/app/views/transactions/categorizes/_entry_row.html.erb@@ -7,7 +7,7 @@data-action="change->categorize#uncheckRule"><span class="min-w-0 font-medium text-primary truncate"><%= entry.name %></span><span class="text-secondary whitespace-nowrap"><%= l(entry.date, format: :short) %></span>- <span class="font-medium text-right whitespace-nowrap <%= entry.amount.negative? ? "text-green-600" : "text-primary" %>">+ <span class="privacy-sensitive font-medium text-right whitespace-nowrap <%= entry.amount.negative? ? "text-green-600" : "text-primary" %>"><%= format_money(entry.amount_money.abs) %></span><%= select_tag "category_id",diff --git a/app/views/transactions/categorizes/_group_summary.html.erb b/app/views/transactions/categorizes/_group_summary.html.erbindex eeaa0dc140..0477fe7e12 100644--- a/app/views/transactions/categorizes/_group_summary.html.erb+++ b/app/views/transactions/categorizes/_group_summary.html.erb@@ -2,6 +2,6 @@<p class="text-sm text-secondary"><%= t("transactions.categorizes.show.transaction_count", count: entries.size) %>·- <%= format_money(entries.sum { |e| e.amount_money.abs }) %>+ <span class="privacy-sensitive"><%= format_money(entries.sum { |e| e.amount_money.abs }) %></span></p><% end %>diff --git a/app/views/transactions/categorizes/show.html.erb b/app/views/transactions/categorizes/show.html.erbindex 59d49f61b5..e5cb1f20e3 100644--- a/app/views/transactions/categorizes/show.html.erb+++ b/app/views/transactions/categorizes/show.html.erb@@ -29,7 +29,7 @@<p class="text-sm text-secondary"><%= t(".transaction_count", count: @group.entries.size) %>·- <%= format_money(@group.entries.sum { |e| e.amount_money.abs }) %>+ <span class="privacy-sensitive"><%= format_money(@group.entries.sum { |e| e.amount_money.abs }) %></span></p><% end %></div>