Skip to content

Instantly share code, notes, and snippets.

@volkanunsal
Created June 25, 2012 02:08
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 volkanunsal/2986021 to your computer and use it in GitHub Desktop.
Save volkanunsal/2986021 to your computer and use it in GitHub Desktop.
A better Rails template

A better Rails template

Disclaimer: I'm really opinionated about how Rails should work.

The default Rails template is boring. The Gemfile is a mess - it's not even in alphabetical order! Mainly, it's missing:

  • a better webserver than webrick
  • compass for consistent styling
  • factory_girl
  • foreman
  • haml
  • rspec
  • shoulda
  • synced $STDOUT

And it has a bunch of stuff that no one should ever use:

  • test/unit
  • erb templates
  • lots of annoying comments that experienced developers don't want/need

Usage

rails new my_app -m <raw_url_to_template.rb>
# A better rails template
# Last updated: June 24, 2012
# Created by Seth Vargo
# remove pesky files and directories
remove_dir 'doc'
remove_dir 'log'
remove_dir 'public/assets'
remove_dir 'test'
remove_dir 'tmp'
remove_dir 'vendor'
remove_file 'public/index.html'
remove_file 'README.rdoc'
# gem commands
gsub_file 'Gemfile', /gem 'sqlite3'/, ''
gsub_file 'Gemfile', /gem 'jquery-rails'/, ''
gsub_file 'Gemfile', /\n\n# Bundle edge Rails(.+)rails.git'\n\n\n\n/m, ''
gsub_file 'Gemfile', /\n# Gems used only for assets(.+)default./m, ''
gsub_file 'Gemfile', /\s+# See https:\/\/(.+):ruby\s+$/m, ''
gsub_file 'Gemfile', /^source (.+)$/ do
"source :rubygems"
end
gsub_file 'Gemfile', /^gem 'rails', (.+)$/ do |match| <<-RUBY
gem 'compass'
gem 'foreman'
gem 'haml-rails'
gem 'jquery-rails'
# gem 'puma'
gem 'unicorn',"~> 4.2.1"
#{match}
gem "grape"
gem "devise"
gem 'heroku','~> 2.25.0'
gem "friendly_id", "~> 4.0.1"
gem 'sass-rails', '~> 3.2.3'
gem "backbone-rails"
gem 'marionette-rails'
gem 'urbanairship','~> 2.0.0'
gem 'settingslogic','~> 2.0.6'
gem 'andand','~> 1.3.3'
gem 'kaminari',"~>0.13.0"
#CACHING
gem 'redis-store','1.0.0.beta5'
gem 'hiredis','~> 0.4.5'
gem 'hashie','~> 1.2.0'
#QUEUEING
gem 'resque','~> 1.20.0'
#DATA FAKING
gem 'forgery', '0.5.0'
group :development do
gem 'growl'
gem 'guard'
gem 'guard-bundler'
gem 'guard-rspec'
gem 'guard-spork'
gem 'wirble'
gem 'yard'
gem 'looksee'
gem "taps"
gem 'sqlite3','~>1.3.4'
gem "heroku-rails"
end
group :test do
gem 'capybara'
gem 'factory_girl_rails'
gem 'rspec-rails'
gem 'shoulda'
gem 'simplecov'
gem 'spork-rails'
gem "faker"
gem "mocha"
end
group :assets do
gem 'coffee-rails', '~> 3.2.1'
gem "asset_sync"
end
RUBY
end
gsub_file 'Gemfile', /^\s+gem 'sass-rails', (.+)$/ do |match|
" gem 'compass-rails'\n" +
"#{match}"
end
gsub_file 'Gemfile', /\s+# To use ActiveModel(.+)'debugger'/m, ''
run 'bundle install'
# application.rb
gsub_file 'config/application.rb', "require 'rails/all'" do
"# require 'rails/all' expanded to:\n" +
"require 'active_record/railtie'\n" +
"require 'action_controller/railtie'\n" +
"require 'action_mailer/railtie'\n" +
"require 'active_resource/railtie'\n" +
"# require 'rails/test_unit/railtie'"
end
gsub_file 'config/application.rb', "# config.autoload_paths += %W(\#{config.root}/extras)" do <<-RUBY
config.autoload_paths += %W(\#{config.root}/lib)
config.generators do |g|
g.test_framework :rspec, :views => false, :fixture => true
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
g.template_engine :haml
g.helper = false
end
RUBY
end
append_file 'config/application.rb' do <<-RUBY
STDOUT.sync = true
RUBY
end
# application.rb
gsub_file 'config/application.rb', /^STDOUT\.sync = true?/ do <<-RUBY
YAML::ENGINE.yamler = 'syck'
RUBY
end
# development.rb
gsub_file 'config/environments/development.rb', /^\s+config.assets.debug = true\s+?/ do <<-RUBY
config.assets.debug = false
config.assets.debugger = nil
RUBY
end
# Procfile
create_file 'Procfile' do
"web: bundle exec rails server puma -p $PORT\n" +
"spork: bundle exec guard"
end
# Procfile.local
create_file 'Procfile.local' do
"web: bundle exec rails server -p $PORT\n" +
"spork: bundle exec guard"
end
# Settings
create_file 'config/initializers/settings.rb' do <<-RUBY
if !defined?(Settings)
class Settings < Settingslogic
source File.expand_path('../../../config/application.yml',__FILE__)
namespace ENV['APP_HOST'] || 'defaults'
end
end
RUBY
end
create_file "config/application.yml" do <<-RUBY
# config/application.yml
defaults: &defaults
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
RUBY
end
# rspec
run 'bundle exec rails g rspec:install'
run 'spork rspec --bootstrap'
# Kaminari
run "rails g kaminari:config"
# Forgery
run "rails generate forgery"
# guard
remove_file 'Guardfile'
run 'bundle exec guard init'
# images
remove_file 'app/assets/images/rails.png'
# compass & stylesheets
run 'bundle exec compass init rails'
remove_file 'app/assets/stylesheets/application.css'
remove_file 'app/assets/stylesheets/ie.css.scss'
remove_file 'app/assets/stylesheets/print.css.scss'
remove_file 'app/assets/stylesheets/screen.css.scss'
create_file 'app/assets/stylesheets/application.scss' do
"//= require_self"
end
# javascripts
remove_file 'app/assets/javascripts/application.js'
create_file 'app/assets/javascripts/application.coffee' do
"\#= require jquery\n" +
"\#= require jquery_ujs" +
"\n" +
"\#= require_self"
end
# application layout
remove_file 'app/views/layouts/application.html.erb'
create_file 'app/views/layouts/application.html.haml' do
"!!!
%html
%head
%title My Application
= stylesheet_link_tag 'application', :media => 'all'
= javascript_include_tag 'application'
= csrf_meta_tags
%body
= yield"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment