The personal finance app for everyone (by everyone)
Ruby
Show exact share count in the holding drawer (#3305)
Justin McBrideaba8a8eb3c95
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex d8d92c0316..eff64a7af4 100644--- a/app/helpers/application_helper.rb+++ b/app/helpers/application_helper.rb@@ -226,19 +226,21 @@ module ApplicationHelperENV.fetch("DEV_WEBHOOKS_URL", root_url).chomp("/") + "/enable_banking_items/callback"end- # Formats quantity with adaptive precision based on the value size.+ # Formats a holding quantity with adaptive precision based on the value size.# Shows more decimal places for small quantities (common with crypto).## @param qty [Numeric] The quantity to format- # @param max_precision [Integer] Maximum precision for very small numbers+ # @param exact [Boolean] Show the full stored precision, without rounding# @return [String] Formatted quantity with appropriate precision- def format_quantity(qty)+ def format_quantity(qty, exact: false)return "0" if qty.nil? || qty.zero?abs_qty = qty.abs- precision = if abs_qty >= 1- 1 # "10.5"+ precision = if exact+ 8 # "10.374"+ elsif abs_qty >= 1+ 1 # "10.4"elsif abs_qty >= 0.012 # "0.52"elsif abs_qty >= 0.0001diff --git a/app/views/holdings/show.html.erb b/app/views/holdings/show.html.erbindex e382fda311..14d7cfd5c9 100644--- a/app/views/holdings/show.html.erb+++ b/app/views/holdings/show.html.erb@@ -96,7 +96,7 @@</div><div class="flex items-center justify-between text-sm"><dt class="text-secondary"><%= t(".shares_label") %></dt>- <dd class="text-primary"><%= format_quantity(@holding.qty) %></dd>+ <dd id="<%= dom_id(@holding, :shares) %>" class="text-primary"><%= format_quantity(@holding.qty, exact: true) %></dd></div><div class="flex items-center justify-between text-sm"><dt class="text-secondary"><%= t(".portfolio_weight_label") %></dt>@@ -141,7 +141,7 @@class: "space-y-3",data: drawer_form_data do |f| %><p class="text-xs text-secondary mb-2">- <%= t("holdings.cost_basis_cell.set_cost_basis_header", ticker: @holding.ticker, qty: format_quantity(@holding.qty)) %>+ <%= t("holdings.cost_basis_cell.set_cost_basis_header", ticker: @holding.ticker, qty: format_quantity(@holding.qty, exact: true)) %></p><!-- Total cost basis input --><div class="form-field">diff --git a/test/controllers/holdings_controller_test.rb b/test/controllers/holdings_controller_test.rbindex dfa52d4994..6e88a2a196 100644--- a/test/controllers/holdings_controller_test.rb+++ b/test/controllers/holdings_controller_test.rb@@ -18,6 +18,14 @@ class HoldingsControllerTest < ActionDispatch::IntegrationTestassert_response :successend+ test "shows exact share count without rounding" do+ @holding.update!(qty: 10.374)++ get holding_path(@holding)++ assert_select "##{dom_id(@holding, :shares)}", text: "10.374"+ end+test "destroys holding and associated entries" doassert_difference -> { Holding.count } => -1,-> { Entry.count } => -1 do