Skip to content

Instantly share code, notes, and snippets.

@dennisreimann
Created February 28, 2011 22:37
Show Gist options
  • Save dennisreimann/848195 to your computer and use it in GitHub Desktop.
Save dennisreimann/848195 to your computer and use it in GitHub Desktop.
My template for generating new rails apps
# Helpers
def git_update(message)
git :add => ".", :commit => "-m '#{message}'"
end
def git_remove(file)
git :rm => file
end
# Flags
devise_flag = yes?("Do you want to use Devise?")
if devise_flag
devise_class = ask("What is the name of your Devise class? (i.e. 'User')")
devise_class = 'User' if devise_class.blank?
devise_class_underscored = devise_class.underscore
end
vlad_flag = yes?("Do you want to use Vlad?")
# Git
say "Git setup", :yellow
append_file '.gitignore' do
<<-RUBY
.DS_Store
.sass-cache
config/database.yml
log/*.log
coverage
*.pid
public/system
public/uploads
rerun.txt
capybara*.html
vendor/cache/*
RUBY
end
git :init
git_update "Initial commit with basic rails app"
# Crap
say "removing unneccessary files...", :yellow
git_remove 'public/index.html'
git_remove 'public/favicon.ico'
git_remove 'README'
git_update "Removed unneccessary files"
# README
vlad_readme = "
Deployment
----------
bundle exec rake vlad:deploy"
create_file "README.md" do
<<-RUBY
#{app_name}
====================
Setup
-----
* `bundle`
* `cp config/database.example.yml config/database.yml`
* Make the necessary changes to the database configuration
* `bundle exec rake db:setup`
* `rails server`
* Rock on!
Testing
-------
Start them with the following commands:
* `bundle exec guard`
* `bundle exec cucumber`
#{vlad_readme if vlad_flag}
RUBY
end
git_update "Added a proper README"
# RVMRC
say ".rvmrc setup", :yellow
rvm_lib_path = "#{`echo $rvm_path`.strip}/lib"
$LOAD_PATH.unshift(rvm_lib_path) unless $LOAD_PATH.include?(rvm_lib_path)
require 'rvm'
rvm_env = RVM::Environment.current
create_file ".rvmrc" do
"rvm use #{rvm_env.environment_name} --create"
end
git_update "Added .rvmrc"
# Gems
say "Gemfile setup", :yellow
gemfile_devise = "
gem 'devise'"
gemfile_vlad = "
# Deployment
gem 'vlad', :require => false
gem 'vlad-git', :require => false
gem 'vlad-extras', :require => false"
remove_file "Gemfile"
create_file "Gemfile" do
<<-RUBY
source "http://rubygems.org"
# Essentials
gem 'rake', '~> 0.9.2'
gem 'rails', '3.1.3'
gem 'bcrypt-ruby'
gem 'mysql2'#{gemfile_devise if devise_flag}
gem 'simple_form'
gem 'responders'
gem 'settingslogic'
gem 'awesome_print'
gem 'haml-rails'
gem 'jquery-rails'
# Test data and seeds
gem 'factory_girl_rails'
gem 'faker'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'compass'
gem 'sass-rails'
gem 'coffee-rails'
gem 'uglifier'
end
group :development do
gem 'mongrel', '1.2.0.pre2'
gem 'active_reload'
gem 'rails-dev-tweaks'
gem 'query_reviewer', :git => 'git://github.com/nesquena/query_reviewer.git'#{gemfile_vlad if vlad_flag}
end
group :development, :test do
gem 'rspec-rails'
gem 'cucumber-rails'
# Guard
gem 'rb-fsevent', :require => false
gem 'guard-bundler'
gem 'guard-spork'
gem 'guard-rspec'
gem 'guard-livereload'
end
group :test do
gem 'launchy'
gem 'email_spec'
gem 'database_cleaner'
gem 'capybara'
gem 'capybara-webkit'
gem 'fuubar'
gem 'fuubar-cucumber'
gem 'spork', '> 0.9.0.rc'
end
RUBY
end
run 'bundle install'
git_update "Added some gems"
# Vlad setup
if vlad_flag
say "Vlad setup", :yellow
create_file "config/deploy.rb" do
<<-RUBY
set :user, "deploy"
set :application, "#{app_name}"
set :domain, "\#{application}.com"
set :deploy_to, "/var/www/\#{application}"
set :repository, "PLEASE_SET_THE_REPOSITORY_LOCATION"
set :bundle_cmd, "/usr/local/bin/bundle"
set :skip_scm, false
set :copy_shared, {
'config/database.yml' => 'config/database.yml' }
set :symlinks, {
'assets' => 'public/assets',
'config/database.yml' => 'config/database.yml' }
# Revision defaults to master
# set :revision, "origin/develop"
role :app, domain
role :web, domain
role :db, domain, :primary => true
require 'bundler/vlad'
# If Vlad does not find some commands, try the following:
#
# To enable per user PATH environments for ssh logins you
# need to add to your sshd_config:
# PermitUserEnvironment yes
#
# After that, restart sshd!
#
# Then in your "users" ssh home directory (~/.ssh/environment),
# add something to this effect (your mileage will vary):
# PATH=/opt/ruby-1.8.7/bin:/usr/local/bin:/bin:/usr/bin
#
# For details on that, see:
# http://zerobearing.com/2009/04/27/capistrano-rake-command-not-found
#
# Maybe you also need to configure SSH Agent Forwarding:
#
# $ ssh-add ~/.ssh/<private_keyname>
#
# Edit your ~/.ssh/config file and add something like this:
# Host <name>
# HostName <ip or host>
# User <username>
# IdentityFile ~/.ssh/<filename>
# ForwardAgent yes
#
# For details on that, see:
# http://jordanelver.co.uk/articles/2008/07/10/rails-deployment-with-git-vlad-and-ssh-agent-forwarding/
RUBY
end
inject_into_file "Rakefile", :after => "require 'rake'\n" do
<<-RUBY
begin
require 'vlad'
require 'vlad-extras'
Vlad.load(scm: :git, web: :nginx, app: :passenger, type: :rails)
rescue LoadError
puts 'Could not load Vlad'
end
RUBY
end
git_update "Setup vlad"
end
say "Settingslogic setup", :yellow
create_file "app/models/settings.rb" do
<<-RUBY
class Settings < Settingslogic
source "\#{Rails.root}/config/application.yml"
namespace Rails.env
end
RUBY
end
create_file "config/application.yml" do
<<-RUBY
defaults: &defaults
app_name: '#{app_name}'
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
RUBY
end
git_update "Setup settingslogic"
say "responders setup", :yellow
generate "responders:install"
git_update "Setup responsers"
say "simple_form setup", :yellow
generate "simple_form:install"
git_update "Setup simple_form"
say "RSpec setup", :yellow
generate 'rspec:install'
remove_file "spec/spec_helper.rb"
create_file "spec/spec_helper.rb" do
<<-RUBY
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require "rails/application"
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.filter_run focus: true
config.use_transactional_fixtures = true
config.run_all_when_everything_filtered = true
config.treat_symbols_as_metadata_keys_with_true_values = true
end
end
Spork.each_run do
FactoryGirl.reload
end
RUBY
end
git_update "Setup rspec"
say "Cucumber setup", :yellow
generate 'cucumber:install', '--rspec'
remove_file "config/cucumber.yml"
create_file "config/cucumber.yml" do
<<-RUBY
<%
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format \#{ENV['CUCUMBER_FORMAT'] || 'fuubar'} features" : "--format \#{ENV['CUCUMBER_FORMAT'] || 'pretty'} \#{rerun}"
std_opts = "--format \#{ENV['CUCUMBER_FORMAT'] || 'fuubar'} --strict --tags ~@wip"
%>
default: <%= std_opts %> --drb features
wip: --drb --tags @wip:3 --wip features
rerun: <%= rerun_opts %> --drb --format rerun --out rerun.txt --strict --tags ~@wip
RUBY
end
remove_file "features/support/env.rb"
create_file "features/support/env.rb" do
<<-RUBY
require 'rubygems'
require 'spork'
Spork.prefork do
require "rails/application"
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
require 'cucumber/rails'
Capybara.default_selector = :css
ActionController::Base.allow_rescue = false
DatabaseCleaner.strategy = :transaction
Cucumber::Rails::Database.javascript_strategy = :truncation
end
Spork.each_run do
FactoryGirl.reload
end
RUBY
end
git_update "Setup cucumber"
say "Guard setup", :yellow
create_file "Guardfile" do
<<-RUBY
guard 'bundler' do
watch('Gemfile')
end
guard 'spork', cucumber_env: { 'RAILS_ENV' => 'test' }, rspec_env: { 'RAILS_ENV' => 'test' } do
watch('config/application.rb')
watch('config/environment.rb')
watch(%r{^config/environments/.+\.rb$})
watch(%r{^config/initializers/.+\.rb$})
watch('spec/spec_helper.rb')
watch(%r{^spec/support/.+\.rb$})
watch(%r{^features/support/.+\.rb$})
end
guard 'rspec', version: 2, cli: "--drb --format Fuubar", all_on_start: true, all_after_pass: true do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^app/(.+)\.rb$}) { |m| "spec/\#{m[1]}_spec.rb" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/\#{m[1]}_spec.rb" }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('spec/spec_helper.rb') { "spec" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/\#{m[1]}_routing_spec.rb", "spec/\#{m[2]}s/\#{m[1]}_\#{m[2]}_spec.rb", "spec/acceptance/\#{m[1]}_spec.rb"] }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
end
guard 'livereload' do
watch(%r{app/.+\.(erb|haml)})
watch(%r{app/helpers/.+\.rb})
watch(%r{(public/|app/assets).+\.(css|js|html)})
watch(%r{(app/assets/.+\.css)\.s[ac]ss}) { |m| m[1] }
watch(%r{(app/assets/.+\.js)\.coffee}) { |m| m[1] }
watch(%r{config/locales/.+\.yml})
end
RUBY
end
git_update "Setup guard"
# Generators
initializer 'generators.rb', <<-RUBY
Rails.application.config.generators do |g|
g.stylesheet_engine = :sass
g.fixture_replacement :factory_girl, dir: "spec/factories"
g.view_specs false
g.helper_specs false
g.routing_specs false
g.controller_specs false
g.stylesheets false
g.helper false
g.assets false
g.integration_tool false
end
RUBY
git_update "Added generators initializer"
# Database
say "Database setup", :yellow
database_config = "defaults: &defaults
adapter: mysql2
encoding: utf8
reconnect: false
pool: 5
username: root
password:
socket:
development:
<<: *defaults
database: #{app_name}_development
test:
<<: *defaults
database: #{app_name}_test
production:
<<: *defaults
database: #{app_name}"
remove_file "config/database.yml"
create_file "config/database.yml", database_config
create_file "config/database.example.yml", database_config
run 'rake db:setup'
git_update "Setup database"
# Layout
devise_layout_stuff = ""
if devise_flag
create_file "app/views/shared/_account.html.haml", <<-RUBY
#account
- if #{devise_class_underscored}_signed_in?
= "\#{t('hello')} \#{current_#{devise_class_underscored}.email}"
= link_to t('sign_out'), destroy_#{devise_class_underscored}_session_path
- else
= link_to t('sign_in'), new_#{devise_class_underscored}_session_path
|
= link_to t('sign_up'), new_#{devise_class_underscored}_registration_path
RUBY
end
create_file "app/views/layouts/application.html.haml", "!!!
%html
%head
%title= page_title
= stylesheet_link_tag 'application'
= javascript_include_tag 'application'
= csrf_meta_tag
= yield(:head)
%body
%header
%h1= yield :title
%nav
= link_to 'Home', root_url
#content
= render 'shared/flash', flash: flash
= yield
"
create_file "app/helpers/layout_helper.rb", %q{module LayoutHelper
def title(page_title)
content_for :title do
page_title.to_s
end
end
def page_title
app_name = Settings.app_name
content_for(:title).blank? ? app_name : "#{app_name} - #{content_for(:title)}"
end
end}
create_file "app/views/shared/_flash.html.haml", %q{- flash.each do |key, value|
%div{ class: "flash #{key}" }= value}
git_remove "app/views/layouts/application.html.erb"
git_update "Added layout and helpers"
# Devise
if devise_flag
say "Devise setup", :yellow
generate 'devise:install'
puts "modifying environment configuration files for Devise..."
gsub_file 'config/environments/development.rb', /# Don't care if the mailer can't send/, '# ActionMailer'
gsub_file 'config/environments/development.rb', /config.action_mailer.raise_delivery_errors = false/ do
<<-RUBY
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default charset: "utf-8"
RUBY
end
gsub_file 'config/environments/production.rb', /config.i18n.fallbacks = true/ do
<<-RUBY
config.i18n.fallbacks = true
# ActionMailer
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default charset: "utf-8"
RUBY
end
git_update "Setup devise"
puts "creating a #{devise_class} model, modifying routes for Devise..."
generate "devise #{devise_class}"
gsub_file "app/models/#{devise_class_underscored}.rb", /attr_accessible :email, :password, :password_confirmation, :remember_me/ do
<<-RUBY
validates_uniqueness_of :email, :case_sensitive => false
attr_accessible :email, :password, :password_confirmation, :remember_me
RUBY
end
run 'rake db:migrate'
git_update "Added #{devise_class} model"
end
# i18n
gsub_file 'config/application.rb', /# config.i18n.default_locale = :de/, "config.i18n.default_locale = :de"
gsub_file 'config/application.rb', "config.action_view.javascript_expansions[:defaults] = %w()", "# config.action_view.javascript_expansions[:defaults] = %w()"
get "https://github.com/svenfuchs/rails-i18n/raw/master/rails/locale/de.yml", "config/locales/rails.de.yml"
get "https://gist.github.com/raw/848021/responders.de.yml", "config/locales/responders.de.yml"
get "https://gist.github.com/raw/848017/simple_form.de.yml", "config/locales/simple_form.de.yml"
get "https://gist.github.com/raw/848028/devise.de.yml", "config/locales/devise.de.yml" if devise_flag
git_remove 'config/locales/en.yml'
create_file "config/locales/app.de.yml" do
<<-RUBY
de:
hello: "Hallo"
show: "Anzeigen"
back: "Zurück"
save: "Speichern"
edit: "Bearbeiten"
create: "Anlegen"
update: "Bearbeiten"
destroy: "Löschen"
cancel: "Abbrechen"
confirm: "Sind Sie sicher?"
# Layout
sign_up: "Anmelden"
sign_in: "Login"
sign_out: "Logout"
RUBY
end
git_update "Setup locale"
# Homepage
say "Homepage setup", :yellow
generate(:controller, "pages index")
gsub_file 'config/routes.rb', /get \"pages\/index\"/, 'root :to => "pages#index"'
git_update "Added homepage"
#!/usr/bin/env ruby
rvm_ruby = ARGV[0]
app_name = ARGV[1]
template = ARGV[2] || "https://gist.github.com/raw/848195/rails-app-template.rb"
unless rvm_ruby && app_name && template
puts "\nYou need to:\n"
puts "- specify which rvm ruby to use\n" unless rvm_ruby
puts "- specify the name of your app\n" unless app_name
puts "\n"
exit
end
rvm_lib_path = "#{`echo $rvm_path`.strip}/lib"
$LOAD_PATH.unshift(rvm_lib_path) unless $LOAD_PATH.include?(rvm_lib_path)
require 'rvm'
@env = RVM::Environment.new(rvm_ruby)
puts "Creating gemset #{app_name} in #{rvm_ruby}"
@env.gemset_create(app_name)
@env = RVM::Environment.new("#{rvm_ruby}@#{app_name}")
puts "Installing bundler and rails."
puts "Successfully installed bundler" if @env.run("gem install bundler")
puts "Successfully installed rails" if @env.run("gem install rails")
system("rvm #{rvm_ruby}@#{app_name} exec rails new #{app_name} -JT -d mysql -m #{template}")
@arbovm
Copy link

arbovm commented Mar 1, 2011

This is great stuff! Thanks!

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