Skip to content

Commit

Permalink
MONGOID-5439 Generate config/initializers/mongoid.rb (#5529)
Browse files Browse the repository at this point in the history
* 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 05e2be9.

* 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)
  • Loading branch information
jamis authored Feb 6, 2023
1 parent a596006 commit cb7e0bd
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 51 deletions.
8 changes: 7 additions & 1 deletion docs/reference/configuration.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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``.
Expand Down
11 changes: 8 additions & 3 deletions docs/reference/rails-integration.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 10 additions & 6 deletions docs/tutorials/getting-started-rails6.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 <run-locally>` and
:ref:`Use MongoDB Atlas <use-atlas>` to decide how you would like to deploy
Expand Down
16 changes: 10 additions & 6 deletions docs/tutorials/getting-started-rails7.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 <configure-self-managed>`
and :ref:`Configure for MongoDB Atlas <configure-atlas>` to decide how you
Expand Down
6 changes: 5 additions & 1 deletion lib/mongoid/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions lib/rails/generators/mongoid/config/config_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
24 changes: 24 additions & 0 deletions lib/rails/generators/mongoid/config/templates/mongoid.rb
Original file line number Diff line number Diff line change
@@ -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
71 changes: 39 additions & 32 deletions spec/integration/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cb7e0bd

Please sign in to comment.