Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Count the same accounts in group totals and sparklines (#4338)

GFR9c8dbb3e94ea
diff --git a/app/controllers/accountable_sparklines_controller.rb b/app/controllers/accountable_sparklines_controller.rb
index 9b67ba4656..4127a6f0b3 100644
--- a/app/controllers/accountable_sparklines_controller.rb
+++ b/app/controllers/accountable_sparklines_controller.rb
@@ -19,10 +19,15 @@ class AccountableSparklinesController < ApplicationController
@family ||= Current.family
end
- # Scope to the accounts the current user can access (owned or shared),
- # matching the account list and group total rendered in the sidebar.
+ # Same accounts the sidebar group total sums: accessible to the current
+ # user, visible, included in reports and counted in their finances (owned,
+ # or shared with include_in_finances).
def account_scope
- @account_scope ||= Current.accessible_accounts.visible.where(accountable_type: @accountable.name)
+ @account_scope ||= Current.accessible_accounts
+ .visible
+ .included_in_reports
+ .included_in_finances_for(Current.user)
+ .where(accountable_type: @accountable.name)
end
def account_ids
@@ -71,9 +76,9 @@ class AccountableSparklinesController < ApplicationController
# The series is built from exactly these account rows, so the key carries a
# digest of them. A share count/timestamp version can stay the same while
- # access changes (ownership moved by update_all, shares replaced with older
- # timestamps); the row digest changes whenever the accessible set does and
- # never repeats for a different set.
+ # the counted set changes (ownership moved by update_all, shares replaced
+ # with older timestamps, report exclusion toggled); the row digest changes
+ # whenever the set does and never repeats for a different set.
def cache_key
family.build_cache_key(
"#{@accountable.name}_sparkline_#{Account::Chartable::SPARKLINE_CACHE_VERSION}_#{account_rows_digest}",
diff --git a/app/helpers/accounts_helper.rb b/app/helpers/accounts_helper.rb
index 1e12be4bcc..616c79a757 100644
--- a/app/helpers/accounts_helper.rb
+++ b/app/helpers/accounts_helper.rb
@@ -56,7 +56,7 @@ module AccountsHelper
end
[
- family.build_cache_key("account_sidebar_tabs_v3", invalidate_on_data_updates: true),
+ family.build_cache_key("account_sidebar_tabs_v4", invalidate_on_data_updates: true),
Current.user&.id,
shares_version,
active_tab,
diff --git a/app/models/balance_sheet/account_group.rb b/app/models/balance_sheet/account_group.rb
index 3ad275f34e..2ab20c0df6 100644
--- a/app/models/balance_sheet/account_group.rb
+++ b/app/models/balance_sheet/account_group.rb
@@ -33,8 +33,10 @@ class BalanceSheet::AccountGroup
accountable_type.to_s.underscore
end
+ # Counts the same accounts as the classification total and net worth, so the
+ # group weights add up and the sidebar sparkline matches this figure.
def total
- accounts.reject { |a| a.respond_to?(:exclude_from_reports?) && a.exclude_from_reports? }.sum(&:converted_balance)
+ BalanceSheet::ClassificationGroup.counted_accounts(accounts).sum(&:converted_balance)
end
def weight
diff --git a/app/models/balance_sheet/classification_group.rb b/app/models/balance_sheet/classification_group.rb
index 50d20ceec4..c90e15a6f2 100644
--- a/app/models/balance_sheet/classification_group.rb
+++ b/app/models/balance_sheet/classification_group.rb
@@ -20,10 +20,15 @@ class BalanceSheet::ClassificationGroup
classification == "asset" ? "plus" : "minus"
end
- def total
+ # Accounts that count towards totals: included in the viewer's finances and
+ # not excluded from reports. Account groups use the same rule.
+ def self.counted_accounts(accounts)
accounts.select { |a| a.respond_to?(:included_in_finances?) ? a.included_in_finances? : true }
.reject { |a| a.respond_to?(:exclude_from_reports?) && a.exclude_from_reports? }
- .sum(&:converted_balance)
+ end
+
+ def total
+ self.class.counted_accounts(accounts).sum(&:converted_balance)
end
def syncing?
diff --git a/app/views/accounts/_account_sidebar_tabs.html.erb b/app/views/accounts/_account_sidebar_tabs.html.erb
index d1115a04c3..dd4664bf06 100644
--- a/app/views/accounts/_account_sidebar_tabs.html.erb
+++ b/app/views/accounts/_account_sidebar_tabs.html.erb
@@ -4,7 +4,7 @@
ERB dependency tracker mis-parses as a bogus "Ds/D" template dependency
and then logs "Couldn't find template for digesting: Ds/D" on every cache
miss. The cache key is already manually versioned via
- account_sidebar_tabs_cache_key ("account_sidebar_tabs_v3",
+ account_sidebar_tabs_cache_key ("account_sidebar_tabs_v4",
invalidate_on_data_updates: true), so we opt out of automatic template
digesting. Bump that version suffix when changing this template or its
sidebar dependencies. %>
diff --git a/test/controllers/accountable_sparklines_controller_test.rb b/test/controllers/accountable_sparklines_controller_test.rb
index a998027be6..624e01db53 100644
--- a/test/controllers/accountable_sparklines_controller_test.rb
+++ b/test/controllers/accountable_sparklines_controller_test.rb
@@ -153,7 +153,36 @@ class AccountableSparklinesControllerTest < ActionDispatch::IntegrationTest
assert_match(/SELECT .*"accounts"\."id".*"account_providers"\."id" FROM "accounts"/, queries.join("\n"))
assert_empty queries.grep(/SELECT "accounts"\.\* FROM "accounts"/)
assert_empty queries.grep(/SELECT 1 AS one FROM "accounts".*JOIN "account_providers"/)
- assert_empty queries.grep(/SELECT "accounts"\."id" FROM "accounts" WHERE "accounts"\."family_id" = .*"accounts"\."status" IN .*"accounts"\."accountable_type" =/)
+ assert_empty queries.grep(/SELECT (DISTINCT )?"accounts"\."id" FROM "accounts" .*"accounts"\."accountable_type" =/)
+ end
+
+ test "sparkline skips accounts excluded from reports" do
+ accounts(:connected).update!(exclude_from_reports: true)
+
+ account_ids = sparkline_account_ids("depository")
+
+ assert_includes account_ids, accounts(:depository).id
+ assert_not_includes account_ids, accounts(:connected).id
+ end
+
+ test "sparkline skips shared accounts not included in the member's finances" do
+ sign_in users(:family_member)
+ account_shares(:depository_shared_with_member).update!(include_in_finances: false)
+
+ assert_not_includes sparkline_account_ids("depository"), accounts(:depository).id
+ end
+
+ test "sparkline cache follows the member's finance setting" do
+ Rails.stubs(:cache).returns(ActiveSupport::Cache::MemoryStore.new)
+ sign_in users(:family_member)
+ share = account_shares(:depository_shared_with_member)
+
+ assert_includes sparkline_account_ids("depository"), accounts(:depository).id
+
+ # Only the share changes, so the family's data-update timestamps stay put.
+ share.update_columns(include_in_finances: false)
+
+ assert_not_includes sparkline_account_ids("depository", clear_cache: false), accounts(:depository).id
end
private
@@ -161,6 +190,19 @@ class AccountableSparklinesControllerTest < ActionDispatch::IntegrationTest
Series.new(start_date: 1.day.ago.to_date, end_date: Date.current, interval: "1 day", values: [])
end
+ def sparkline_account_ids(accountable_key, clear_cache: true)
+ Rails.cache.clear if clear_cache
+ account_ids = nil
+ builder = mock("chart_series_builder")
+ builder.stubs(:balance_series).returns(empty_series)
+ Balance::ChartSeriesBuilder.expects(:new).with { |args| account_ids = args[:account_ids]; true }.returns(builder)
+
+ get accountable_sparkline_url(accountable_key)
+
+ assert_response :success
+ account_ids
+ end
+
def capture_sql_queries
queries = []
callback = lambda do |_name, _started, _finished, _unique_id, payload|
diff --git a/test/models/balance_sheet_test.rb b/test/models/balance_sheet_test.rb
index 97883262dd..0351c5308f 100644
--- a/test/models/balance_sheet_test.rb
+++ b/test/models/balance_sheet_test.rb
@@ -161,6 +161,21 @@ class BalanceSheetTest < ActiveSupport::TestCase
assert_not_includes account_ids, excluded_account.id
end
+ test "group totals skip shared accounts not included in the member's finances" do
+ member = users(:new_email)
+ included_account = create_account(balance: 1000, accountable: Depository.new)
+ excluded_account = create_account(balance: 2000, accountable: Depository.new)
+ included_account.share_with!(member, include_in_finances: true)
+ excluded_account.share_with!(member, include_in_finances: false)
+
+ assets = BalanceSheet.new(@family, user: member).assets
+ depository_group = assets.account_groups.find { |ag| ag.name == Depository.display_name }
+
+ assert_includes depository_group.accounts.map(&:id), excluded_account.id
+ assert_equal 1000, depository_group.total
+ assert_equal assets.total, assets.account_groups.sum(&:total)
+ end
+
test "calculates asset group totals" do
create_account(balance: 1000, accountable: Depository.new)
create_account(balance: 2000, accountable: Depository.new)