diff --git a/config/initializers/mongo.rb b/config/initializers/mongo.rb index 650d386..7fb7d5f 100644 --- a/config/initializers/mongo.rb +++ b/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 @@ -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'] @@ -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 -