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.

Add products/variants multi search with and without pg_search

Łukasz Adamczykb15c28b7c511
diff --git a/admin/app/controllers/spree/admin/products_controller.rb b/admin/app/controllers/spree/admin/products_controller.rb
index 660ddc7f76..8e85602c9c 100644
--- a/admin/app/controllers/spree/admin/products_controller.rb
+++ b/admin/app/controllers/spree/admin/products_controller.rb
@@ -27,6 +27,29 @@ module Spree
update.before :remove_empty_params
helper_method :clone_object_url
+ # https://blog.corsego.com/hotwire-turbo-streams-autocomplete-search
+ def search
+ query = params[:q]&.strip
+
+ head :ok and return if query.blank? || query.length < 3
+
+ scope = current_store.products.not_archived.accessible_by(current_ability, :index)
+ scope = scope.where.not(id: params[:omit_ids].split(',')) if params[:omit_ids].present?
+ @products = scope.includes(master: :images, variants: :images).multi_search(query).limit(params[:limit] || 10)
+
+ respond_to do |format|
+ format.turbo_stream do
+ render turbo_stream: [
+ turbo_stream.replace(
+ 'products_search_results',
+ partial: 'spree/admin/products/search_results',
+ locals: { products: @products }
+ )
+ ]
+ end
+ end
+ end
+
def show
redirect_to action: :edit
end
diff --git a/admin/spec/controllers/spree/admin/products_controller_spec.rb b/admin/spec/controllers/spree/admin/products_controller_spec.rb
index c7e27aeba9..1836a96c7b 100644
--- a/admin/spec/controllers/spree/admin/products_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/products_controller_spec.rb
@@ -77,6 +77,67 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
end
end
+ describe 'POST #search' do
+ let!(:product1) { create(:product, name: 'Product 1') }
+ let!(:product2) { create(:product, name: 'Product 2') }
+ let!(:product3) { create(:product, name: 'Product 3') }
+
+ context 'when query is blank' do
+ it 'returns a 200 status code' do
+ get :search, params: { q: '' }, format: :turbo_stream
+ expect(response).to have_http_status(:ok)
+ end
+
+ it 'returns an empty response body' do
+ get :search, params: { q: '' }, format: :turbo_stream
+ expect(response.body).to be_blank
+ end
+ end
+
+ context 'when query is less than 3 characters' do
+ it 'returns a 200 status code' do
+ get :search, params: { q: 'ab' }, format: :turbo_stream
+ expect(response).to have_http_status(:ok)
+ end
+
+ it 'returns an empty response body' do
+ get :search, params: { q: 'ab' }, format: :turbo_stream
+ expect(response.body).to be_blank
+ end
+ end
+
+ context 'when query is valid' do
+ it 'returns a 200 status code' do
+ get :search, params: { q: 'Product' }, format: :turbo_stream
+ expect(response).to have_http_status(:ok)
+ end
+
+ it 'renders the search_results partial' do
+ get :search, params: { q: 'Product' }, format: :turbo_stream
+ expect(response).to render_template(partial: 'spree/admin/products/_search_results')
+ end
+
+ it 'assigns the products matching the query to @products' do
+ get :search, params: { q: 'Product' }, format: :turbo_stream
+ expect(assigns(:products)).to match_array([product1, product2, product3])
+ end
+
+ context 'when omit_ids parameter is present' do
+ it 'excludes the products with the given ids' do
+ get :search, params: { q: 'Product', omit_ids: "#{product1.id},#{product2.id}" }, format: :turbo_stream
+ expect(assigns(:products)).to match_array([product3])
+ end
+ end
+
+ context 'when limit parameter is present' do
+ it 'limits the number of products returned' do
+ get :search, params: { q: 'Product', limit: 2 }, format: :turbo_stream
+ expect(assigns(:products).size).to eq(2)
+ end
+ end
+ end
+ end
+
describe 'POST #create' do
subject { post :create, params: { product: product_params } }
let(:stock_location) { create(:stock_location) }
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rb
index 28a6596f90..ccfec73946 100644
--- a/core/app/models/concerns/spree/product_scopes.rb
+++ b/core/app/models/concerns/spree/product_scopes.rb
@@ -305,7 +305,7 @@ module Spree
# .search_by_name
if defined?(PgSearch)
include PgSearch::Model
- pg_search_scope :search_by_name, against: :name, using: { tsearch: { any_word: true, prefix: true } }
+ pg_search_scope :search_by_name, against: { name: 'A', meta_title: 'B' }, using: { tsearch: { any_word: true, prefix: true } }
else
def self.search_by_name(query)
i18n { name.lower.matches("%#{query.downcase}%") }
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index e82905f220..3f0857c630 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -46,6 +46,13 @@ module Spree
translates(*TRANSLATABLE_FIELDS, column_fallback: !Spree.always_use_translations?)
self::Translation.class_eval do
+ if defined?(PgSearch)
+ include PgSearch::Model
+
+ pg_search_scope :search_by_name, against: { name: 'A', meta_title: 'B' },
+ using: { tsearch: { prefix: true, any_word: true } }
+ end
+
before_save :set_slug
acts_as_paranoid
# deleted translation values also need to be accessible for index views listing deleted resources
@@ -177,15 +184,45 @@ module Spree
where("#{Spree::Price.table_name}.compare_at_amount > #{Spree::Price.table_name}.amount")
}
+ 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)
+ else
+ Spree::Product.search_by_name(query).ids
+ end
+
+ variant_product_ids = if include_options.present?
+ Spree::Variant.search_by_sku_or_options(query).pluck(:product_id)
+ else
+ Spree::Variant.search_by_sku(query).pluck(:product_id)
+ end
+
+ where(id: (product_ids + variant_product_ids).uniq.compact)
+ }
+ else
+ scope :multi_search, lambda { |query|
+ return none if query.blank?
+ return none if query.length < 3
+
+ Spree::Variant.search_by_product_name_or_sku(query)
+ }
+ end
+
attr_accessor :option_values_hash
accepts_nested_attributes_for :product_properties, allow_destroy: true, reject_if: ->(pp) { pp[:property_name].blank? }
alias options product_option_types
- self.whitelisted_ransackable_associations = %w[taxons stores variants_including_master master variants tags labels]
self.whitelisted_ransackable_attributes = %w[description name slug discontinue_on status]
- self.whitelisted_ransackable_scopes = %w[not_discontinued search_by_name in_taxon price_between]
+ self.whitelisted_ransackable_associations = %w[taxons stores variants_including_master master variants tags labels
+ shipping_category classifications option_types properties]
+ self.whitelisted_ransackable_scopes = %w[not_discontinued search_by_name in_taxon price_between
+ multi_search in_stock_items out_of_stock_items]
[
:sku, :barcode, :price, :currency, :weight, :height, :width, :depth, :is_master,
@@ -219,6 +256,23 @@ module Spree
after_transition to: :draft, do: [:after_draft, :send_product_drafted_webhook]
end
+ def self.bulk_auto_match_taxons(store, product_ids)
+ return if store.taxons.automatic.none?
+
+ products_to_auto_match_ids = store.products.not_deleted.not_archived.where(id: product_ids).ids
+
+ # for ActiveJob 7.1+
+ if ActiveJob.respond_to?(:perform_all_later)
+ auto_match_taxons_jobs = products_to_auto_match_ids.map do |product_id|
+ Spree::Products::AutoMatchTaxonsJob.new(product_id)
+ end
+
+ ActiveJob.perform_all_later(auto_match_taxons_jobs)
+ else
+ products_to_auto_match_ids.each { |product_id| Spree::Products::AutoMatchTaxonsJob.perform_later(product_id) }
+ end
+ end
+
# Can't use short form block syntax due to https://github.com/Netflix/fast_jsonapi/issues/259
def purchasable?
@purchasable ||= default_variant.purchasable? || variants.in_stock_or_backorderable.any?
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index df39230f38..8f316d7a9a 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -111,6 +111,34 @@ module Spree
joins(:option_values).where(Spree::OptionValue.table_name => { name: option_value, option_type_id: option_type_ids })
}
+ if defined?(PgSearch)
+ include PgSearch::Model
+
+ pg_search_scope :search_by_sku, against: :sku, using: { tsearch: { prefix: true } }
+
+ pg_search_scope :search_by_sku_or_options,
+ against: :sku,
+ using: { tsearch: { prefix: true } },
+ associated_against: { option_values: %i[presentation] }
+
+ pg_search_scope :search_by_name_sku_or_options, against: :sku, associated_against: {
+ product: %i[name],
+ option_values: %i[presentation]
+ }, using: { tsearch: { prefix: true } }
+
+ scope :multi_search, lambda { |query|
+ return none if query.blank? || query.length < 3
+
+ search_by_name_sku_or_options(query)
+ }
+ else
+ scope :multi_search, lambda { |query|
+ return none if query.blank? || query.length < 3
+
+ product_name_or_sku_cont(query)
+ }
+ end
+
# FIXME: cost price should be represented with DisplayMoney class
LOCALIZED_NUMBERS = %w(cost_price weight depth width height)