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

MONGOID-5422 - Configuration DSL should not require an argument to its block (Rails parity) #5367

Merged
merged 6 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/release-notes/mongoid-8.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,31 @@ Mongoid 7 behavior:
end


Configuration DSL No Longer Requires an Argument to its Block
-------------------------------------------------------------

In Mongoid 8.0, it is now possible to use ``Mongoid.configure`` without
providing an argument to its block:

.. code-block:: ruby

Mongoid.configure do
connect_to("mongoid_test")

# Use config method when assigning variables
config.preload_models = true

Note that ``configure`` will continue to support a block argument.
The following is equivalent to the above:

.. code-block:: ruby

Mongoid.configure do |config|
config.connect_to("mongoid_test")

config.preload_models = true


``#pluck`` on Embedded Criteria Returns ``nil`` Values
------------------------------------------------------

Expand Down
14 changes: 12 additions & 2 deletions lib/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,19 @@ module Mongoid
# }
# end
#
# @example Using a block without an argument. Use `config` inside
# the block to perform variable assignment.
#
# Mongoid.configure do
# connect_to("mongoid_test")
#
# config.preload_models = true
#
# @return [ Config ] The configuration object.
def configure
block_given? ? yield(Config) : Config
def configure(&block)
return Config unless block_given?

block.arity == 0 ? Config.instance_exec(&block) : yield(Config)
end

# Convenience method for getting the default client.
Expand Down
7 changes: 7 additions & 0 deletions lib/mongoid/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ module Config
# always return a Hash.
option :legacy_attributes, default: false

# Returns the Config singleton, for use in the configure DSL.
#
# @return [ self ] The Config singleton.
def config
self
end

# Has Mongoid been configured? This is checking that at least a valid
# client config exists.
#
Expand Down
45 changes: 37 additions & 8 deletions spec/mongoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,51 @@
end
end

context "when a block is supplied" do
context "when a block is given" do

before do
after do
Mongoid.configure do |config|
config.preload_models = true
config.preload_models = false
p-mongo marked this conversation as resolved.
Show resolved Hide resolved
end
end

after do
Mongoid.configure do |config|
config.preload_models = false
context "with arity 0" do

before do
Mongoid.configure do
config.preload_models = true
end
end

it "sets the values on the config instance" do
expect(Mongoid.preload_models).to be true
end
end

context "with arity 1" do

before do
Mongoid.configure do |config|
config.preload_models = true
end
end

it "sets the values on the config instance" do
expect(Mongoid.preload_models).to be true
end
end

it "sets the values on the config instance" do
expect(Mongoid.preload_models).to be true
context "with arity 2" do

before do
Mongoid.configure do |config, _other|
config.preload_models = true
end
end

it "sets the values on the config instance" do
expect(Mongoid.preload_models).to be true
end
end
end
end
Expand Down