-
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-5422 - Configuration DSL should not require an argument to its block (Rails parity) #5367
MONGOID-5422 - Configuration DSL should not require an argument to its block (Rails parity) #5367
Conversation
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.
If you can adjust the tests to correctly restore config options everything else looks good.
Done > using config_override now |
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.
The release notes addition will conflict with #5396.
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.
This is awesome! My one question is that it looks like config is now delegating to self. That means you could probably do something like:
Mongoid.configure do
self.preload_models = true
end
Or even:
Mongoid.configure do
preload_models = true
end
Two questions:
- Is that true?
- Does rails behave that way as well?
@Neilshweky the behavior here is the same as Rails: Mongoid.configure do
self.preload_models = true # can do this
config.preload_models = true # can also do this, because there is now a magic `config` method which returns self
preload_models = true # cannot do this, because its a local variable assignment
end |
@p-mongo release note conflict resolved; ready to merge. |
docs/release-notes/mongoid-8.1.txt
Outdated
Configuration DSL No Longer Requires an Argument to its Block | ||
------------------------------------------------------------- | ||
|
||
In Mongoid 8.1, it is now possible to use ``Mongoid.configure`` without |
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.
Please do not add version numbers to release notes like this, it makes backporting changes more difficult and the release notes already specify the version they are for.
Thanks @p-mongo for getting this one to the finish line! |
This PR removes the need for
|config|
arg inMongoid.configure
. It makes it more terse and consistent withRails.application.configure
. The change is fully backwards compatible.This syntax is now possible:
Previous syntax (still supported):
To do this magic, like Rails, it uses
instance_exec
. For variable assignment, in the block, you need to useself.variable = "foo"
(otherwise its a local variable.) To get around this, we introduce a methodconfig
that returns self, which is also what Rails does:Note this only affects assigning with
=
, so other methods work fine (such asconnect_to
in example above. (I now fully grok why Puma's config DSL uses only methods and not variable assignment.) Most IDEs will warn about local variable assignment in the block.