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.

refactor Products Searcher

Łukasz Adamczyk776b1f4b6ecf
diff --git a/core/lib/spree/core/search/base.rb b/core/lib/spree/core/search/base.rb
index 5a358a44ab..568f9a94e1 100644
--- a/core/lib/spree/core/search/base.rb
+++ b/core/lib/spree/core/search/base.rb
@@ -2,13 +2,14 @@ module Spree
module Core
module Search
class Base
- attr_accessor :properties
- attr_accessor :current_user
- attr_accessor :current_currency
+ attr_accessor :properties, :current_user, :current_currency, :current_store, :taxon
def initialize(params)
- self.current_currency = Spree::Config[:currency]
@properties = {}
+ @current_currency = Spree::Config[:currency]
+ @current_store = params[:current_store]
+ @taxon = params[:taxon]
+
prepare(params)
end
@@ -115,8 +116,6 @@ module Spree
end
def prepare(params)
- @properties[:current_store] = Spree::Store.find_by(id: params[:current_store_id])
- @properties[:taxon] = params[:taxon].blank? ? nil : Spree::Taxon.find(params[:taxon])
@properties[:keywords] = params[:keywords]
@properties[:option_value_ids] = build_option_value_ids(params)
@properties[:price] = get_price_range(params[:price])
diff --git a/core/spec/lib/search/base_spec.rb b/core/spec/lib/search/base_spec.rb
index 5bf1eb0968..fc5ad6165e 100644
--- a/core/spec/lib/search/base_spec.rb
+++ b/core/spec/lib/search/base_spec.rb
@@ -21,7 +21,7 @@ describe Spree::Core::Search::Base do
context 'when include_images is included in the initalization params' do
subject { described_class.new(params).retrieve_products }
- let(:params) { { include_images: true, keyword: product1.name, taxon: taxon.id } }
+ let(:params) { { include_images: true, keyword: product1.name, taxon: taxon } }
before do
product1.master.images << create(:image, position: 2)
@@ -117,8 +117,8 @@ describe Spree::Core::Search::Base do
let!(:product_for_spree_store) { create(:product, name: 'P-Spree-1', stores: [spree_store]) }
- context 'current_store_id is given in params' do
- let(:params) { { current_store_id: current_store.id } }
+ context 'current sture is given in params' do
+ let(:params) { { current_store: current_store } }
context 'when EU Store' do
let(:current_store) { eu_store }
@@ -157,8 +157,8 @@ describe Spree::Core::Search::Base do
end
end
- context 'store id is not given in params' do
- let(:params) { { current_store_id: nil } }
+ context 'current store is not given in params' do
+ let(:params) { { current_store: nil } }
it 'returns 8 products' do
expect(retrieved_products.count).to eq(8)
diff --git a/frontend/app/controllers/spree/products_controller.rb b/frontend/app/controllers/spree/products_controller.rb
index 7bdd8f60db..183762a2aa 100644
--- a/frontend/app/controllers/spree/products_controller.rb
+++ b/frontend/app/controllers/spree/products_controller.rb
@@ -12,7 +12,7 @@ module Spree
respond_to :html
def index
- @searcher = build_searcher(params.merge(include_images: true, current_store_id: current_store.id))
+ @searcher = build_searcher(params.merge(include_images: true, current_store: current_store))
@products = @searcher.retrieve_products
if http_cache_enabled?
diff --git a/frontend/app/controllers/spree/taxons_controller.rb b/frontend/app/controllers/spree/taxons_controller.rb
index b98c3b391b..37251482fc 100644
--- a/frontend/app/controllers/spree/taxons_controller.rb
+++ b/frontend/app/controllers/spree/taxons_controller.rb
@@ -37,8 +37,8 @@ module Spree
def load_products
search_params = params.merge(
- current_store_id: current_store.id,
- taxon: @taxon.id,
+ current_store: current_store,
+ taxon: @taxon,
include_images: true
)