Skip to content

Commit

Permalink
Allow backend productAutocomplete to support a custom ransack query
Browse files Browse the repository at this point in the history
From this change, when initializing a productAutocomplete, a callback
can be given to the ransack_q_callback option. This callback should
return a hash for the ransack search. The callback also allows for one
argument, which is the string typed in the select2 (term).
  • Loading branch information
RyanofWoods committed Nov 22, 2022
1 parent d582f54 commit 9a96c36
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions backend/app/assets/javascripts/spree/backend/product_picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ $.fn.productAutocomplete = function (options) {
// Default options
options = options || {}
var multiple = typeof(options['multiple']) !== 'undefined' ? options['multiple'] : true
function ransackQuery(term) {
if (typeof(options['ransack_q_callback']) === 'function') return options['ransack_q_callback'](term)

return {
name_cont: term,
variants_including_master_sku_start: term,
m: 'or'
}
}

function formatProduct(product) {
return Select2.util.escapeMarkup(product.name);
Expand All @@ -27,11 +36,7 @@ $.fn.productAutocomplete = function (options) {
params: { "headers": { 'Authorization': 'Bearer ' + Spree.api_key } },
data: function (term, page) {
return {
q: {
name_cont: term,
variants_including_master_sku_start: term,
m: 'or'
},
q: ransackQuery(term),
token: Spree.api_key,
page: page
};
Expand Down

0 comments on commit 9a96c36

Please sign in to comment.