The personal finance app for everyone (by everyone)
Ruby
fix(trades): let the header call a trade what the importer recorded (#3150)
buzzromained28fab807ca
diff --git a/app/views/trades/_header.html.erb b/app/views/trades/_header.html.erbindex eea444354a..c7aff25b24 100644--- a/app/views/trades/_header.html.erb+++ b/app/views/trades/_header.html.erb@@ -4,9 +4,16 @@<%= tag.header class: "mb-4 space-y-1" do %><% label = entry.trade.investment_activity_label %><% income_trade = %w[Dividend Interest].include?(label) %>+ <%# The label is what the importer actually recorded, so it wins over the+ amount's sign: a transfer between two accounts you own is neither a+ purchase nor a sale, and calling it one misreads the event. The sign is+ still the fallback for trades that carry no label at all, and for a+ label this view has no wording for. %>+ <% activity_key = label.presence&.parameterize(separator: "_") %>+ <% named_activity = activity_key.present? && I18n.exists?("trades.header.#{activity_key}") %><span class="text-secondary text-sm">- <% if income_trade %>- <%= t(".#{label.downcase}") %>+ <% if named_activity %>+ <%= t(".#{activity_key}") %><% else %><%= entry.amount.positive? ? t(".buy") : t(".sell") %><% end %>diff --git a/config/locales/views/trades/en.yml b/config/locales/views/trades/en.ymlindex 72577b5006..5f625740d4 100644--- a/config/locales/views/trades/en.yml+++ b/config/locales/views/trades/en.yml@@ -26,6 +26,15 @@ en:sell: Selldividend: Dividendinterest: Interest+ contribution: Contribution+ exchange: Exchange+ fee: Fee+ other: Other+ reinvestment: Reinvestment+ sweep_in: Sweep in+ sweep_out: Sweep out+ transfer: Transfer+ withdrawal: Withdrawalcurrent_market_price_label: Current Market Priceoverview: Overviewpurchase_price_label: Purchase Pricediff --git a/test/controllers/trades_controller_test.rb b/test/controllers/trades_controller_test.rbindex e93c63246f..775cb305b8 100644--- a/test/controllers/trades_controller_test.rb+++ b/test/controllers/trades_controller_test.rb@@ -8,6 +8,45 @@ class TradesControllerTest < ActionDispatch::IntegrationTest@entry = entries(:trade)end+ # The header used to read the amount's sign and call every trade a buy or a+ # sell, so an inbound transfer — Questrade journals one, and so do the+ # self-custody wallets — was shown as a purchase it never was.+ test "the header calls a labelled trade what it is, not a buy or a sell" do+ @entry.trade.update!(investment_activity_label: "Transfer")++ get trade_url(@entry)++ assert_response :success+ # Scoped to the header's own line: "Buy" also appears in the quick-edit+ # picker further down the page.+ assert_select "span.text-secondary.text-sm", text: I18n.t("trades.header.transfer")+ assert_select "span.text-secondary.text-sm", text: I18n.t("trades.header.buy"), count: 0+ end++ test "an unlabelled trade still reads from the amount" do+ @entry.trade.update!(investment_activity_label: nil)++ get trade_url(@entry)++ assert_response :success+ expected = @entry.amount.positive? ? I18n.t("trades.header.buy") : I18n.t("trades.header.sell")+ assert_select "span.text-secondary.text-sm", text: expected+ end++ # A label this view has no wording for must not blank the line out.+ test "an unknown label falls back rather than rendering nothing" do+ @entry.trade.update!(investment_activity_label: "Sweep In")+ I18n.backend.store_translations(:en, trades: { header: { sweep_in: nil } })++ get trade_url(@entry)++ assert_response :success+ expected = @entry.amount.positive? ? I18n.t("trades.header.buy") : I18n.t("trades.header.sell")+ assert_select "span.text-secondary.text-sm", text: expected+ ensure+ I18n.reload!+ end+test "updates trade entry" doassert_no_difference [ "Entry.count", "Trade.count" ] dopatch trade_url(@entry), params: {