Skip to content

Commit

Permalink
MONGOID-5422 - Configuration DSL should not require an argument to it…
Browse files Browse the repository at this point in the history
…s block (Rails parity) (#5367)

* MONGOID-5422 - Configuration DSL no longer requires an argument to its block

* More terse syntax

* Use config_override

* Update docs/release-notes/mongoid-8.1.txt

Co-authored-by: shields <shields@tablecheck.com>
Co-authored-by: Oleg Pudeyev <39304720+p-mongo@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 26, 2022
1 parent 4d757e5 commit 45ae318
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 11 deletions.
25 changes: 25 additions & 0 deletions docs/release-notes/mongoid-8.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,31 @@ please consult GitHub releases for detailed release notes and JIRA for
the complete list of issues fixed in each release, including bug fixes.


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

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


Added ``Mongoid::Criteria`` finder methods
------------------------------------------

Expand Down
14 changes: 12 additions & 2 deletions lib/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,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 @@ -127,6 +127,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
42 changes: 33 additions & 9 deletions spec/mongoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,46 @@
end
end

context "when a block is supplied" do
context "when a block is given" do
config_override :preload_models, false

before do
Mongoid.configure do |config|
config.preload_models = true
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

after do
Mongoid.configure do |config|
config.preload_models = false
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

0 comments on commit 45ae318

Please sign in to comment.