mirror of
https://github.com/we-promise/sure.git
synced 2026-04-09 07:14:47 +00:00
* Record dividends and interest as Trades in investment accounts
All investment income (dividends and interest) is now modeled as a
Trade with qty: 0 and price: 0, keeping security_id NOT NULL on trades
intact. Dividends require a security; interest falls back to a
per-account synthetic cash security (kind: "cash", offline: true) when
none is selected, matching how brokerages handle uninvested cash
internally.
- Add `kind` column to securities ("standard" | "cash") with DB check
constraint; `Security.cash_for(account)` lazily finds or creates the
synthetic cash security; `scope :standard` excludes synthetic
securities from user-facing pickers
- Trade::CreateForm: new `dividend` type (security required); `interest`
now creates a Trade instead of a Transaction
- Trade form: Dividend and Interest in the type dropdown with a security
combobox (required for dividend, optional for interest)
- transactions table: untouched
* UI fixes
* HealthChecker — both scopes now chain .standard to exclude cash securities from provider health checks.
DB query moved to model — Account#traded_standard_securities in app/models/account.rb, view uses account.traded_standard_securities.
DRY income creation — create_income_trade(sec:, label:, name:) extracted as shared private method; create_dividend_income and create_interest_income delegate to it.
show.html.erb blocks merged — single unless trade.qty.zero? block covers qty/price/fee fields.
Test extended — assert_response :unprocessable_entity added after the assert_no_difference block.
* Hide cash account ticker from no-security trade detail
* Fix CodeRabbit review issues from PR #1311
- Remove duplicate YAML keys in translation files (de, es, fr)
- Add error handling for security resolution in create_dividend_income
- Extract income trade check to reduce duplication in header template
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
* Include holdings in dividend/interest security picker
The security picker for dividend/interest trades should include all securities
in holdings, not just those with trade history. This fixes the issue where
accounts with imported holdings (e.g., SimpleFIN) but no trades would have an
empty picker and be unable to record dividends.
Uses UNION to combine securities from both trades and holdings.
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
* scope picker to holdings only (a trade creates a holding anyway)
---------
Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
72 lines
2.4 KiB
Ruby
72 lines
2.4 KiB
Ruby
require "test_helper"
|
|
|
|
class SecurityTest < ActiveSupport::TestCase
|
|
# Below has 3 example scenarios:
|
|
# 1. Original ticker
|
|
# 2. Duplicate ticker on a different exchange (different market price)
|
|
# 3. "Offline" version of the same ticker (for users not connected to a provider)
|
|
test "can have duplicate tickers if exchange is different" do
|
|
original = Security.create!(ticker: "TEST", exchange_operating_mic: "XNAS")
|
|
duplicate = Security.create!(ticker: "TEST", exchange_operating_mic: "CBOE")
|
|
offline = Security.create!(ticker: "TEST", exchange_operating_mic: nil)
|
|
|
|
assert original.valid?
|
|
assert duplicate.valid?
|
|
assert offline.valid?
|
|
end
|
|
|
|
test "cannot have duplicate tickers if exchange is the same" do
|
|
original = Security.create!(ticker: "TEST", exchange_operating_mic: "XNAS")
|
|
duplicate = Security.new(ticker: "TEST", exchange_operating_mic: "XNAS")
|
|
|
|
assert_not duplicate.valid?
|
|
assert_equal [ "has already been taken" ], duplicate.errors[:ticker]
|
|
end
|
|
|
|
test "cannot have duplicate tickers if exchange is nil" do
|
|
original = Security.create!(ticker: "TEST", exchange_operating_mic: nil)
|
|
duplicate = Security.new(ticker: "TEST", exchange_operating_mic: nil)
|
|
|
|
assert_not duplicate.valid?
|
|
assert_equal [ "has already been taken" ], duplicate.errors[:ticker]
|
|
end
|
|
|
|
test "casing is ignored when checking for duplicates" do
|
|
original = Security.create!(ticker: "TEST", exchange_operating_mic: "XNAS")
|
|
duplicate = Security.new(ticker: "tEst", exchange_operating_mic: "xNaS")
|
|
|
|
assert_not duplicate.valid?
|
|
assert_equal [ "has already been taken" ], duplicate.errors[:ticker]
|
|
end
|
|
|
|
test "cash_for lazily creates a per-account synthetic cash security" do
|
|
account = accounts(:investment)
|
|
|
|
cash = Security.cash_for(account)
|
|
|
|
assert cash.persisted?
|
|
assert cash.cash?
|
|
assert cash.offline?
|
|
assert_equal "Cash", cash.name
|
|
assert_includes cash.ticker, account.id.upcase
|
|
end
|
|
|
|
test "cash_for returns the same security on repeated calls" do
|
|
account = accounts(:investment)
|
|
|
|
first = Security.cash_for(account)
|
|
second = Security.cash_for(account)
|
|
|
|
assert_equal first.id, second.id
|
|
end
|
|
|
|
test "standard scope excludes cash securities" do
|
|
account = accounts(:investment)
|
|
Security.cash_for(account)
|
|
|
|
standard_tickers = Security.standard.pluck(:ticker)
|
|
|
|
assert_not_includes standard_tickers, "CASH-#{account.id.upcase}"
|
|
end
|
|
end
|