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

Cop idea: Detection of date comparison in where scopes #921

Closed
anthony-robin opened this issue Jan 22, 2023 · 2 comments
Closed

Cop idea: Detection of date comparison in where scopes #921

anthony-robin opened this issue Jan 22, 2023 · 2 comments

Comments

@anthony-robin
Copy link
Contributor

anthony-robin commented Jan 22, 2023

I discovered recently I could rewrite a manual scope comparing dates using Rails power directly.

For example:

Before

Event.where('at >= ?', Time.current) 
# => "SELECT \"events\".* FROM \"events\" WHERE (at >= '2023-01-22 20:14:34.878368')"

Event.where('at < ?', Time.current) 
# => "SELECT \"events\".* FROM \"events\" WHERE (at < '2023-01-22 20:14:34.878368')"

After

Event.where(at: Time.current..)
# =>  "SELECT \"events\".* FROM \"events\" WHERE \"events\".\"at\" >= '2023-01-22 20:14:34.878368'"

Event.where(at: ...Time.current)
# => "SELECT \"events\".* FROM \"events\" WHERE \"events\".\"at\" < '2023-01-22 20:14:34.878368'"

In the same way, the cop should handle:

- `<` : `…Time.current`
- `<=` : `..Time.current`
- `>=` : `Time.current..`
- `>` : `Time.current…` # seems to behave as `>=`

I haven’t seen any cop that could detect this, what do you think about adding a new rule to the gem ?

Thank you :)

Some related context from Rails repo: rails/rails#47054 (comment)

@fatkodima
Copy link
Contributor

Duplicate - #689 ?

@anthony-robin
Copy link
Contributor Author

Indeed, it seems a duplicate of #689

Thanks for the catch, will close this one 👍

@anthony-robin anthony-robin closed this as not planned Won't fix, can't repro, duplicate, stale Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants