From cb7e0bdcc39eb94547fc235fe9734c12f7df0079 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Mon, 6 Feb 2023 11:13:30 -0700 Subject: [PATCH] MONGOID-5439 Generate `config/initializers/mongoid.rb` (#5529) * add mongoid.rb rails initializer * write all available config options to the mongoid.rb initializer * let's try loading mongoid.yml after all other config has loaded this is so that the YAML options can override any options set in the the config/initializers. * remove Mongoid options from the mongoid.yml template Preference is now for these to be specified in the config/initializers/mongoid.rb initializer, though if present in the mongoid.yml file, they'll still be processed. * update docs to reference the mongoid.rb initializers * Revert "update docs to reference the mongoid.rb initializers" This reverts commit 05e2be9be6bd66f020e56bf4feb29637e8d29c93. * prefer the YAML file for Mongoid configuration but let's still auto-generate the default config by parsing the Mongoid::Config file... * fix tests * update docs * revert changes to mongoid.yml (in favor of #5539) --- docs/reference/configuration.txt | 8 ++- docs/reference/rails-integration.txt | 11 ++- docs/tutorials/getting-started-rails6.txt | 16 +++-- docs/tutorials/getting-started-rails7.txt | 16 +++-- lib/mongoid/railtie.rb | 6 +- .../mongoid/config/config_generator.rb | 7 +- .../mongoid/config/templates/mongoid.rb | 24 +++++++ spec/integration/app_spec.rb | 71 ++++++++++--------- 8 files changed, 108 insertions(+), 51 deletions(-) create mode 100644 lib/rails/generators/mongoid/config/templates/mongoid.rb diff --git a/docs/reference/configuration.txt b/docs/reference/configuration.txt index f2ca9c3692..108caba0e8 100644 --- a/docs/reference/configuration.txt +++ b/docs/reference/configuration.txt @@ -43,7 +43,13 @@ configuration file for you by running the following command: rails g mongoid:config -The configuration file will be placed in ``config/mongoid.yml``. +The configuration file will be placed in ``config/mongoid.yml``. An +initializer will also be created and placed in +``config/initializers/mongoid.rb``. It is recommended that all configuration +be specified in ``config/mongoid.yml``, but if you prefer, the ``mongoid.rb`` +initializer may also be used to set configuration options. Note, though, that +settings in ``mongoid.yml`` always take precedence over settings in the +initializer. If you are not using Ruby on Rails, you can copy the minimal configuration given above and save it as ``config/mongoid.yml``. diff --git a/docs/reference/rails-integration.txt b/docs/reference/rails-integration.txt index cd01df1bb4..4c7181cff0 100644 --- a/docs/reference/rails-integration.txt +++ b/docs/reference/rails-integration.txt @@ -19,9 +19,14 @@ manually enabled. Configuration ============= -You can set Mongoid configuration options in your ``application.rb`` along with other Rails -environment specific options by accessing config.mongoid. Options set here will override -those set in your ``config/mongoid.yml``. +You can set Mongoid configuration options in your ``application.rb`` along with +other Rails environment specific options by accessing config.mongoid. The +``mongoid:config`` generator will create an initializer in +``config/initializers/mongoid.rb`` which can also be used for configuring +Mongoid. Note, though, that options set in your ``config/mongoid.yml`` will +take precendence over options set elsewhere; it is recommended that whenever +possible you use ``mongoid.yml`` as the default location for Mongoid +configuration. .. code-block:: ruby diff --git a/docs/tutorials/getting-started-rails6.txt b/docs/tutorials/getting-started-rails6.txt index 4bfc72879b..c071407baf 100644 --- a/docs/tutorials/getting-started-rails6.txt +++ b/docs/tutorials/getting-started-rails6.txt @@ -125,10 +125,11 @@ Add Mongoid bin/rails g mongoid:config -This generator will create the ``config/mongoid.yml`` configuration file, -which is used to configure the connection to the MongoDB deployment. -Note that as we are not using ActiveRecord we will not have a ``database.yml`` -file. +This generator will create the ``config/mongoid.yml`` configuration file +(used to configure the connection to the MongoDB deployment) and the +``config/initializers/mongoid.rb`` initializer file (which may be used for +other Mongoid-related configuration). Note that as we are not using +ActiveRecord we will not have a ``database.yml`` file. .. _run-locally: @@ -465,8 +466,11 @@ Generate the default Mongoid configuration: bin/rails g mongoid:config -This generator will create the ``config/mongoid.yml`` configuration file, -which is used to configure the connection to the MongoDB deployment. +This generator will create the ``config/mongoid.yml`` configuration file +(used to configure the connection to the MongoDB deployment) and the +``config/initializers/mongoid.rb`` initializer file (which may be used for +other Mongoid-related configuration). In general, it is recommended to use +``mongoid.yml`` for all Mongoid configuration. Review the sections :ref:`Run MongoDB Locally ` and :ref:`Use MongoDB Atlas ` to decide how you would like to deploy diff --git a/docs/tutorials/getting-started-rails7.txt b/docs/tutorials/getting-started-rails7.txt index c99b669833..8283860699 100644 --- a/docs/tutorials/getting-started-rails7.txt +++ b/docs/tutorials/getting-started-rails7.txt @@ -95,10 +95,11 @@ Setup Mongoid bin/rails g mongoid:config -This generator will create the ``config/mongoid.yml`` configuration file, -which is used to configure the connection to the MongoDB deployment. -Note that as we are not using ActiveRecord we will not have a ``database.yml`` -file. +This generator will create the ``config/mongoid.yml`` configuration file +(used to configure the connection to the MongoDB deployment) and the +``config/initializers/mongoid.rb`` initializer file (which may be used for +other Mongoid-related configuration). Note that as we are not using +ActiveRecord we will not have a ``database.yml`` file. .. _configure-self-managed: @@ -333,8 +334,11 @@ Generate the default Mongoid configuration: bin/rails g mongoid:config -This generator will create the ``config/mongoid.yml`` configuration file, -which is used to configure the connection to the MongoDB deployment. +This generator will create the ``config/mongoid.yml`` configuration file +(used to configure the connection to the MongoDB deployment) and the +``config/initializers/mongoid.rb`` initializer file (which may be used for +other Mongoid-related configuration). In general, it is recommended to use +``mongoid.yml`` for all Mongoid configuration. Review the sections :ref:`Configure for Self Managed MongoDB ` and :ref:`Configure for MongoDB Atlas ` to decide how you diff --git a/lib/mongoid/railtie.rb b/lib/mongoid/railtie.rb index 9c5c2dfac7..f4edf6d927 100644 --- a/lib/mongoid/railtie.rb +++ b/lib/mongoid/railtie.rb @@ -51,7 +51,11 @@ def self.rescue_responses # Initialize Mongoid. This will look for a mongoid.yml in the config # directory and configure mongoid appropriately. - initializer "mongoid.load-config" do + # + # It runs after all config/initializers have loaded, so that the YAML + # options can override options specified in + # (e.g.) config/initializers/mongoid.rb. + initializer "mongoid.load-config", after: :load_config_initializers do config_file = Rails.root.join("config", "mongoid.yml") if config_file.file? begin diff --git a/lib/rails/generators/mongoid/config/config_generator.rb b/lib/rails/generators/mongoid/config/config_generator.rb index 0147175d44..59dac2ac11 100644 --- a/lib/rails/generators/mongoid/config/config_generator.rb +++ b/lib/rails/generators/mongoid/config/config_generator.rb @@ -5,7 +5,7 @@ module Mongoid module Generators class ConfigGenerator < Rails::Generators::Base - desc "Creates a Mongoid configuration file at config/mongoid.yml" + desc "Creates Mongoid configuration files" argument :database_name, type: :string, optional: true @@ -25,9 +25,12 @@ def app_name end def create_config_file - template 'mongoid.yml', File.join('config', "mongoid.yml") + template 'mongoid.yml', File.join('config', 'mongoid.yml') end + def create_initializer_file + template 'mongoid.rb', File.join('config', 'initializers', 'mongoid.rb') + end end end end diff --git a/lib/rails/generators/mongoid/config/templates/mongoid.rb b/lib/rails/generators/mongoid/config/templates/mongoid.rb new file mode 100644 index 0000000000..9a42a0a533 --- /dev/null +++ b/lib/rails/generators/mongoid/config/templates/mongoid.rb @@ -0,0 +1,24 @@ +Mongoid.configure do + target_version = "<%= Mongoid::VERSION[/^\d+\.\d+/] %>" + + # Load Mongoid behavior defaults. This automatically sets + # features flags (refer to documentation) + config.load_defaults target_version + + # It is recommended to use config/mongoid.yml for most Mongoid-related + # configuration, whenever possible, but if you prefer, you can set + # configuration values here, instead: + # + # config.log_level = :debug + # + # Note that the settings in config/mongoid.yml always take precedence, + # whatever else is set here. +end + +# Enable Mongo driver query cache for Rack +# Rails.application.config.middleware.use(Mongo::QueryCache::Middleware) + +# Enable Mongo driver query cache for ActiveJob +# ActiveSupport.on_load(:active_job) do +# include Mongo::QueryCache::Middleware::ActiveJob +# end diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index 360895107b..d1a1a7dd00 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -86,50 +86,57 @@ def start_app(cmd, port, timeout) rv end - context 'new application - rails' do - it 'creates' do - install_rails + def prepare_new_rails_app(name) + install_rails - Dir.chdir(TMP_BASE) do - FileUtils.rm_rf('mongoid-test') - check_call(%w(rails new mongoid-test --skip-spring --skip-active-record), env: clean_env) + Dir.chdir(TMP_BASE) do + FileUtils.rm_rf(name) + check_call(%W(rails new #{name} --skip-spring --skip-active-record), env: clean_env) - Dir.chdir('mongoid-test') do - adjust_app_gemfile - check_call(%w(bundle install), env: clean_env) + Dir.chdir(name) do + adjust_rails_defaults + adjust_app_gemfile + check_call(%w(bundle install), env: clean_env) - check_call(%w(rails g model post), env: clean_env) - check_call(%w(rails g model comment post:belongs_to), env: clean_env) + yield + end + end + end - # https://jira.mongodb.org/browse/MONGOID-4885 - comment_text = File.read('app/models/comment.rb') - comment_text.should =~ /belongs_to :post/ - comment_text.should_not =~ /embedded_in :post/ - end + context 'new application - rails' do + it 'creates' do + prepare_new_rails_app 'mongoid-test' do + check_call(%w(rails g model post), env: clean_env) + check_call(%w(rails g model comment post:belongs_to), env: clean_env) + + # https://jira.mongodb.org/browse/MONGOID-4885 + comment_text = File.read('app/models/comment.rb') + comment_text.should =~ /belongs_to :post/ + comment_text.should_not =~ /embedded_in :post/ end end it 'generates Mongoid config' do - install_rails + prepare_new_rails_app 'mongoid-test-config' do + mongoid_config_file = File.join(TMP_BASE, 'mongoid-test-config/config/mongoid.yml') - Dir.chdir(TMP_BASE) do - FileUtils.rm_rf('mongoid-test-config') - check_call(%w(rails new mongoid-test-config --skip-spring --skip-active-record), env: clean_env) + File.exist?(mongoid_config_file).should be false + check_call(%w(rails g mongoid:config), env: clean_env) + File.exist?(mongoid_config_file).should be true - Dir.chdir('mongoid-test-config') do - adjust_app_gemfile - check_call(%w(bundle install), env: clean_env) - - mongoid_config_file = File.join(TMP_BASE,'mongoid-test-config/config/mongoid.yml') + config_text = File.read(mongoid_config_file) + config_text.should =~ /mongoid_test_config_development/ + config_text.should =~ /mongoid_test_config_test/ + end + end - File.exist?(mongoid_config_file).should be false - check_call(%w(rails g mongoid:config), env: clean_env) - File.exist?(mongoid_config_file).should be true + it 'generates Mongoid initializer' do + prepare_new_rails_app 'mongoid-test-init' do + mongoid_initializer = File.join(TMP_BASE, 'mongoid-test-init/config/initializers/mongoid.rb') - config_text = File.read(mongoid_config_file) - config_text.should =~ /mongoid_test_config_development/ - config_text.should =~ /mongoid_test_config_test/ - end + File.exist?(mongoid_initializer).should be false + check_call(%w(rails g mongoid:config), env: clean_env) + File.exist?(mongoid_initializer).should be true end end end