The personal finance app for everyone (by everyone)
Ruby
feat(accounts): group split transactions in account activity feed (#3359)
GFR7e26fcb478ec
diff --git a/app/components/UI/account/activity_date.html.erb b/app/components/UI/account/activity_date.html.erbindex e788bfa4bc..67758b8a18 100644--- a/app/components/UI/account/activity_date.html.erb+++ b/app/components/UI/account/activity_date.html.erb@@ -38,8 +38,18 @@</details><div class="bg-container shadow-border-xs rounded-lg">- <% entries.each do |entry| %>- <%= render entry, view_ctx: "account" %>+ <% if Current.user.show_split_grouped? %>+ <% helpers.group_split_entries(entries, split_parents).each do |item| %>+ <% if item.is_a?(EntriesHelper::SplitGroup) %>+ <%= render "entries/split_group", split_group: item, view_ctx: "account" %>+ <% else %>+ <%= render item, view_ctx: "account" %>+ <% end %>+ <% end %>+ <% else %>+ <% entries.each do |entry| %>+ <%= render entry, view_ctx: "account" %>+ <% end %><% end %></div><% end %>diff --git a/app/components/UI/account/activity_date.rb b/app/components/UI/account/activity_date.rbindex 9de67f8fe5..667435a696 100644--- a/app/components/UI/account/activity_date.rb+++ b/app/components/UI/account/activity_date.rb@@ -1,7 +1,7 @@class UI::Account::ActivityDate < ApplicationComponentattr_reader :account, :data- delegate :date, :entries, :balance, :transfers, to: :data+ delegate :date, :entries, :balance, :transfers, :split_parents, to: :datadef initialize(account:, data:)@account = accountdiff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rbindex 981ca499f5..8f36c864d7 100644--- a/app/controllers/accounts_controller.rb+++ b/app/controllers/accounts_controller.rb@@ -129,7 +129,21 @@ class AccountsController < ApplicationControllerSet.newend- @activity_feed_data = Account::ActivityFeedData.new(@account, @entries)+ # Load split parent entries for grouped display (only when grouping is enabled)+ @split_parents = if Current.user.show_split_grouped?+ split_parent_ids = @entries.filter_map(&:parent_entry_id).uniq+ if split_parent_ids.any?+ Entry.where(id: split_parent_ids)+ .includes(:account, entryable: [ :category, :merchant ])+ .index_by(&:id)+ else+ {}+ end+ else+ {}+ end++ @activity_feed_data = Account::ActivityFeedData.new(@account, @entries, split_parents: @split_parents)enddef syncdiff --git a/app/models/account/activity_feed_data.rb b/app/models/account/activity_feed_data.rbindex 7d1c41bdfa..78099065d9 100644--- a/app/models/account/activity_feed_data.rb+++ b/app/models/account/activity_feed_data.rb@@ -2,13 +2,14 @@# This data object is useful for avoiding N+1 queries and having an easy way to pass around the required data to the# activity feed component in controllers and background jobs that refresh it.class Account::ActivityFeedData- ActivityDateData = Data.define(:date, :entries, :balance, :transfers)+ ActivityDateData = Data.define(:date, :entries, :balance, :transfers, :split_parents)- attr_reader :account, :entries+ attr_reader :account, :entries, :split_parents- def initialize(account, entries)+ def initialize(account, entries, split_parents: {})@account = account@entries = entries.to_a+ @split_parents = split_parentsenddef entries_by_date@@ -18,7 +19,8 @@ class Account::ActivityFeedDatadate: date,entries: date_entries,balance: balance_for_date(date),- transfers: transfers_for_date(date)+ transfers: transfers_for_date(date),+ split_parents: split_parents)endenddiff --git a/app/views/entries/_split_group.html.erb b/app/views/entries/_split_group.html.erbindex 978f3d019d..6b2cfb9727 100644--- a/app/views/entries/_split_group.html.erb+++ b/app/views/entries/_split_group.html.erb@@ -1,10 +1,10 @@-<%# locals: (split_group:) %>+<%# locals: (split_group:, view_ctx: "global") %><div class="split-group"><%= render "transactions/split_parent_row", entry: split_group.parent %><% split_group.children.each do |child_entry| %><% if child_entry.entryable.present? %><%= render partial: child_entry.entryable.to_partial_path,- locals: { entry: child_entry, in_split_group: true } %>+ locals: { entry: child_entry, in_split_group: true, view_ctx: view_ctx } %><% end %><% end %></div>diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rbindex 6f0447c554..2bb01af1ba 100644--- a/test/controllers/accounts_controller_test.rb+++ b/test/controllers/accounts_controller_test.rb@@ -3,6 +3,7 @@ require "test_helper"class AccountsControllerTest < ActionDispatch::IntegrationTestinclude ActionView::RecordIdentifierinclude OnchainTestHelper+ include EntriesTestHelpersetup dosign_in @user = users(:family_admin)@@ -139,6 +140,59 @@ class AccountsControllerTest < ActionDispatch::IntegrationTestassert_equal 0, per_row_split, "N+1 per-row split-parent queries detected (#{per_row_split})"end+ test "show groups split transactions into a single split-group row when grouping is enabled" do+ @user.update!(preferences: { "show_split_grouped" => true })+ entry = create_transaction(name: "Grocery Store", amount: 100, account: @account)+ entry.split!([+ { name: "Food", amount: 60 },+ { name: "Household", amount: 40 }+ ])++ get account_url(@account)++ assert_response :success+ assert_select ".split-group", count: 1+ assert_select ".split-group" do+ assert_select "p", text: "Food", count: 0+ end+ end++ test "show renders split children as flat rows when grouping is disabled" do+ @user.update!(preferences: { "show_split_grouped" => false })+ entry = create_transaction(name: "Grocery Store", amount: 100, account: @account)+ entry.split!([+ { name: "Food", amount: 60 },+ { name: "Household", amount: 40 }+ ])++ get account_url(@account)++ assert_response :success+ assert_select ".split-group", count: 0+ end++ test "show avoids N+1 queries when loading split parents for grouped display" do+ @user.update!(preferences: { "show_split_grouped" => true })+ 3.times do |i|+ entry = create_transaction(name: "Grocery Store #{i}", amount: 100, account: @account)+ entry.split!([+ { name: "Food", amount: 60 },+ { name: "Household", amount: 40 }+ ])+ end++ queries = capture_sql_queries { get account_url(@account) }+ assert_response :success++ # @split_parents loads all referenced split-parent entries in a single+ # `WHERE "entries"."id" IN (...)` query — a per-row `"id" = $1` lookup+ # would indicate the batching regressed into N+1.+ per_row_split_parent = queries.count { |q|+ q.match?(/FROM "entries".*WHERE.*"entries"\."id" = \$?\d+/) && !q.include?(" IN (")+ }+ assert_equal 0, per_row_split_parent, "N+1 per-row split-parent lookups detected (#{per_row_split_parent})"+ end+test "show lazily loads statement tab data unless statements tab is active" doAccountStatement::Coverage.expects(:for_year).neverAccountStatement.expects(:reconciliation_statuses_for).never