Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Storefront: added search suggestions

Damian Legawiecb0cde325cc92
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 37e0df7d21..2759b81641 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -190,7 +190,6 @@ module Spree
if defined?(PgSearch)
scope :multi_search, lambda { |query, include_options = false|
return none if query.blank?
- return none if query.length < 3
product_ids = if Spree.use_translations?
Spree::Product::Translation.search_by_name(query).pluck(:spree_product_id)
@@ -209,7 +208,6 @@ module Spree
else
scope :multi_search, lambda { |query|
return none if query.blank?
- return none if query.length < 3
product_ids = Spree::Variant.search_by_product_name_or_sku(query).pluck(:product_id)
where(id: product_ids.uniq.compact)
@@ -229,7 +227,9 @@ module Spree
group(:id).
having("SUM(#{Spree::StockItem.table_name}.count_on_hand) <= 0")
}
- scope :out_of_stock, -> { joins(:stock_items).where("#{Spree::StockItem.table_name}.count_on_hand <= ? OR #{Spree::Variant.table_name}.track_inventory = ?", 0, false) }
+ scope :out_of_stock, lambda {
+ joins(:stock_items).where("#{Spree::StockItem.table_name}.count_on_hand <= ? OR #{Spree::Variant.table_name}.track_inventory = ?", 0, false)
+ }
scope :by_best_selling, lambda { |order_direction = :desc|
left_joins(:orders).
@@ -242,7 +242,9 @@ module Spree
attr_accessor :option_values_hash
- accepts_nested_attributes_for :product_properties, allow_destroy: true, reject_if: ->(pp) { pp[:property_id].blank? || (pp[:id].blank? && pp[:value].blank?) }
+ accepts_nested_attributes_for :product_properties, allow_destroy: true, reject_if: lambda { |pp|
+ pp[:property_id].blank? || (pp[:id].blank? && pp[:value].blank?)
+ }
accepts_nested_attributes_for(
:variants,
allow_destroy: true,
diff --git a/storefront/app/controllers/spree/search_controller.rb b/storefront/app/controllers/spree/search_controller.rb
new file mode 100644
index 0000000000..1c5b7c29c7
--- /dev/null
+++ b/storefront/app/controllers/spree/search_controller.rb
@@ -0,0 +1,39 @@
+module Spree
+ class SearchController < StoreController
+ after_action :track, only: :show
+
+ helper_method :query
+
+ def show
+ @current_page = current_theme.pages.find_by(type: 'Spree::Pages::SearchResults')
+ end
+
+ def suggestions
+ @products = []
+ @taxons = []
+
+ if query.present? && query.length >= Spree::Storefront::Config.search_min_query_length
+ products_scope = current_store.products.active(current_currency).multi_search(query)
+ @products = products_scope.includes(storefront_products_includes)
+ @taxons = current_store.taxons.search_by_name(query)
+ end
+ end
+
+ private
+
+ def query
+ @query ||= params[:q].presence&.strip_html_tags&.strip
+ end
+
+ def track
+ return if turbo_frame_request? || turbo_stream_request?
+ return if query.blank?
+
+ track_event('product_searched', { query: query })
+ end
+
+ def default_products_sort
+ 'manual'
+ end
+ end
+end
diff --git a/storefront/app/views/spree/search/suggestions.turbo_stream.erb b/storefront/app/views/spree/search/suggestions.turbo_stream.erb
new file mode 100644
index 0000000000..be60708cba
--- /dev/null
+++ b/storefront/app/views/spree/search/suggestions.turbo_stream.erb
@@ -0,0 +1,8 @@
+<%= turbo_stream.update 'search-suggestions-content' do %>
+ <%= render 'spree/search/suggestions',
+ loaded: true,
+ products: @products,
+ taxons: @taxons,
+ query: query
+ %>
+<% end %>
diff --git a/storefront/lib/spree/storefront/configuration.rb b/storefront/lib/spree/storefront/configuration.rb
index 07d46870dd..f539c439a0 100644
--- a/storefront/lib/spree/storefront/configuration.rb
+++ b/storefront/lib/spree/storefront/configuration.rb
@@ -5,6 +5,7 @@ module Spree
class Configuration < ::Spree::Preferences::RuntimeConfiguration
preference :products_per_page, :integer, default: 20
+ preference :search_min_query_length, :integer, default: 2
end
end
end
diff --git a/storefront/spec/controllers/spree/search_controller_spec.rb b/storefront/spec/controllers/spree/search_controller_spec.rb
new file mode 100644
index 0000000000..6ac1bbb28a
--- /dev/null
+++ b/storefront/spec/controllers/spree/search_controller_spec.rb
@@ -0,0 +1,118 @@
+require 'spec_helper'
+
+RSpec.describe Spree::SearchController, type: :controller do
+ let(:store) { Spree::Store.default }
+ let(:theme) { create(:theme, store: store) }
+ let!(:search_page) { create(:page, type: 'Spree::Pages::SearchResults') }
+ let(:query) { 'test query' }
+
+ before do
+ allow(controller).to receive(:current_store).and_return(store)
+ allow(controller).to receive(:current_theme).and_return(theme)
+ allow(theme.pages).to receive(:find_by).with(type: 'Spree::Pages::SearchResults').and_return(search_page)
+ end
+
+ describe 'GET #show' do
+ before { get :show, params: { q: query } }
+
+ it 'assigns @current_page' do
+ expect(assigns(:current_page)).to eq(search_page)
+ end
+
+ it 'renders the show template' do
+ expect(response).to render_template(:show)
+ end
+
+ context 'when tracking search' do
+ context 'with turbo frame request' do
+ before do
+ allow(controller).to receive(:turbo_frame_request?).and_return(true)
+ get :show, params: { q: query }
+ end
+
+ it 'does not track the event' do
+ expect(controller).not_to receive(:track_event)
+ end
+ end
+
+ context 'with blank query' do
+ before { get :show, params: { q: '' } }
+
+ it 'does not track the event' do
+ expect(controller).not_to receive(:track_event)
+ end
+ end
+ end
+ end
+
+ describe 'GET #suggestions' do
+ context 'with valid query length' do
+ let!(:product) { create(:product_in_stock, stores: [store], name: 'Shirt') }
+ let!(:product_2) { create(:product_in_stock, stores: [store], name: 'Shoes') }
+ let!(:product_3) { create(:product_in_stock, stores: [store], name: 'Hat') }
+ let(:taxonomy) { create(:taxonomy, store: store, name: 'Clothing') }
+ let!(:taxon) { create(:taxon, name: 'Shirt', taxonomy: taxonomy) }
+ let!(:taxon_2) { create(:taxon, name: 'Shoes', taxonomy: taxonomy) }
+ let!(:taxon_3) { create(:taxon, name: 'Hat', taxonomy: taxonomy) }
+
+ let(:query) { 'sh' }
+
+ before do
+ get :suggestions, params: { q: query }, format: :turbo_stream
+ end
+
+ it 'assigns products array' do
+ expect(assigns(:products)).to match_array([product, product_2])
+ end
+
+ it 'assigns taxons array' do
+ expect(assigns(:taxons)).to match_array([taxon, taxon_2])
+ end
+ end
+
+ context 'with invalid query length' do
+ before do
+ allow(Spree::Storefront::Config).to receive(:search_min_query_length).and_return(5)
+ get :suggestions, params: { q: 'ab' }, format: :turbo_stream
+ end
+
+ it 'assigns empty products array' do
+ expect(assigns(:products)).to be_empty
+ end
+
+ it 'assigns empty taxons array' do
+ expect(assigns(:taxons)).to be_empty
+ end
+ end
+
+ context 'with blank query' do
+ before { get :suggestions, params: { q: ' ' }, format: :turbo_stream }
+
+ it 'assigns empty products array' do
+ expect(assigns(:products)).to be_empty
+ end
+
+ it 'assigns empty taxons array' do
+ expect(assigns(:taxons)).to be_empty
+ end
+ end
+ end
+
+ describe '#query' do
+ it 'strips HTML tags and whitespace' do
+ controller.params[:q] = " <script>alert('test')</script> query "
+ expect(controller.send(:query)).to eq("alert('test') query")
+ end
+
+ it 'returns nil for blank query' do
+ controller.params[:q] = ''
+ expect(controller.send(:query)).to be_nil
+ end
+ end
+
+ describe '#default_products_sort' do
+ it 'returns manual as default sort' do
+ expect(controller.send(:default_products_sort)).to eq('manual')
+ end
+ end
+end