From 4b68ed881bc707fc32912fd953b7adfdd774596d Mon Sep 17 00:00:00 2001 From: shields Date: Sun, 10 Jul 2022 20:40:37 +0900 Subject: [PATCH 1/4] MONGOID-5422 - Configuration DSL no longer requires an argument to its block --- docs/release-notes/mongoid-8.0.txt | 25 +++++++++++++++++ lib/mongoid.rb | 20 +++++++++++-- lib/mongoid/config.rb | 7 +++++ spec/mongoid_spec.rb | 45 ++++++++++++++++++++++++------ 4 files changed, 87 insertions(+), 10 deletions(-) diff --git a/docs/release-notes/mongoid-8.0.txt b/docs/release-notes/mongoid-8.0.txt index ac06b39765..8e1d89362b 100644 --- a/docs/release-notes/mongoid-8.0.txt +++ b/docs/release-notes/mongoid-8.0.txt @@ -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 ------------------------------------------------------ diff --git a/lib/mongoid.rb b/lib/mongoid.rb index 358ef1aad2..d4d7e62d65 100644 --- a/lib/mongoid.rb +++ b/lib/mongoid.rb @@ -57,9 +57,25 @@ 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) + if block_given? + if block.arity == 0 + Config.instance_exec(&block) + else + yield(Config) + end + else + Config + end end # Convenience method for getting the default client. diff --git a/lib/mongoid/config.rb b/lib/mongoid/config.rb index abd7065f13..9c6e7c51ee 100644 --- a/lib/mongoid/config.rb +++ b/lib/mongoid/config.rb @@ -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. # diff --git a/spec/mongoid_spec.rb b/spec/mongoid_spec.rb index 464f5f8b9f..8d1312bae4 100644 --- a/spec/mongoid_spec.rb +++ b/spec/mongoid_spec.rb @@ -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 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 From ca0562bdab78d2f682237ee919195756234512da Mon Sep 17 00:00:00 2001 From: Johnny Shields Date: Sun, 10 Jul 2022 20:51:53 +0900 Subject: [PATCH 2/4] More terse syntax --- lib/mongoid.rb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/mongoid.rb b/lib/mongoid.rb index d4d7e62d65..d63a3dfc71 100644 --- a/lib/mongoid.rb +++ b/lib/mongoid.rb @@ -67,15 +67,9 @@ module Mongoid # # @return [ Config ] The configuration object. def configure(&block) - if block_given? - if block.arity == 0 - Config.instance_exec(&block) - else - yield(Config) - end - else - Config - end + return Config unless block_given? + + block.arity == 0 ? Config.instance_exec(&block) : yield(Config) end # Convenience method for getting the default client. From ed8462e29b0480af5b3ce42887084ce2060b3846 Mon Sep 17 00:00:00 2001 From: shields Date: Wed, 20 Jul 2022 05:23:01 +0900 Subject: [PATCH 3/4] Use config_override --- spec/mongoid_spec.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/spec/mongoid_spec.rb b/spec/mongoid_spec.rb index 8d1312bae4..5f25635594 100644 --- a/spec/mongoid_spec.rb +++ b/spec/mongoid_spec.rb @@ -14,12 +14,7 @@ end context "when a block is given" do - - after do - Mongoid.configure do |config| - config.preload_models = false - end - end + config_override :preload_models, false context "with arity 0" do From f3442d61dae7d55ffae8438ef38e6117483fce0d Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev <39304720+p-mongo@users.noreply.github.com> Date: Mon, 25 Jul 2022 18:24:26 -0400 Subject: [PATCH 4/4] Update docs/release-notes/mongoid-8.1.txt --- docs/release-notes/mongoid-8.1.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/release-notes/mongoid-8.1.txt b/docs/release-notes/mongoid-8.1.txt index 8bdcdbd372..ff5bf71d6b 100644 --- a/docs/release-notes/mongoid-8.1.txt +++ b/docs/release-notes/mongoid-8.1.txt @@ -21,7 +21,7 @@ the complete list of issues fixed in each release, including bug fixes. Configuration DSL No Longer Requires an Argument to its Block ------------------------------------------------------------- -In Mongoid 8.1, it is now possible to use ``Mongoid.configure`` without +It is now possible to use ``Mongoid.configure`` without providing an argument to its block: .. code-block:: ruby