Skip to content

Commit

Permalink
Merge pull request #4844 from nebulab/kennyadsl/fix-search-nomethoderror
Browse files Browse the repository at this point in the history
Raise a custom extension passing invalid search params
  • Loading branch information
kennyadsl authored Jan 18, 2023
2 parents 625aa06 + 9ce2bed commit 17540ad
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
25 changes: 17 additions & 8 deletions core/lib/spree/core/search/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ module Spree
module Core
module Search
class Base
class InvalidOptions < ArgumentError
def initialize(option)
super("Invalid option passed to the searcher: '#{option}'")
end
end

attr_accessor :properties
attr_accessor :current_user
attr_accessor :pricing_options
Expand Down Expand Up @@ -57,16 +63,19 @@ def add_eagerload_scopes(scope)
end

def add_search_scopes(base_scope)
if @properties[:search]
@properties[:search].each do |name, scope_attribute|
scope_name = name.to_sym
if base_scope.respond_to?(:search_scopes) && base_scope.search_scopes.include?(scope_name.to_sym)
base_scope = base_scope.send(scope_name, *scope_attribute)
else
base_scope = base_scope.merge(Spree::Product.ransack({ scope_name => scope_attribute }).result)
end
return base_scope unless @properties[:search].present?
raise InvalidOptions.new(:search) unless @properties[:search].respond_to?(:each_pair)

@properties[:search].each_pair do |name, scope_attribute|
scope_name = name.to_sym

if base_scope.respond_to?(:search_scopes) && base_scope.search_scopes.include?(scope_name.to_sym)
base_scope = base_scope.send(scope_name, *scope_attribute)
else
base_scope = base_scope.merge(Spree::Product.ransack({ scope_name => scope_attribute }).result)
end
end

base_scope
end

Expand Down
7 changes: 7 additions & 0 deletions core/spec/lib/search/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@
expect(searcher.retrieve_products.count).to eq(2)
end

it "raises a proper exception when search param is invalid" do
params = { per_page: "",
search: "blub" }
searcher = Spree::Core::Search::Base.new(params)
expect { searcher.retrieve_products }.to raise_error(described_class::InvalidOptions, "Invalid option passed to the searcher: 'search'")
end

it "accepts a current user" do
user = double
searcher = Spree::Core::Search::Base.new({})
Expand Down

0 comments on commit 17540ad

Please sign in to comment.