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

Use method overloading to improve WhereChain definition #2070

Merged
merged 1 commit into from
Nov 18, 2024

Conversation

paracycle
Copy link
Member

Motivation

ActiveRecord::QueryMethods#where returns a ActiveRecord::QueryMethods::WhereChain instance only when it is called with no arguments. When called with arguments, it returns the same ActiveRecord::Relation instance.

Previously, we didn't have proper overloading support in RBI files, so the best thing we could do was act like ActiveRecord::QueryMethods::WhereChain was a subclass of the related relation class. This was not ideal because it would allow users to call methods on WhereChain that were not actually defined on it. For example, one could do User.all.where(name: "John").not which is not valid.

Implementation

Now that we have support for overloading, we can define where to return a WhereChain instance only when called with no arguments, and in all other cases, we can say that it returns the same relation instance.

Tests

Updated existing tests.

@paracycle paracycle requested a review from a team as a code owner November 8, 2024 16:37
@paracycle paracycle added the enhancement New feature or request label Nov 8, 2024
@paracycle
Copy link
Member Author

I tested this against our core monolith and it worked fine.

Copy link
Contributor

@egiurleo egiurleo left a comment

Choose a reason for hiding this comment

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

Nice! Thanks for fixing this. Not sure what's up with CI, but I can investigate if you need any support.

Copy link
Contributor

@amomchilov amomchilov left a comment

Choose a reason for hiding this comment

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

Nice!

`ActiveRecord::QueryMethods#where` returns a `WhereChain` instance only when it is called with no arguments. When called with arguments, it returns the same `ActiveRecord::Relation` instance.

Previously, we didn't have proper overloading support in RBI files, so the best thing we could do was act like `WhereChain` was a subclass of the related relation class. This was not ideal because it would allow users to call methods on `WhereChain` that were not actually defined on it. For example, one could do `User.all.where(name: "John").not` which is not valid.

Now that we have support for overloading, we can define `where` to return a `WhereChain` instance only when called with no arguments, and in all other cases, we can say that it returns the same relation instance.
@egiurleo egiurleo force-pushed the uk-improve-where-chain branch from 702133f to 519d65a Compare November 18, 2024 16:48
@egiurleo
Copy link
Contributor

Force pushed to get CI fixes :)

@egiurleo egiurleo enabled auto-merge November 18, 2024 17:02
@egiurleo egiurleo merged commit 063f134 into main Nov 18, 2024
27 checks passed
@egiurleo egiurleo deleted the uk-improve-where-chain branch November 18, 2024 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants