2024-07-16 09:26:49 -04:00
|
|
|
class Security < ApplicationRecord
|
2025-02-28 11:35:10 -05:00
|
|
|
include Provided
|
2025-02-28 09:34:14 -05:00
|
|
|
|
2025-05-08 14:31:43 -04:00
|
|
|
before_validation :upcase_symbols
|
2024-07-16 09:26:49 -04:00
|
|
|
|
2025-04-14 11:40:34 -04:00
|
|
|
has_many :trades, dependent: :nullify, class_name: "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
|
|
|
|
2025-05-22 12:43:24 -04:00
|
|
|
scope :online, -> { where(offline: false) }
|
|
|
|
|
2024-10-09 14:59:18 -04:00
|
|
|
def current_price
|
2025-03-17 11:54:53 -04:00
|
|
|
@current_price ||= find_or_fetch_price
|
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,
|
2025-02-11 10:40:30 -06:00
|
|
|
exchange_operating_mic: exchange_operating_mic,
|
2025-05-22 09:45:08 -05:00
|
|
|
country_code: country_code
|
2024-11-27 16:01:50 -05:00
|
|
|
)
|
2024-10-28 15:49:19 -04:00
|
|
|
end
|
|
|
|
|
2024-07-16 09:26:49 -04:00
|
|
|
private
|
2025-05-08 14:31:43 -04:00
|
|
|
def upcase_symbols
|
2024-08-01 19:43:23 -04:00
|
|
|
self.ticker = ticker.upcase
|
2025-05-08 14:31:43 -04:00
|
|
|
self.exchange_operating_mic = exchange_operating_mic.upcase if exchange_operating_mic.present?
|
2024-07-16 09:26:49 -04:00
|
|
|
end
|
|
|
|
end
|