Skip to content

Commit

Permalink
honor the multiple configuration methods that does
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Aug 29, 2011
1 parent 59fd674 commit 19bc328
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions config/initializers/mongo.rb
@@ -1,5 +1,9 @@
module Mongo
class << self

# Looks for configuration files in this order
CONFIGURATION_FILES = ["central_logger.yml", "mongoid.yml", "database.yml"]

def db
@db ||= configure
end
Expand All @@ -9,11 +13,11 @@ def collection
end

def configure
config_file = Rails.root.join("config", "database.yml")
config = YAML.load(ERB.new(config_file.read).result)[Rails.env]
config = { 'host' => 'localhost',
'port' => '27017' }.merge(config)
@collection = config["collection"]
@collection = "#{Rails.env}_log"
config = {
'host' => 'localhost',
'port' => 27017 }.merge(resolve_config)
db = Mongo::Connection.new(config['host'], config['port'], :auto_reconnect => true).db(config['database'])

if config['username'] && config['password']
Expand All @@ -22,6 +26,19 @@ def configure
end
db
end

def resolve_config
config = {}
CONFIGURATION_FILES.each do |filename|
config_file = Rails.root.join("config", filename)
if config_file.file?
config = YAML.load(ERB.new(config_file.read).result)[Rails.env]
config = config['mongo'] if config.has_key?('mongo')
break
end
end
config
end

end
end

0 comments on commit 19bc328

Please sign in to comment.