Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnklint/248940 to your computer and use it in GitHub Desktop.
Save arnklint/248940 to your computer and use it in GitHub Desktop.
MongoDB Rails Template
# mongo_template.rb
# fork of Ben Scofield's Rails MongoMapper Template (http://gist.github.com/181842)
#
# To use:
# rails project_name -m http://gist.github.com/raw/248940/c31e540b1b68236697fe8c0f32e6b83a39978797/Rails%20MongoMapper%20Template.rb
# remove unneeded defaults
run "rm public/index.html"
run "rm public/images/rails.png"
run "rm public/javascripts/controls.js"
run "rm public/javascripts/dragdrop.js"
run "rm public/javascripts/effects.js"
run "rm public/javascripts/prototype.js"
# add basic layout to start
file 'app/views/layouts/application.html.erb', <<-HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Application!</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js" type="text/javascript"></script>
<%= stylesheet_link_tag 'global' %>
</head>
<body>
<%= yield %>
</body>
</html>
HTML
# MongoDB FTW!
db_name = ask('What should I call the database? ')
initializer 'mongodb.rb', <<-CODE
include MongoMapper
db_config = YAML::load(File.read(File.join(Rails.root, "/config/database.yml")))
if db_config[Rails.env] && db_config[Rails.env]['adapter'] == 'mongodb'
mongo = db_config[Rails.env]
MongoMapper.connection = Mongo::Connection.new(mongo['host'] || 'localhost',
mongo['port'] || 27017,
:logger => Rails.logger)
MongoMapper.database = mongo['database']
if mongo['username'] && mongo['password']
MongoMapper.database.authenticate(mongo['username'], mongo['password'])
end
end
ActionController::Base.rescue_responses['MongoMapper::DocumentNotFound'] = :not_found
#MongoMapper.database = "#{db_name}-\#{Rails.env}"
#if defined?(PhusionPassenger)
# PhusionPassenger.on_event(:starting_worker_process) do |forked|
# MongoMapper.database.connect_to_master if forked
# end
#end
CODE
file 'config/database.yml', <<-CODE
# Using MongoDB!
base: &base
adapter: mongodb
database: "#{db_name}"
#These are needed to authenticate with your db
#should it run on another server
host: localhost
# username: your-username
# password: your-password
development:
<<: *base
test:
<<: *base
production:
<<: *base
database: "#{db_name}-production"
CODE
# Don't need ActiveRecord
environment 'config.frameworks -= [:active_record]'
# MongoMapper
gem 'mongo_mapper'
# Testing Helper
file 'test/test_helper.rb', <<-CODE
ENV['RAILS_ENV'] = 'test'
require File.expand_path(File.dirname(__FILE__) + '/../config/environment')
require 'test_help'
require 'shoulda'
require 'mocha'
require 'factory_girl'
class ActiveSupport::TestCase
# Drop all columns after each test case.
def teardown
MongoMapper.database.collections.each do |coll|
coll.remove
end
end
# Make sure that each test case has a teardown
# method to clear the db after each test.
def inherited(base)
base.define_method teardown do
super
end
end
end
CODE
# Testing tools
gem 'redgreen'
gem 'shoulda'
gem 'factory_girl'
gem 'mocha'
# Gem management
rake 'gems:install'
rake 'gems:unpack'
#rake 'rails:freeze:gems'
# source control
file '.gitignore', <<-FILES
.DS_Store
**/.DS_Store
log/*
tmp/*
tmp/**/*
coverage/*
coverage/**/*
FILES
git :init
git :add => '.'
git :commit => '-a -m "Initial commit"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment