mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 20:14:08 +00:00
Improve handling of cost_basis during holding materialization and display (#619)
- Refactored `persist_holdings` to separate and conditionally upsert holdings with and without cost_basis. - Updated `avg_cost` logic to treat 0 cost_basis as unknown and return nil when cost_basis cannot be determined. - Modified trend and investment calculation to exclude holdings with unknown cost_basis. - Adjusted `average_cost` formatting to handle nil values in API responses and views. - Added comprehensive tests to ensure cost_basis preservation and fallback behavior. - Localized `unknown` label for display when cost_basis is unavailable. Co-authored-by: Josh Waldrep <joshua.waldrep5+github@gmail.com>
This commit is contained in:
@@ -133,8 +133,12 @@ class InvestmentStatement
|
||||
holdings = current_holdings.to_a
|
||||
return nil if holdings.empty?
|
||||
|
||||
current = holdings.sum(&:amount)
|
||||
previous = holdings.sum { |h| h.qty * h.avg_cost.amount }
|
||||
# Only include holdings with known cost basis in the calculation
|
||||
holdings_with_cost_basis = holdings.select(&:avg_cost)
|
||||
return nil if holdings_with_cost_basis.empty?
|
||||
|
||||
current = holdings_with_cost_basis.sum(&:amount)
|
||||
previous = holdings_with_cost_basis.sum { |h| h.qty * h.avg_cost.amount }
|
||||
|
||||
Trend.new(current: current, previous: previous)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user