Skip to content

Instantly share code, notes, and snippets.

@oma
Created May 5, 2011 16:15
Show Gist options
  • Save oma/957341 to your computer and use it in GitHub Desktop.
Save oma/957341 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
@oma
Copy link
Author

oma commented May 5, 2011

To supplement rspec example with mongodb for helping out at stackoverflow
http://stackoverflow.com/questions/5796240/tdd-rspec-ruby-mongodb-ruby-mongo-driver/5901577#5901577

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment