mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 23:59:40 +02:00
Stock filter (#1376)
* Initial pass at stock filtering * Rough in filter * Cleaning up security listing * Tweak to search function * Combobox tweaks * Clean up search query * Update trades test with combobox * Update securities.yml
This commit is contained in:
parent
c2561b5fb4
commit
7d8028b505
15 changed files with 104 additions and 7 deletions
|
@ -6,12 +6,36 @@ class Security < ApplicationRecord
|
|||
validates :ticker, presence: true
|
||||
validates :ticker, uniqueness: { scope: :exchange_mic, case_sensitive: false }
|
||||
|
||||
scope :search, ->(query) {
|
||||
return none if query.blank? || query.length < 2
|
||||
|
||||
# Clean and normalize the search terms
|
||||
sanitized_query = query.split.map do |term|
|
||||
cleaned_term = term.gsub(/[^a-zA-Z0-9]/, " ").strip
|
||||
next if cleaned_term.blank?
|
||||
cleaned_term
|
||||
end.compact.join(" | ")
|
||||
|
||||
return none if sanitized_query.blank?
|
||||
|
||||
sanitized_query = ActiveRecord::Base.connection.quote(sanitized_query)
|
||||
|
||||
where("search_vector @@ to_tsquery('simple', #{sanitized_query}) AND exchange_mic IS NOT NULL")
|
||||
.select("securities.*, ts_rank_cd(search_vector, to_tsquery('simple', #{sanitized_query})) AS rank")
|
||||
.reorder("rank DESC")
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
def to_combobox_display
|
||||
"#{ticker} - #{name} (#{exchange_acronym})"
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def upcase_ticker
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue