From b2a8ddf17a2f53ef5632bdc22b3917c1fa309448 Mon Sep 17 00:00:00 2001 From: Josh Pigford Date: Tue, 29 Oct 2024 21:08:36 -0400 Subject: [PATCH] Another step towards functional combobox --- app/controllers/account/trades_controller.rb | 13 +++++++++++-- app/models/account/trade_builder.rb | 6 ++++-- app/models/security.rb | 3 ++- app/views/account/trades/_security.turbo_stream.erb | 11 +++++++++++ app/views/account/trades/_tickers.turbo_stream.erb | 11 ----------- .../account/trades/securities.turbo_stream.erb | 9 ++------- 6 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 app/views/account/trades/_security.turbo_stream.erb delete mode 100644 app/views/account/trades/_tickers.turbo_stream.erb diff --git a/app/controllers/account/trades_controller.rb b/app/controllers/account/trades_controller.rb index 8d31d3aa..04f2048f 100644 --- a/app/controllers/account/trades_controller.rb +++ b/app/controllers/account/trades_controller.rb @@ -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 diff --git a/app/models/account/trade_builder.rb b/app/models/account/trade_builder.rb index e174a5c2..d28c508e 100644 --- a/app/models/account/trade_builder.rb +++ b/app/models/account/trade_builder.rb @@ -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 diff --git a/app/models/security.rb b/app/models/security.rb index 71922392..732599ce 100644 --- a/app/models/security.rb +++ b/app/models/security.rb @@ -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 diff --git a/app/views/account/trades/_security.turbo_stream.erb b/app/views/account/trades/_security.turbo_stream.erb new file mode 100644 index 00000000..232e4261 --- /dev/null +++ b/app/views/account/trades/_security.turbo_stream.erb @@ -0,0 +1,11 @@ +
+ <%= image_tag(security.logo_url, class: "rounded-full h-8 w-8 inline-block mr-2" ) %> +
+ + <%= security.name.presence || security.ticker %> + + + <%= "#{security.ticker} (#{security.exchange_acronym})" %> + +
+
\ No newline at end of file diff --git a/app/views/account/trades/_tickers.turbo_stream.erb b/app/views/account/trades/_tickers.turbo_stream.erb deleted file mode 100644 index 20bf8f9c..00000000 --- a/app/views/account/trades/_tickers.turbo_stream.erb +++ /dev/null @@ -1,11 +0,0 @@ -
- <%= image_tag(tickers[:logo_url], class: "rounded-full h-8 w-8 inline-block mr-2" ) %> -
- - <%= tickers[:name].presence || tickers[:symbol] %> - - - <%= "#{tickers[:symbol]} (#{tickers[:exchange_acronym]})" %> - -
-
\ No newline at end of file diff --git a/app/views/account/trades/securities.turbo_stream.erb b/app/views/account/trades/securities.turbo_stream.erb index f7fcaf2c..a3128c77 100644 --- a/app/views/account/trades/securities.turbo_stream.erb +++ b/app/views/account/trades/securities.turbo_stream.erb @@ -1,7 +1,2 @@ -<%= async_combobox_options @securities.map { |security| - { - display: security, - value: security[:symbol] - } - }, - render_in: { partial: "account/trades/tickers" } %> \ No newline at end of file +<%= async_combobox_options @securities, + render_in: { partial: "account/trades/security" } %> \ No newline at end of file