How can I pass config from Rails 4.1 secrets.yml file to mongoid.yml assuming I have the scenario below:
config/secrets.yml file
default: &mongodb
mongodb_host: <%= ENV['TRG_MONGODB_HOST'] %>
mongodb_host_port: <%= ENV['TRG_MONGODB_HOST_PORT'] %>
mongodb_database: <%= ENV['TRG_MONGODB_DATABASE'] %>
mongodb_db_username: <%= ENV['TRG_MONGODB_DB_USER'] %>
mongodb_db_passowrd: <%= ENV['TRG_MONGODB_DB_PASSWORD'] %>
development:
secret_key_base: xxxxxxxxyyyy
<<: *mongodb
Shortened config/mongoid.yml file
development:
# Configure available database sessions. (required)
sessions:
default:
uri: mongodb://username:password@champ.mongohq.com:17856/ffff
# uri: mongodb://Rails.application.secrets.mongodb_db_username:Rails.application.secrets.mongodb_db_password@Rails.application.secrets.mongodb_host:Rails.application.secrets.mongodb_host_port/Rails.application.secrets.mongodb_database
# uri: mongodb://ENV['mongodb_db_username']:ENV['mongodb_db_password']@ENV['mongodb_host']:ENV['mongodb_host_port']/ENV['mongodb_database']
The only way the mongoid.yml works is when I directly add the database settings using uri: mongodb://username:password@champ.mongohq.com:17856/ffff. If I use uri: mongodb//Rails.application.secrets.xx the settings are not picked up. Also, the settings are not picked up when I use uri: mongodb://ENV['mongodb_db_username'].
I also tried passing the environment variables directly to mongoid.yml using embedded ruby like this uri:mongodb://<%= ENV['TRG_MONGODB_DB_USER'] %> but it also did not work.
Any suggestions on how to make Rails4.1 secrets.yml file to pass settings to mongoid.yml file.
Finally, I was able to fix this by passing config from the secrets.yml file to the mongoid.yml or database.yml file using embedded ruby as described in here: https://coderwall.com/p/3c2alg
I had to explicitly prefix the keys for database settings with symbol in config/secrets.yml file so instead this:
default: &mongodb
mongodb_host: <%= ENV['TRG_MONGODB_HOST'] %>
mongodb_host_port: <%= ENV['TRG_MONGODB_HOST_PORT'] %>
mongodb_database: <%= ENV['TRG_MONGODB_DATABASE'] %>
mongodb_db_username: <%= ENV['TRG_MONGODB_DB_USER'] %>
mongodb_db_passowrd: <%= ENV['TRG_MONGODB_DB_PASSWORD'] %>
We now have this:
config/secrets.yml file
default: &mongodb
:mongodb_host: <%= ENV['TRG_MONGODB_HOST'] %>
:mongodb_host_port: <%= ENV['TRG_MONGODB_HOST_PORT'] %>
:mongodb_database: <%= ENV['TRG_MONGODB_DATABASE'] %>
:mongodb_db_username: <%= ENV['TRG_MONGODB_DB_USER'] %>
:mongodb_db_passowrd: <%= ENV['TRG_MONGODB_DB_PASSWORD'] %>
The second change was to use embedded ruby in the mongodb.yml file, so we have this:
instead of just
Rails.application.secrets.mongodb_db_username
The final uri in the mongoid.yml looks like this:
uri: mongodb://<%= Rails.application.secrets.mongodb_db_username %>:<%= Rails.application.secrets.mongodb_db_password %>@<%= Rails.application.secrets.mongodb_host %>:<%= Rails.application.secrets.mongodb_host_port %>/<%= Rails.application.secrets.mongodb_database %>