Skip to content

Commit

Permalink
Merge pull request #4197 from SuperGoodSoft/adammathys/variant-search…
Browse files Browse the repository at this point in the history
…er-autocomplete

Use Variant Searcher for Autocomplete
  • Loading branch information
jarednorman authored Feb 15, 2022
2 parents 669225c + e9d5db1 commit a6c2cee
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
10 changes: 8 additions & 2 deletions api/app/controllers/spree/api/variants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ def destroy
# we render on the view so we better update it any time a node is included
# or removed from the views.
def index
@variants = scope.includes(include_list)
.ransack(params[:q]).result
@variants =
if params[:variant_search_term]
Spree::Config.variant_search_class.new(
params[:variant_search_term], scope: scope
).results.includes(include_list)
else
scope.includes(include_list).ransack(params[:q]).result
end

@variants = paginate(@variants)
respond_with(@variants)
Expand Down
9 changes: 8 additions & 1 deletion api/spec/requests/spree/api/variants_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ module Spree::Api
expect(json_response['pages']).to eq(3)
end

it 'can query the results through a paramter' do
it 'can query the results through the search class' do
expected_result = create(:variant, sku: 'FOOBAR')
get spree.api_variants_path, params: { variant_search_term: 'FOO' }
expect(json_response['count']).to eq(1)
expect(json_response['variants'].first['sku']).to eq expected_result.sku
end

it 'can query the results through ransack' do
expected_result = create(:variant, sku: 'FOOBAR')
get spree.api_variants_path, params: { q: { sku_cont: 'FOO' } }
expect(json_response['count']).to eq(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
},
data: function(term, page) {
var searchData = {
q: {
product_name_or_sku_cont: term
},
variant_search_term: term,
token: Spree.api_key
};
return _.extend(searchData, searchOptions);
Expand Down

0 comments on commit a6c2cee

Please sign in to comment.