-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
MONGOID-5654 [Monkey Patch Removal] Move Hash#__consolidate__ into Mongoid::Contextual #5689
MONGOID-5654 [Monkey Patch Removal] Move Hash#__consolidate__ into Mongoid::Contextual #5689
Conversation
…codebase, in a private capacity inside Contextual::Mongo class, so there is no reason to monkey-patch Hash for this. The method name has __ prefix so we can assume this is an internal only method that is safe to remove.
The failing spec for ruby-2.7 is not related to this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johnnyshields - I love the idea of pulling this out of Hash
. However, when it comes to testing a private method, it raises a bit of a red flag for me. I think that if the method needs to be tested in isolation, it should probably be pulled out into its own module, much like you did for Embedded.traverse
. What do you think?
Hmm... I interpret the guideline as "private methods are not required to be tested, but it is ok to test them if we judge it benefits stability." Private methods make future refactoring much easier. Making a method public solely because we'd like to test it would be "the tail wagging the dog." Since we already have the tests, let's keep them. Re: I am all for refactoring Mongoid::Contextual into smaller classes, but it's beyond the scope of this PR. |
I understand your point, but I still am strongly opposed to writing tests for methods with If you feel delegation is too heavy-handed for this feature, there are a couple of other options you might consider:
Also, just to clarify my original point: moving the method to a new module so it can be tested in isolation does not mean the new module and method must be (or even should be) part of Mongoid's public API. In many instances, I think it makes sense to document the new module and/or method with There are probably other ways to approach this, as well, but adding new tests (or refactoring old tests) that directly invoke private methods is a non-starter for me. |
@jamis would you accept tests for the public |
@johnnyshields -- yeah, that sounds reasonable. Thanks for meeting me halfway. :) |
OK cool, will do. By the way, most of the monkey patch PRs do not include private method tests. |
@johnnyshields -- I've opened a new PR that builds on your work here (#5740), and I'm going to close this PR in favor of that one. I've described the reasoning for it on that PR. Thanks for your work on this! |
Fixes MONGOID-5654
Hash#__consolidate__
monkey-patch method is used only once in the entire Mongoid codebase, in a private capacity insideContextual::Mongo
class. There is no reason to monkey-patch the kernel Hash class for this.This PR does the following:
Move the method intoContextual::Mongo
as a private method.Hash#mongoize_for
private method, which is only called once inside__consolidate__
The method name has
__
prefix so we can assume this is an internal only method that is safe to remove.