Skip to content

Commit cb7e0bd

Browse files
authored
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 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)
1 parent a596006 commit cb7e0bd

File tree

8 files changed

+108
-51
lines changed

8 files changed

+108
-51
lines changed

docs/reference/configuration.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ configuration file for you by running the following command:
4343

4444
rails g mongoid:config
4545

46-
The configuration file will be placed in ``config/mongoid.yml``.
46+
The configuration file will be placed in ``config/mongoid.yml``. An
47+
initializer will also be created and placed in
48+
``config/initializers/mongoid.rb``. It is recommended that all configuration
49+
be specified in ``config/mongoid.yml``, but if you prefer, the ``mongoid.rb``
50+
initializer may also be used to set configuration options. Note, though, that
51+
settings in ``mongoid.yml`` always take precedence over settings in the
52+
initializer.
4753

4854
If you are not using Ruby on Rails, you can copy the minimal configuration
4955
given above and save it as ``config/mongoid.yml``.

docs/reference/rails-integration.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ manually enabled.
1919
Configuration
2020
=============
2121

22-
You can set Mongoid configuration options in your ``application.rb`` along with other Rails
23-
environment specific options by accessing config.mongoid. Options set here will override
24-
those set in your ``config/mongoid.yml``.
22+
You can set Mongoid configuration options in your ``application.rb`` along with
23+
other Rails environment specific options by accessing config.mongoid. The
24+
``mongoid:config`` generator will create an initializer in
25+
``config/initializers/mongoid.rb`` which can also be used for configuring
26+
Mongoid. Note, though, that options set in your ``config/mongoid.yml`` will
27+
take precendence over options set elsewhere; it is recommended that whenever
28+
possible you use ``mongoid.yml`` as the default location for Mongoid
29+
configuration.
2530

2631
.. code-block:: ruby
2732

docs/tutorials/getting-started-rails6.txt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ Add Mongoid
125125

126126
bin/rails g mongoid:config
127127

128-
This generator will create the ``config/mongoid.yml`` configuration file,
129-
which is used to configure the connection to the MongoDB deployment.
130-
Note that as we are not using ActiveRecord we will not have a ``database.yml``
131-
file.
128+
This generator will create the ``config/mongoid.yml`` configuration file
129+
(used to configure the connection to the MongoDB deployment) and the
130+
``config/initializers/mongoid.rb`` initializer file (which may be used for
131+
other Mongoid-related configuration). Note that as we are not using
132+
ActiveRecord we will not have a ``database.yml`` file.
132133

133134

134135
.. _run-locally:
@@ -465,8 +466,11 @@ Generate the default Mongoid configuration:
465466

466467
bin/rails g mongoid:config
467468

468-
This generator will create the ``config/mongoid.yml`` configuration file,
469-
which is used to configure the connection to the MongoDB deployment.
469+
This generator will create the ``config/mongoid.yml`` configuration file
470+
(used to configure the connection to the MongoDB deployment) and the
471+
``config/initializers/mongoid.rb`` initializer file (which may be used for
472+
other Mongoid-related configuration). In general, it is recommended to use
473+
``mongoid.yml`` for all Mongoid configuration.
470474

471475
Review the sections :ref:`Run MongoDB Locally <run-locally>` and
472476
:ref:`Use MongoDB Atlas <use-atlas>` to decide how you would like to deploy

docs/tutorials/getting-started-rails7.txt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ Setup Mongoid
9595

9696
bin/rails g mongoid:config
9797

98-
This generator will create the ``config/mongoid.yml`` configuration file,
99-
which is used to configure the connection to the MongoDB deployment.
100-
Note that as we are not using ActiveRecord we will not have a ``database.yml``
101-
file.
98+
This generator will create the ``config/mongoid.yml`` configuration file
99+
(used to configure the connection to the MongoDB deployment) and the
100+
``config/initializers/mongoid.rb`` initializer file (which may be used for
101+
other Mongoid-related configuration). Note that as we are not using
102+
ActiveRecord we will not have a ``database.yml`` file.
102103

103104

104105
.. _configure-self-managed:
@@ -333,8 +334,11 @@ Generate the default Mongoid configuration:
333334

334335
bin/rails g mongoid:config
335336

336-
This generator will create the ``config/mongoid.yml`` configuration file,
337-
which is used to configure the connection to the MongoDB deployment.
337+
This generator will create the ``config/mongoid.yml`` configuration file
338+
(used to configure the connection to the MongoDB deployment) and the
339+
``config/initializers/mongoid.rb`` initializer file (which may be used for
340+
other Mongoid-related configuration). In general, it is recommended to use
341+
``mongoid.yml`` for all Mongoid configuration.
338342

339343
Review the sections :ref:`Configure for Self Managed MongoDB <configure-self-managed>`
340344
and :ref:`Configure for MongoDB Atlas <configure-atlas>` to decide how you

lib/mongoid/railtie.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ def self.rescue_responses
5151

5252
# Initialize Mongoid. This will look for a mongoid.yml in the config
5353
# directory and configure mongoid appropriately.
54-
initializer "mongoid.load-config" do
54+
#
55+
# It runs after all config/initializers have loaded, so that the YAML
56+
# options can override options specified in
57+
# (e.g.) config/initializers/mongoid.rb.
58+
initializer "mongoid.load-config", after: :load_config_initializers do
5559
config_file = Rails.root.join("config", "mongoid.yml")
5660
if config_file.file?
5761
begin

lib/rails/generators/mongoid/config/config_generator.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
module Mongoid
66
module Generators
77
class ConfigGenerator < Rails::Generators::Base
8-
desc "Creates a Mongoid configuration file at config/mongoid.yml"
8+
desc "Creates Mongoid configuration files"
99

1010
argument :database_name, type: :string, optional: true
1111

@@ -25,9 +25,12 @@ def app_name
2525
end
2626

2727
def create_config_file
28-
template 'mongoid.yml', File.join('config', "mongoid.yml")
28+
template 'mongoid.yml', File.join('config', 'mongoid.yml')
2929
end
3030

31+
def create_initializer_file
32+
template 'mongoid.rb', File.join('config', 'initializers', 'mongoid.rb')
33+
end
3134
end
3235
end
3336
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Mongoid.configure do
2+
target_version = "<%= Mongoid::VERSION[/^\d+\.\d+/] %>"
3+
4+
# Load Mongoid behavior defaults. This automatically sets
5+
# features flags (refer to documentation)
6+
config.load_defaults target_version
7+
8+
# It is recommended to use config/mongoid.yml for most Mongoid-related
9+
# configuration, whenever possible, but if you prefer, you can set
10+
# configuration values here, instead:
11+
#
12+
# config.log_level = :debug
13+
#
14+
# Note that the settings in config/mongoid.yml always take precedence,
15+
# whatever else is set here.
16+
end
17+
18+
# Enable Mongo driver query cache for Rack
19+
# Rails.application.config.middleware.use(Mongo::QueryCache::Middleware)
20+
21+
# Enable Mongo driver query cache for ActiveJob
22+
# ActiveSupport.on_load(:active_job) do
23+
# include Mongo::QueryCache::Middleware::ActiveJob
24+
# end

spec/integration/app_spec.rb

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -86,50 +86,57 @@ def start_app(cmd, port, timeout)
8686
rv
8787
end
8888

89-
context 'new application - rails' do
90-
it 'creates' do
91-
install_rails
89+
def prepare_new_rails_app(name)
90+
install_rails
9291

93-
Dir.chdir(TMP_BASE) do
94-
FileUtils.rm_rf('mongoid-test')
95-
check_call(%w(rails new mongoid-test --skip-spring --skip-active-record), env: clean_env)
92+
Dir.chdir(TMP_BASE) do
93+
FileUtils.rm_rf(name)
94+
check_call(%W(rails new #{name} --skip-spring --skip-active-record), env: clean_env)
9695

97-
Dir.chdir('mongoid-test') do
98-
adjust_app_gemfile
99-
check_call(%w(bundle install), env: clean_env)
96+
Dir.chdir(name) do
97+
adjust_rails_defaults
98+
adjust_app_gemfile
99+
check_call(%w(bundle install), env: clean_env)
100100

101-
check_call(%w(rails g model post), env: clean_env)
102-
check_call(%w(rails g model comment post:belongs_to), env: clean_env)
101+
yield
102+
end
103+
end
104+
end
103105

104-
# https://jira.mongodb.org/browse/MONGOID-4885
105-
comment_text = File.read('app/models/comment.rb')
106-
comment_text.should =~ /belongs_to :post/
107-
comment_text.should_not =~ /embedded_in :post/
108-
end
106+
context 'new application - rails' do
107+
it 'creates' do
108+
prepare_new_rails_app 'mongoid-test' do
109+
check_call(%w(rails g model post), env: clean_env)
110+
check_call(%w(rails g model comment post:belongs_to), env: clean_env)
111+
112+
# https://jira.mongodb.org/browse/MONGOID-4885
113+
comment_text = File.read('app/models/comment.rb')
114+
comment_text.should =~ /belongs_to :post/
115+
comment_text.should_not =~ /embedded_in :post/
109116
end
110117
end
111118

112119
it 'generates Mongoid config' do
113-
install_rails
120+
prepare_new_rails_app 'mongoid-test-config' do
121+
mongoid_config_file = File.join(TMP_BASE, 'mongoid-test-config/config/mongoid.yml')
114122

115-
Dir.chdir(TMP_BASE) do
116-
FileUtils.rm_rf('mongoid-test-config')
117-
check_call(%w(rails new mongoid-test-config --skip-spring --skip-active-record), env: clean_env)
123+
File.exist?(mongoid_config_file).should be false
124+
check_call(%w(rails g mongoid:config), env: clean_env)
125+
File.exist?(mongoid_config_file).should be true
118126

119-
Dir.chdir('mongoid-test-config') do
120-
adjust_app_gemfile
121-
check_call(%w(bundle install), env: clean_env)
122-
123-
mongoid_config_file = File.join(TMP_BASE,'mongoid-test-config/config/mongoid.yml')
127+
config_text = File.read(mongoid_config_file)
128+
config_text.should =~ /mongoid_test_config_development/
129+
config_text.should =~ /mongoid_test_config_test/
130+
end
131+
end
124132

125-
File.exist?(mongoid_config_file).should be false
126-
check_call(%w(rails g mongoid:config), env: clean_env)
127-
File.exist?(mongoid_config_file).should be true
133+
it 'generates Mongoid initializer' do
134+
prepare_new_rails_app 'mongoid-test-init' do
135+
mongoid_initializer = File.join(TMP_BASE, 'mongoid-test-init/config/initializers/mongoid.rb')
128136

129-
config_text = File.read(mongoid_config_file)
130-
config_text.should =~ /mongoid_test_config_development/
131-
config_text.should =~ /mongoid_test_config_test/
132-
end
137+
File.exist?(mongoid_initializer).should be false
138+
check_call(%w(rails g mongoid:config), env: clean_env)
139+
File.exist?(mongoid_initializer).should be true
133140
end
134141
end
135142
end

0 commit comments

Comments
 (0)