Created
May 5, 2011 16:15
-
-
Save oma/957341 to your computer and use it in GitHub Desktop.
oma mongomapper setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/mongodb.yml | |
development: | |
uri: <%= ENV['MONGO_URL'] %> | |
test: | |
uri: mongodb://localhost:27017/myapp_test | |
staging: | |
uri: <%= ENV['MONGOHQ_URL'] %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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
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