2024-07-16 09:26:49 -04:00
|
|
|
class Security < ApplicationRecord
|
2024-08-01 19:43:23 -04:00
|
|
|
before_save :upcase_ticker
|
2024-07-16 09:26:49 -04:00
|
|
|
|
|
|
|
has_many :trades, dependent: :nullify, class_name: "Account::Trade"
|
|
|
|
|
2024-08-01 19:43:23 -04:00
|
|
|
validates :ticker, presence: true, uniqueness: { case_sensitive: false }
|
2024-07-16 09:26:49 -04:00
|
|
|
|
2024-10-09 14:59:18 -04:00
|
|
|
def current_price
|
|
|
|
@current_price ||= Security::Price.find_price(ticker:, date: Date.current)
|
|
|
|
return nil if @current_price.nil?
|
|
|
|
Money.new(@current_price.price, @current_price.currency)
|
|
|
|
end
|
|
|
|
|
2024-07-16 09:26:49 -04:00
|
|
|
private
|
|
|
|
|
2024-08-01 19:43:23 -04:00
|
|
|
def upcase_ticker
|
|
|
|
self.ticker = ticker.upcase
|
2024-07-16 09:26:49 -04:00
|
|
|
end
|
|
|
|
end
|