Skip to content

Commit a6c2cee

Browse files
authored
Merge pull request #4197 from SuperGoodSoft/adammathys/variant-searcher-autocomplete
Use Variant Searcher for Autocomplete
2 parents 669225c + e9d5db1 commit a6c2cee

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

api/app/controllers/spree/api/variants_controller.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ def destroy
2525
# we render on the view so we better update it any time a node is included
2626
# or removed from the views.
2727
def index
28-
@variants = scope.includes(include_list)
29-
.ransack(params[:q]).result
28+
@variants =
29+
if params[:variant_search_term]
30+
Spree::Config.variant_search_class.new(
31+
params[:variant_search_term], scope: scope
32+
).results.includes(include_list)
33+
else
34+
scope.includes(include_list).ransack(params[:q]).result
35+
end
3036

3137
@variants = paginate(@variants)
3238
respond_with(@variants)

api/spec/requests/spree/api/variants_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@ module Spree::Api
3838
expect(json_response['pages']).to eq(3)
3939
end
4040

41-
it 'can query the results through a paramter' do
41+
it 'can query the results through the search class' do
42+
expected_result = create(:variant, sku: 'FOOBAR')
43+
get spree.api_variants_path, params: { variant_search_term: 'FOO' }
44+
expect(json_response['count']).to eq(1)
45+
expect(json_response['variants'].first['sku']).to eq expected_result.sku
46+
end
47+
48+
it 'can query the results through ransack' do
4249
expected_result = create(:variant, sku: 'FOOBAR')
4350
get spree.api_variants_path, params: { q: { sku_cont: 'FOO' } }
4451
expect(json_response['count']).to eq(1)

backend/app/assets/javascripts/spree/backend/variant_autocomplete.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
},
3232
data: function(term, page) {
3333
var searchData = {
34-
q: {
35-
product_name_or_sku_cont: term
36-
},
34+
variant_search_term: term,
3735
token: Spree.api_key
3836
};
3937
return _.extend(searchData, searchOptions);

0 commit comments

Comments
 (0)