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

Add option filter on in_order_of #51761

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

igordepolli
Copy link

@igordepolli igordepolli commented May 7, 2024

Motivation / Background

This Pull Request has been created because currently, in_order_of method always use where clause to filter the results only with the values specified in values. Sometimes, we only want to put some values as priority in the sorting but we want the entire search scope without caring about the rest of the sorting. The propose here is add an option to specify to filter scope by values or not.

Detail

This Pull Request changes:

order = [3, 4, 1]

# Without specify option
Post.in_order_of(:id, order).to_sql
# SELECT 
  # "posts".* FROM "posts" 
# WHERE 
  # "posts"."id" IN (3, 4, 1) 
# ORDER BY 
  # CASE WHEN "posts"."id" = 3 THEN 1 WHEN "posts"."id" = 4 THEN 2 WHEN "posts"."id" = 1 THEN 3 END ASC

# With option seted to false
Post.in_order_of(:id, order, filter: false)
# SELECT 
  # "posts".* FROM "posts" 
# ORDER BY 
  # CASE WHEN "posts"."id" = 3 THEN 1 WHEN "posts"."id" = 4 THEN 2 WHEN "posts"."id" = 1 THEN 3 ELSE 4 END ASC

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@admsev

This comment was marked as spam.

@gooroodev

This comment was marked as spam.

@timoschilling
Copy link
Contributor

I really like this idea, I had already the same, but you are faster ;)

Maybe only_values isn't the best name for the option. Some more ideas are:

  • find which will be true by default and can be overwritten
  • filter which will be true by default and can be overwritten

@igordepolli
Copy link
Author

@timoschilling, ty very much for your review! I liked your idea, filter really is a better name and have taken your suggestion!

@admsev , thanks for the bot suggestions as well! I made the grammar correction and the other consistency correction it suggested!

@igordepolli igordepolli changed the title Add option only_values on in_order_of Add option filter on in_order_of May 28, 2024
Co-authored-by: Timo Schilling <timo@schilling.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants