fix(trades): guard against nil security in create_trade (#2066)

Co-authored-by: sentry[bot] <39604003+sentry[bot]@users.noreply.github.com>
This commit is contained in:
sentry[bot]
2026-06-19 16:54:37 +02:00
committed by GitHub
parent 6ae9779567
commit 84df335e7c
2 changed files with 11 additions and 2 deletions

View File

@@ -37,11 +37,19 @@ class Trade::CreateForm
end
def create_trade
sec = security
unless sec
entry = account.entries.build(entryable: Trade.new)
entry.errors.add(:base, I18n.t("trades.form.trade_requires_security"))
return entry
end
signed_qty = type == "sell" ? -qty.to_d : qty.to_d
signed_amount = signed_qty * price.to_d + fee.to_d
trade_entry = account.entries.new(
name: Trade.build_name(type, qty, security.ticker),
name: Trade.build_name(type, qty, sec.ticker),
date: date,
amount: signed_amount,
currency: currency,
@@ -50,7 +58,7 @@ class Trade::CreateForm
price: price,
fee: fee.to_d,
currency: currency,
security: security,
security: sec,
investment_activity_label: type.capitalize # "buy" → "Buy", "sell" → "Sell"
)
)

View File

@@ -20,6 +20,7 @@ en:
type_dividend: Dividend
type_interest: Interest
dividend_requires_security: Security is required for dividends
trade_requires_security: A security (ticker) is required for buy and sell trades
header:
buy: Buy
sell: Sell