Skip to content

Instantly share code, notes, and snippets.

@mdesantis
Created April 25, 2012 11:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mdesantis/2489048 to your computer and use it in GitHub Desktop.
Save mdesantis/2489048 to your computer and use it in GitHub Desktop.
Rails 3 template (Postgres, RSpec, Capybara, Guard/Linux, Spork)
#!/usr/bin/env ruby -w
#
# Rails 3 template (Postgres, RSpec, Capybara, Guard/libnotify4Linux, Spork)
# This should be used in this way:
#
# rails new APP_NAME -d postgresql -T --skip-bundle -m https://raw.github.com/gist/2489048/rails_3_template.rb
# 1. Non-SSL source proposal (due to bundler bug)
if yes?("Do you want to use the non-SSL source of rubygems.org as gems source?")
# run %q(sed -i 's/https:\/\/rubygems.org/http:\/\/rubygems.org/' Gemfile)
gsub_file 'Gemfile', 'https://rubygems.org', 'http://rubygems.org'
end
# 2. Gems
gem 'therubyracer', :platform => :ruby
gem 'slim-rails'
gem_group :development do
gem 'irbtools'
gem 'libnotify'
gem 'guard-spork'
gem 'guard-rspec'
end
gem_group :development, :test do
gem 'spork-rails'
gem "rspec-rails"
gem 'capybara'
gem 'factory_girl_rails'
end
# 3. bundle install
run 'bundle install'
# 4. generators
generate "rspec:install"
# 5. RSpec: Capybara support
create_file "spec/support/capybara.rb", <<-RUBY
require 'capybara/rspec'
require 'capybara/rails'
RUBY
create_file "spec/requests/home_spec.rb", <<-RUBY
require 'spec_helper'
describe 'visiting the homepage' do
before do
visit '/'
end
it 'should have a body' do
page.should have_css('body')
end
end
RUBY
# 6. RSpec: focus filter
# Adding focus filter to RSpec so we can choose the spec that
# will be run (see http://asciicasts.com/episodes/285-spork)
inject_into_file 'spec/spec_helper.rb', :after => "RSpec.configure do |config|\n" do
<<-RUBY
# Adding focus filter to RSpec so we can choose the spec that
# will be run (see http://asciicasts.com/episodes/285-spork)
config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
RUBY
end
# 7. RSpec: Removing ActiveRecord fixtures support
gsub_file 'spec/spec_helper.rb', 'config.fixture_path = "#{::Rails.root}/spec/fixtures"', '# config.fixture_path = "#{::Rails.root}/spec/fixtures"'
# 8. config/database.yml modifications
if yes?("Do you want to set the database host to '/var/run/postgresql'?")
inject_into_file "config/database.yml", " host: /var/run/postgresql\n", :after => "development:\n"
# Added :force => true, due to this:
# http://stackoverflow.com/questions/10314893/rails-3-template-strange-beahaviour-of-inject-into-file
inject_into_file "config/database.yml", " host: /var/run/postgresql\n", :after => "production:\n", :force => true
inject_into_file "config/database.yml", " host: /var/run/postgresql\n", :after => "test:\n", :force => true
end
if yes?('Do you want to change the database username setting it to the current Unix username?')
username = ENV['USER']
gsub_file 'config/database.yml', /^( username: ).*$/, '\1%s' % username
# run %Q(sed -i 's/ username: .*/ username: #{username}/g' config/database.yml)
end
# 9. Spork https://github.com/sporkrb/spork-rails
run 'bundle exec spork rspec --bootstrap'
# 10. Guard https://github.com/guard/guard-spork
# https://github.com/guard/guard-rspec
# In guard-spork is written to load before spork and then rspec, so:
run 'bundle exec guard init spork'
run 'bundle exec guard init rspec'
# Adding --drb so guard uses Spork
gsub_file 'Guardfile', "guard 'rspec', :version => 2 do", "guard 'rspec', :version => 2, :cli => '--drb' do"
# Setting libnotify as notification library
inject_into_file 'Guardfile', "notification :libnotify\n\n", :before => /^guard 'spork'.*/
# FactoryGirl reloading each tests run
inject_into_file 'spec/spec_helper.rb', " FactoryGirl.reload\n", :after => "Spork.each_run do\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment