From 45ae318dd01c0a05b8e783798b25488ea6ba9c8b Mon Sep 17 00:00:00 2001 From: Johnny Shields Date: Wed, 27 Jul 2022 00:00:01 +0900 Subject: [PATCH] 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 Co-authored-by: Oleg Pudeyev <39304720+p-mongo@users.noreply.github.com> --- docs/release-notes/mongoid-8.1.txt | 25 ++++++++++++++++++ lib/mongoid.rb | 14 ++++++++-- lib/mongoid/config.rb | 7 +++++ spec/mongoid_spec.rb | 42 +++++++++++++++++++++++------- 4 files changed, 77 insertions(+), 11 deletions(-) diff --git a/docs/release-notes/mongoid-8.1.txt b/docs/release-notes/mongoid-8.1.txt index 211cb4de58..10c329af38 100644 --- a/docs/release-notes/mongoid-8.1.txt +++ b/docs/release-notes/mongoid-8.1.txt @@ -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 ------------------------------------------ diff --git a/lib/mongoid.rb b/lib/mongoid.rb index 61191b9a34..22816bd9e2 100644 --- a/lib/mongoid.rb +++ b/lib/mongoid.rb @@ -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. diff --git a/lib/mongoid/config.rb b/lib/mongoid/config.rb index 0c22c96f86..6b3ad65f06 100644 --- a/lib/mongoid/config.rb +++ b/lib/mongoid/config.rb @@ -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. # diff --git a/spec/mongoid_spec.rb b/spec/mongoid_spec.rb index 464f5f8b9f..5f25635594 100644 --- a/spec/mongoid_spec.rb +++ b/spec/mongoid_spec.rb @@ -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