Another step towards functional combobox

This commit is contained in:
Josh Pigford
2024-10-29 21:08:36 -04:00
parent 2919084734
commit b2a8ddf17a
6 changed files with 30 additions and 23 deletions

View File

@@ -37,8 +37,17 @@ class Account::TradesController < ApplicationController
query = params[:q]
return render json: [] if query.blank? || query.length < 2 || query.length > 100
synth_client = Provider::Synth.new(ENV["SYNTH_API_KEY"])
@securities = synth_client.search_securities(query:, dataset: "limited", country_code: Current.family.country).securities
@securities = Security.security_prices_provider.search_securities(query:, dataset: "limited", country_code: Current.family.country).securities.map do |security|
new_security = Security.new(
ticker: security[:symbol],
name: security[:name],
exchange_acronym: security[:exchange_acronym]
)
new_security.define_singleton_method(:logo_url) { security[:logo_url] }
new_security
end
end
private

View File

@@ -31,10 +31,12 @@ class Account::TradeBuilder < Account::EntryBuilder
end
def security
exchange_mic = ticker.match(/\((.*?)\)/)&.captures&.first
exchange_acronym = ticker.match(/\((.*?)\)/)&.captures&.first
ticker_symbol = ticker.gsub(/\s*\(.*?\)\s*/, "")
Security.find_or_create_by(ticker: ticker_symbol, exchange_mic: exchange_mic)
exchange = StockExchange.find_by(acronym: exchange_acronym)
Security.find_or_create_by(ticker: ticker_symbol, exchange_mic: exchange.mic)
end
def amount

View File

@@ -1,4 +1,5 @@
class Security < ApplicationRecord
include Providable
before_save :upcase_ticker
has_many :trades, dependent: :nullify, class_name: "Account::Trade"
@@ -14,7 +15,7 @@ class Security < ApplicationRecord
end
def to_combobox_display
"#{ticker} - #{name} (#{exchange_acronym})"
"#{ticker} (#{exchange_acronym})"
end

View File

@@ -0,0 +1,11 @@
<div class="flex items-center">
<%= image_tag(security.logo_url, class: "rounded-full h-8 w-8 inline-block mr-2" ) %>
<div class="flex flex-col">
<span class="text-sm font-medium">
<%= security.name.presence || security.ticker %>
</span>
<span class="text-xs text-gray-500">
<%= "#{security.ticker} (#{security.exchange_acronym})" %>
</span>
</div>
</div>

View File

@@ -1,11 +0,0 @@
<div class="flex items-center">
<%= image_tag(tickers[:logo_url], class: "rounded-full h-8 w-8 inline-block mr-2" ) %>
<div class="flex flex-col">
<span class="text-sm font-medium">
<%= tickers[:name].presence || tickers[:symbol] %>
</span>
<span class="text-xs text-gray-500">
<%= "#{tickers[:symbol]} (#{tickers[:exchange_acronym]})" %>
</span>
</div>
</div>

View File

@@ -1,7 +1,2 @@
<%= async_combobox_options @securities.map { |security|
{
display: security,
value: security[:symbol]
}
},
render_in: { partial: "account/trades/tickers" } %>
<%= async_combobox_options @securities,
render_in: { partial: "account/trades/security" } %>