1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-18 20:59:39 +02:00
Maybe/app/models/account/trade_builder.rb
Josh Pigford cd91e66618
Initial pass at Synth-based ticker selection (#1392)
* Initial pass at Synth-based ticker selection

* Update _tickers.turbo_stream.erb

* Functional combobox display

* A few cleanup steps

* Linter

* Prevent long strings

* Another step towards functional combobox

* Deprecated files

* Custom Combobox implementation

* Lint

* Test suite fixes

* Lint

* Make direct use of mic codes

* Update splits

* Update trades_test.rb
2024-10-30 09:23:44 -04:00

48 lines
1 KiB
Ruby

class Account::TradeBuilder < Account::EntryBuilder
include ActiveModel::Model
TYPES = %w[buy sell].freeze
attr_accessor :type, :qty, :price, :ticker, :date, :account
validates :type, :qty, :price, :ticker, :date, presence: true
validates :price, numericality: { greater_than: 0 }
validates :type, inclusion: { in: TYPES }
def save
if valid?
create_entry
end
end
private
def create_entry
account.entries.account_trades.create! \
date: date,
amount: amount,
currency: account.currency,
entryable: Account::Trade.new(
security: security,
qty: signed_qty,
price: price.to_d,
currency: account.currency
)
end
def security
ticker_symbol, exchange_mic = ticker.split("|")
Security.find_or_create_by(ticker: ticker_symbol, exchange_mic: exchange_mic)
end
def amount
price.to_d * signed_qty
end
def signed_qty
_qty = qty.to_d
_qty = _qty * -1 if type == "sell"
_qty
end
end