Reputation: 6121
I have the following line in my Sinatra app:
Mongoid.load!('./config/database/mongoid.yml')
This is nice, but I don't want to keep my connection details in a YAML file, and add it to .gitignore
. I want to keep them in ENV
.
I was able to bypass this in the past by adding stuff like username: <%= ENV['MONGODB_USER'] %>
to the YAML config file, then reading it as ERB, saving it and reading it again with Mongoid.load!
before Heroku wiped the disk. Needless to say, that's pretty nutty.
All I could find is the definition of .load!
over here and it doesn't look like there's any way around this.
Is there some hidden way to programmatically configure Mongoid
connections?
Thanks in advance.
Upvotes: 2
Views: 719
Reputation: 1704
Building on mu's answer:
You can give Mongoid a hash to use for initialization like this:
Mongoid.load_configuration(clients: {
default: {
database: database,
hosts: [ host ]
}
})
Note that the hash you pass to load_configuration
is not expected to start with an environment key like you would normally have in mongoid.yml
.
Upvotes: 3
Reputation: 434805
Mongoid.load!
doesn't do very much:
def load!(path, environment = nil)
settings = Environment.load_yaml(path, environment)
if settings.present?
Sessions.disconnect
Sessions.clear
load_configuration(settings)
end
settings
end
All it does is a bit of bookkeeping, loads the YAML, and hands off to load_configuration
to do the heavy lifting. There's nothing stopping you from building the settings
Hash by hand and calling Mongoid.load_configuration
yourself.
Upvotes: 2