Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve variant and product autocomplete functions flexibility with Ransack #4767

Conversation

RyanofWoods
Copy link
Contributor

Summary

From this change, when initializing a productAutocomplete, a callback can be given to the searchCallback option. This callback should return a hash object that will be used to merge onto the existing parameters in the select2 ajax function. The callback also allows for one argument, which is the string typed in the select2 (term). Custom Ransack can be achieved by defining this option with the Ransack query on the q key.

$('#product-dropdown').productAutocomplete({
  multiple: false,
  searchCallback: (searchString) => ({
    q: {
      name_cont: searchString,
      available_on_not_null: true
    })
  }
})

Similar behavior was already existing on the variantAutocomplete, however accepted a simple hash object instead of a function, and so one didn't have access to the select2 search term, so this was improved.

$('#variant-dropdown).variantAutocomplete({
  searchCallback: (searchString) => ({
    q: {
      variant_search_term: null,
      sku_cont: searchString
    })
  }
})

Checklist

Check out our PR guidelines for more details.

The following are mandatory for all PRs:

  • I have written a thorough PR description.
  • I have kept my commits small and atomic.
  • I have used clear, explanatory commit messages.

The following are not always needed (cross them out if they are not):

  • I have added automated tests to cover my changes.
    - [ ] I have attached screenshots to demo visual changes.
    - [ ] I have opened a PR to update the guides.
    - [ ] I have updated the README to account for my changes.

Autocompletes are still working:

Variant

Orders#index

Screenshot 2022-11-28 at 14 10 33

Orders#edit

Screenshot 2022-11-28 at 14 10 45

Product

Promotion Rules - Product

Screenshot 2022-11-28 at 14 12 10

Promotion Rules - Option Values

Screenshot 2022-11-28 at 14 12 47

@RyanofWoods RyanofWoods requested a review from a team as a code owner December 5, 2022 15:16
@github-actions github-actions bot added changelog:solidus_backend Changes to the solidus_backend gem changelog:solidus_api Changes to the solidus_api gem labels Dec 5, 2022
Copy link
Contributor

@waiting-for-dev waiting-for-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, @RyanofWoods!

@RyanofWoods
Copy link
Contributor Author

Note, the variantAutocomplete changes are breaking to any stores that have implemented it. They'll need to switch from searchOptions to searchCallback appropriately.

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work and thanks for also tackling the variantAutocomplete 👏🏻

I still think we can improve the name of the option, to make it more intuitive.

@waiting-for-dev waiting-for-dev added the release:major Breaking change on hold until next major release label Dec 7, 2022
From this change, when initializing a productAutocomplete, a function
can be given to the searchParameters option argument. This callback
should return an object (hash), and accepts a parameter which is the
currently typed string in the autocomplete select2. The "q" key on the
returned hash will be given to Ransack.
From this change, when initializing a variantAutocomplete, a function
can be given to the searchParameters option argument.

Before, variantAutocomplete accepted an object (hash) searchOptions
argument, now instead, searchParameters should be used and given a
function that returns an object (hash). It accepts a parameter which is
the currently typed string in the autocomplete select2. The "q" key on
the returned hash will be given to Ransack if the variant_search_term
key is overridden with a falsy value.

For compatibility, if searchOptions was being used, searchParameters can just return the same hash the same behavior.
For Api::VariantsController#index to use the q parameter with Ransack,
variant_search_term has to be a falsy value/not exist. By default,
variantAutocomplete has variant_search_term defined and needs to be
overriden with a falsy value if ransack wants to be used.
@RyanofWoods RyanofWoods force-pushed the ryanofwoods/make-product-autocomplete-more-reusable branch from b969610 to e2f7fca Compare December 13, 2022 05:16
@RyanofWoods
Copy link
Contributor Author

I pulled a spec change out into an new commit and changed other commits to rename searchCallback to searchParameters

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🙏🏻

Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @RyanofWoods! This is great but I have a request: what if we try to make this change backward compatible converting the previous input format for variantAutocomplete into the new format programmatically and print a deprecation warning so that people have the opportunity to change it?

@RyanofWoods
Copy link
Contributor Author

I can do that @kennyadsl, when you mean print, do you mean like console.warn? I believe we don't have a JS deprecation API in place or that we've done formal JS deprecations before 🤔

@kennyadsl
Copy link
Member

when you mean print, do you mean like console.warn?

Yes, I think that the console warning, combined with an entry in our CHANGELOG would be enough to let people know. We do not have a specific API for that, despite we did something similar in the past.

@github-actions github-actions bot added the changelog:repository Changes to the repository not within any gem label Dec 22, 2022
@RyanofWoods
Copy link
Contributor Author

RyanofWoods commented Dec 22, 2022

Looks like we're not adding manual changes to the changelog anymore @kennyadsl and @waiting-for-dev, so I removed them and left them below for reference for the guides 👍


  • productAutocomplete's options parameter now accepts the searchParameters key, which allows access to the select2 search term and modification of the parameters sent to the API endpoint.

variantAutocomplete Changes

DEPRECATION:

  • Passing an object of parameters is deprecated, instead, please declare the searchParameters key that's a function returning the parameters.

Deprecated usage:

$('#id').variantAutocomplete({
  suppliable_only: true
})

New usage:

$('#id').variantAutocomplete({
  searchParameters: function (_selectSearchTerm) { return { suppliable_only: true  } }
})

@RyanofWoods RyanofWoods force-pushed the ryanofwoods/make-product-autocomplete-more-reusable branch from 39fc037 to fd61689 Compare December 22, 2022 08:32
@github-actions github-actions bot removed the changelog:repository Changes to the repository not within any gem label Dec 22, 2022
@RyanofWoods
Copy link
Contributor Author

So in whole, I added one squash commit for ease of review based on @kennyadsl's comments

Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @RyanofWoods, left one more last suggestion.

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.
@RyanofWoods RyanofWoods force-pushed the ryanofwoods/make-product-autocomplete-more-reusable branch from fd61689 to 5cd3007 Compare December 22, 2022 09:10
@kennyadsl kennyadsl added type:enhancement Proposed or newly added feature and removed release:major Breaking change on hold until next major release labels Dec 22, 2022
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I removed the release:major label, we can merge this in a minor now. Thanks!

@waiting-for-dev waiting-for-dev merged commit 0990aa8 into solidusio:master Dec 22, 2022
@waiting-for-dev waiting-for-dev deleted the ryanofwoods/make-product-autocomplete-more-reusable branch December 22, 2022 11:26
@RyanofWoods
Copy link
Contributor Author

Didn't want the squash commit squashed? 🤔

@waiting-for-dev
Copy link
Contributor

Oh, sorry, I missed that 🤦

@RyanofWoods
Copy link
Contributor Author

No worries! I could have said something @waiting-for-dev

kennyadsl added a commit to nebulab/solidus that referenced this pull request Mar 27, 2023
Instead, pass only

    searchParameters: function (_selectSearchTerm) { return { suppliable_only: true  } }

Ref solidusio#4767
kennyadsl added a commit to nebulab/solidus that referenced this pull request Mar 28, 2023
Instead, pass only

    searchParameters: function (_selectSearchTerm) { return { suppliable_only: true  } }

Ref solidusio#4767
kennyadsl added a commit to nebulab/solidus that referenced this pull request Apr 12, 2023
Instead, pass only

    searchParameters: function (_selectSearchTerm) { return { suppliable_only: true  } }

Ref solidusio#4767
kennyadsl added a commit to nebulab/solidus that referenced this pull request Apr 14, 2023
Instead, pass only

    searchParameters: function (_selectSearchTerm) { return { suppliable_only: true  } }

Ref solidusio#4767
kennyadsl added a commit to nebulab/solidus that referenced this pull request Apr 18, 2023
Instead, pass only

    searchParameters: function (_selectSearchTerm) { return { suppliable_only: true  } }

Ref solidusio#4767
kennyadsl added a commit to nebulab/solidus that referenced this pull request Apr 18, 2023
Instead, pass only

    searchParameters: function (_selectSearchTerm) { return { suppliable_only: true  } }

Ref solidusio#4767
elia pushed a commit to nebulab/solidus that referenced this pull request Apr 24, 2023
Instead, pass only

    searchParameters: function (_selectSearchTerm) { return { suppliable_only: true  } }

Ref solidusio#4767
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_api Changes to the solidus_api gem changelog:solidus_backend Changes to the solidus_backend gem type:enhancement Proposed or newly added feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants