Skip to content

Commit

Permalink
squash Improve variantAutocomplete to be more reusable
Browse files Browse the repository at this point in the history
Make variantAutocomplete backwards compatible. Users can still pass an
object of parameters, null or nothing, if the former is done, a
console.warn deprecation warning is shown.
  • Loading branch information
RyanofWoods committed Dec 22, 2022
1 parent e2f7fca commit 5cd3007
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,30 @@
/**
* Make the element a select2 dropdown used for finding Variants. By default, the search term will be
* passed to the defined Spree::Config.variant_search_class by the controller with its defined scope.
* @param {Object} options Options
* @param {Object|undefined|null} options Options
* @param {Function|undefined} options.searchParameters Returns a hash object for params to merge on the select2 ajax request
* Accepts an argument of the select2 search term. To use Ransack, define
* variant_search_term as a falsy value, and q as the Ransack query. Note,
* you need to ensure that the attributes are allowed to be Ransacked.
*/
$.fn.variantAutocomplete = function(options = {}) {
$.fn.variantAutocomplete = function(options) {
function extraParameters(term) {
if (typeof(options['searchParameters']) === 'function') {
return options['searchParameters'](term)
if (typeof(options) === 'object') {
if (typeof(options['searchParameters']) === 'function') {
return options['searchParameters'](term)
} else {
console.warn(
"Solidus deprecation: Passing an object of parameters to variantAutocomplete is deprecated. Instead, on the options object, please declare `searchParameters` as a function returning the parameters.\n\n",
"Deprecated usage:\n",
"$('#id').variantAutocomplete({\n",
" suppliable_only: true\n",
"})",
"\n\nNew usage:\n",
"$('#id').variantAutocomplete({\n",
" searchParameters: function (_selectSearchTerm) { return { suppliable_only: true } }\n",
"})"
)
}
}

return {}
Expand Down

0 comments on commit 5cd3007

Please sign in to comment.