2024-07-16 09:26:49 -04:00
|
|
|
class Security < ApplicationRecord
|
2024-10-30 09:23:44 -04:00
|
|
|
include Providable
|
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-10-29 15:37:59 -04:00
|
|
|
has_many :prices, dependent: :destroy
|
2024-07-16 09:26:49 -04:00
|
|
|
|
2024-10-25 13:09:02 -05:00
|
|
|
validates :ticker, presence: true
|
2025-02-11 10:40:30 -06:00
|
|
|
validates :ticker, uniqueness: { scope: :exchange_operating_mic, case_sensitive: false }
|
2024-07-16 09:26:49 -04:00
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
class << self
|
|
|
|
def search(query)
|
|
|
|
security_prices_provider.search_securities(
|
|
|
|
query: query[:search],
|
|
|
|
dataset: "limited",
|
2025-02-24 16:00:24 +01:00
|
|
|
country_code: query[:country],
|
|
|
|
exchange_operating_mic: query[:exchange_operating_mic]
|
2024-11-27 16:01:50 -05:00
|
|
|
).securities.map { |attrs| new(**attrs) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-09 14:59:18 -04:00
|
|
|
def current_price
|
2024-10-29 15:37:59 -04:00
|
|
|
@current_price ||= Security::Price.find_price(security: self, date: Date.current)
|
2024-10-09 14:59:18 -04:00
|
|
|
return nil if @current_price.nil?
|
|
|
|
Money.new(@current_price.price, @current_price.currency)
|
|
|
|
end
|
|
|
|
|
2024-11-27 16:01:50 -05:00
|
|
|
def to_combobox_option
|
|
|
|
SynthComboboxOption.new(
|
|
|
|
symbol: ticker,
|
|
|
|
name: name,
|
|
|
|
logo_url: logo_url,
|
|
|
|
exchange_acronym: exchange_acronym,
|
2025-02-11 10:40:30 -06:00
|
|
|
exchange_operating_mic: exchange_operating_mic,
|
2024-11-27 16:01:50 -05:00
|
|
|
exchange_country_code: country_code
|
|
|
|
)
|
2024-10-28 15:49:19 -04:00
|
|
|
end
|
|
|
|
|
2025-02-11 10:40:30 -06:00
|
|
|
def has_prices?
|
|
|
|
exchange_operating_mic.present?
|
|
|
|
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
|