Skip to content

Instantly share code, notes, and snippets.

@terry6394
Forked from oma/mongo.rb
Created May 16, 2011 04:07
Show Gist options
  • Select an option

  • Save terry6394/973928 to your computer and use it in GitHub Desktop.

Select an option

Save terry6394/973928 to your computer and use it in GitHub Desktop.
oma mongomapper setup
# config/initializers/mongo.rb
#
# MonkeyPatch to fix the error going on in mongomapper + rails3 + ruby 1.9.2
#
MongoMapper::Plugins.class_eval do
def plugins
@plugins ||= []
end
def plugin(mod)
# for whatever reason mod.const_defined?(:ClassMethods) doesn't work in ruby192rc2+rails3b4 hence this workaround
extend mod::ClassMethods if mod.constants.include?(:ClassMethods)
include mod::InstanceMethods if mod.const_defined?(:InstanceMethods)
mod.configure(self) if mod.respond_to?(:configure)
plugins << mod
end
end
#
# Moved mongo setup to lib/mongo_setup.rb
# To be able to reconnect on Passenger fork from Delayed Job
#
MongoSetup.setup
# config/mongodb.yml
development:
uri: <%= ENV['MONGO_URL'] %>
test:
uri: mongodb://localhost:27017/myapp_test
staging:
uri: <%= ENV['MONGOHQ_URL'] %>
#lib/monogo_setup.rb
module MongoSetup
def self.setup
file_name = File.join(Rails.root, "/config/mongodb.yml")
mongo_config = YAML::load(ERB.new(File.new(file_name).read).result) #OMG this is ugly
raise "set correct mongodb.yml!" unless mongo_config || mongo_config[Rails.env]
mongo = mongo_config[Rails.env]
begin
MongoMapper.config = {Rails.env => {'uri' => mongo['uri'] }}
MongoMapper.connect(Rails.env)
rescue Mongo::ConnectionFailure => e
puts "Cannot connect to mongo, did you forget to start mongod? Is the config/mongodb.yml file correct?", e
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment