Skip to content

Commit 45ae318

Browse files
MONGOID-5422 - Configuration DSL should not require an argument to its 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 <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 4d757e5 commit 45ae318

File tree

4 files changed

+77
-11
lines changed

4 files changed

+77
-11
lines changed

docs/release-notes/mongoid-8.1.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,31 @@ please consult GitHub releases for detailed release notes and JIRA for
1818
the complete list of issues fixed in each release, including bug fixes.
1919

2020

21+
Configuration DSL No Longer Requires an Argument to its Block
22+
-------------------------------------------------------------
23+
24+
It is now possible to use ``Mongoid.configure`` without
25+
providing an argument to its block:
26+
27+
.. code-block:: ruby
28+
29+
Mongoid.configure do
30+
connect_to("mongoid_test")
31+
32+
# Use config method when assigning variables
33+
config.preload_models = true
34+
35+
Note that ``configure`` will continue to support a block argument.
36+
The following is equivalent to the above:
37+
38+
.. code-block:: ruby
39+
40+
Mongoid.configure do |config|
41+
config.connect_to("mongoid_test")
42+
43+
config.preload_models = true
44+
45+
2146
Added ``Mongoid::Criteria`` finder methods
2247
------------------------------------------
2348

lib/mongoid.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,19 @@ module Mongoid
5858
# }
5959
# end
6060
#
61+
# @example Using a block without an argument. Use `config` inside
62+
# the block to perform variable assignment.
63+
#
64+
# Mongoid.configure do
65+
# connect_to("mongoid_test")
66+
#
67+
# config.preload_models = true
68+
#
6169
# @return [ Config ] The configuration object.
62-
def configure
63-
block_given? ? yield(Config) : Config
70+
def configure(&block)
71+
return Config unless block_given?
72+
73+
block.arity == 0 ? Config.instance_exec(&block) : yield(Config)
6474
end
6575

6676
# Convenience method for getting the default client.

lib/mongoid/config.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ module Config
127127
# always return a Hash.
128128
option :legacy_attributes, default: false
129129

130+
# Returns the Config singleton, for use in the configure DSL.
131+
#
132+
# @return [ self ] The Config singleton.
133+
def config
134+
self
135+
end
136+
130137
# Has Mongoid been configured? This is checking that at least a valid
131138
# client config exists.
132139
#

spec/mongoid_spec.rb

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,46 @@
1313
end
1414
end
1515

16-
context "when a block is supplied" do
16+
context "when a block is given" do
17+
config_override :preload_models, false
1718

18-
before do
19-
Mongoid.configure do |config|
20-
config.preload_models = true
19+
context "with arity 0" do
20+
21+
before do
22+
Mongoid.configure do
23+
config.preload_models = true
24+
end
25+
end
26+
27+
it "sets the values on the config instance" do
28+
expect(Mongoid.preload_models).to be true
2129
end
2230
end
2331

24-
after do
25-
Mongoid.configure do |config|
26-
config.preload_models = false
32+
context "with arity 1" do
33+
34+
before do
35+
Mongoid.configure do |config|
36+
config.preload_models = true
37+
end
38+
end
39+
40+
it "sets the values on the config instance" do
41+
expect(Mongoid.preload_models).to be true
2742
end
2843
end
2944

30-
it "sets the values on the config instance" do
31-
expect(Mongoid.preload_models).to be true
45+
context "with arity 2" do
46+
47+
before do
48+
Mongoid.configure do |config, _other|
49+
config.preload_models = true
50+
end
51+
end
52+
53+
it "sets the values on the config instance" do
54+
expect(Mongoid.preload_models).to be true
55+
end
3256
end
3357
end
3458
end

0 commit comments

Comments
 (0)