Skip to content

Instantly share code, notes, and snippets.

@joker1007
Created September 2, 2010 07:55
Show Gist options
  • Save joker1007/562020 to your computer and use it in GitHub Desktop.
Save joker1007/562020 to your computer and use it in GitHub Desktop.
rails3-template use Rspec
# rails3-rspec-template
#
# using:
# haml
# rspec
# cucumber
# guard
# spork
# Copy database.yml
run "cp config/database.yml config/database.sample.yml"
# Delete unnecessary files
run "rm README"
run "rm public/index.html"
# generators
gem "rails3-generators", git: 'git://github.com/indirect/rails3-generators.git', :group => [:development]
gem "i18n_generators", :group => [:development]
# haml
gem "haml-rails"
# Use unicorn as the web server
gem 'unicorn'
# Deploy with Capistrano
gem 'capistrano', :group => [:development]
gem 'capistrano_colors', :require => false, :group => [:development]
gem 'capistrano-ext', :require => false, :group => [:development]
# For Test
gem "factory_girl_rails", :group => [:development, :test]
gem "rspec", ">= 2.11.0", :group => [:test, :development]
gem "rspec-rails", ">= 2.11.0", :group => [:test, :development]
gem "spork", ">= 0.9.2", :group => [:test, :development]
gem 'capybara', :group => [:test, :development]
gem 'capybara-webkit', :group => [:test, :development]
gem 'minitest', :group => [:test, :development]
gem "launchy", :group => [:test, :development]
gem "awesome_print", :group => [:test, :development]
gem "tapp", :group => [:test, :development]
gem "guard-spork", :group => [:test, :development]
gem "guard-rspec", :group => [:test, :development]
gem "parallel_tests", :group => [:test, :development]
gem "rb-readline", :group => [:test]
gem "turn", :require => false, :group => [:test]
gem "simplecov", :require => false, :group => [:test]
gem "simplecov-rcov", :require => false, :group => [:test]
gem "delorean", :group => [:test]
gem "database_cleaner", :group => [:test]
# Cucumber
if cucumber = yes?("Use cucumber?")
gem 'cucumber', :group => [:test, :development]
gem 'cucumber-rails', :group => [:test, :development], :require => false
gem "guard-cucumber", :group => [:test, :development]
end
# Pry
gem "pry-rails", :group => [:development, :test]
gem "pry-remote", :group => [:development, :test]
# Notification
case RUBY_PLATFORM.downcase
when /linux/
gem "libnotify", :group => [:development, :test]
gem "rb-inotify", :group => [:development, :test]
when /darwin/
gem "ruby_gntp"
gem "rb-fsevent", :group => [:development, :test]
end
# jasmine
if jasmine = yes?("Use jasmine?")
gem "jasmine", :group => [:test, :development]
gem "jasmine-headless-webkit", :group => [:test, :development]
gem "jasmine-spec-extras", :git => "git://github.com/johnbintz/jasmine-spec-extras.git", :group => [:test, :development]
gem "guard-jasmine-headless-webkit", :group => [:test, :development]
end
# kaminari
if yes?("Use kaminari?")
gem "kaminari"
end
# devise
if yes?("Use devise?")
gem "devise"
end
# twitter-bootstrap
if yes?("Use twitter-bootstrap")
gem "bootstrap-sass", :git => "git://github.com/thomas-mcdonald/bootstrap-sass.git"
end
run "bundle install --path vendor/bundle"
# Add .gitignore
create_file "log/.gitkeep"
create_file "tmp/.gitkeep"
ignore_files = <<-END
# Ignore bundler config
/.bundle
# Ignore the default SQLite database.
/db/*.sqlite3
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp
.DS_Store
*.rbc
*.sassc
capybara-*.html
.rspec
config/database.yml
config/*.yml
db/schema.rb
vendor/bundle
public/system/*
coverage/
spec/tmp/*
**.orig
rerun.txt
Guardfile
db/structure.sql
db/development_structure.sql
public/uploads
jhw*.html
.jhw-cache
runner.html
*~
tags
END
run "echo \"#{ignore_files}\" >> .gitignore"
# Execute generator
generate "rspec:install"
run %q{echo "--format d" >> .rspec}
run %q{echo "--drb" >> .rspec}
run %q{cp spec/spec_helper.rb spec/spec_helper.rb.bak}
spec_helper = <<'RUBY'
require 'spork'
require 'simplecov'
# --- Instructions ---
# Sort the contents of this file into a Spork.prefork and a Spork.each_run
# block.
#
# The Spork.prefork block is run only once when the spork server is started.
# You typically want to place most of your (slow) initializer code in here, in
# particular, require'ing any 3rd-party gems that you don't normally modify
# during development.
#
# The Spork.each_run block is run each time you run your specs. In case you
# need to load files that tend to change during development, require them here.
# With Rails, your application modules are loaded automatically, so sometimes
# this block can remain empty.
#
# Note: You can modify files loaded *from* the Spork.each_run block without
# restarting the spork server. However, this file itself will not be reloaded,
# so if you change any of the code inside the each_run block, you still need to
# restart the server. In general, if you have non-trivial code in this file,
# it's advisable to move it into a separate file so you can easily edit it
# without restarting spork. (For example, with RSpec, you could move
# non-trivial code into a file spec/support/my_helper.rb, making sure that the
# spec/support/* files are require'd from inside the each_run block.)
#
# Any code that is left outside the two blocks will be run during preforking
# *and* during each_run -- that's probably not what you want.
#
# These instructions should self-destruct in 10 seconds. If they don't, feel
# free to delete them.
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
# for SimpleCov
unless ENV['DRB']
require 'simplecov'
SimpleCov.merge_timeout 3600
SimpleCov.command_name 'rspec'
SimpleCov.start 'rails'
end
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
# If you use Mongoid, comment out following lines.
# require "rails/mongoid"
# Spork.trap_class_method(Rails::Mongoid, :load_models)
require "rails/application"
Spork.trap_method(Rails::Application, :reload_routes!)
# This file is copied to spec/ when you run 'rails generate rspec:install'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
# config.include Mongoid::Matchers
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If you use database_cleaner, comment out following line.
# config.before(:suite) do
# DatabaseCleaner.strategy = :transaction
# DatabaseCleaner.clean_with(:truncation)
# end
# config.before(:each) do
# DatabaseCleaner.start
# end
# config.after(:each) do
# DatabaseCleaner.clean
# end
end
# for Delorean
config.include Delorean
end
Spork.each_run do
if Spork.using_spork?
ActiveSupport::Dependencies.clear
ActiveRecord::Base.instantiate_observers
end
# This code will be run each time you run your specs.
FactoryGirl.reload
# for SimpleCov
if ENV['DRB']
require 'simplecov'
SimpleCov.merge_timeout 3600
SimpleCov.command_name 'rspec'
SimpleCov.start 'rails'
end
end
RUBY
File.open("spec/spec_helper.rb", "w") {|f| f.write spec_helper}
# Cucumber support env
if cucumber
generate "cucumber:install", "--capybara", "--rspec", "--spork"
run %q{cp features/support/env.rb features/support/env.rb.bak}
support_env = <<'RUBY'
# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
# It is recommended to regenerate this file in the future when you upgrade to a
# newer version of cucumber-rails. Consider adding your own code to a new file
# instead of editing this one. Cucumber will automatically load all features/**/*.rb
# files.
require 'spork'
require 'cucumber/rspec/doubles'
Spork.prefork do
require "rails/application"
Spork.trap_method(Rails::Application, :reload_routes!)
require 'cucumber/rails'
# for SimpleCov
unless ENV['DRB']
require 'simplecov'
SimpleCov.merge_timeout 3600
SimpleCov.command_name 'cucumber'
SimpleCov.start 'rails'
end
# Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
# order to ease the transition to Capybara we set the default here. If you'd
# prefer to use XPath just remove this line and adjust any selectors in your
# steps to use the XPath syntax.
Capybara.default_selector = :css
Capybara.javascript_driver = :webkit
end
Spork.each_run do
if Spork.using_spork?
ActiveSupport::Dependencies.clear
ActiveRecord::Base.instantiate_observers
end
# This code will be run each time you run your specs.
# for SimpleCov
if ENV['DRB']
require 'simplecov'
SimpleCov.merge_timeout 3600
SimpleCov.command_name 'cucumber'
SimpleCov.start 'rails'
end
# By default, any exception happening in your Rails application will bubble up
# to Cucumber so that your scenario will fail. This is a different from how
# your application behaves in the production environment, where an error page will
# be rendered instead.
#
# Sometimes we want to override this default behaviour and allow Rails to rescue
# exceptions and display an error page (just like when the app is running in production).
# Typical scenarios where you want to do this is when you test your error pages.
# There are two ways to allow Rails to rescue exceptions:
#
# 1) Tag your scenario (or feature) with @allow-rescue
#
# 2) Set the value below to true. Beware that doing this globally is not
# recommended as it will mask a lot of errors for you!
#
ActionController::Base.allow_rescue = false
# Remove/comment out the lines below if your app doesn't have a database.
# For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
begin
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
# You may also want to configure DatabaseCleaner to use different strategies for certain features and scenarios.
# See the DatabaseCleaner documentation for details. Example:
#
# Before('@no-txn,@selenium,@culerity,@celerity,@javascript') do
# DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
# end
#
# Before('~@no-txn', '~@selenium', '~@culerity', '~@celerity', '~@javascript') do
# DatabaseCleaner.strategy = :transaction
# end
#
# Possible values are :truncation and :transaction
# The :transaction strategy is faster, but might give you threading problems.
# See https://github.com/cucumber/cucumber-rails/blob/master/features/choose_javascript_database_strategy.feature
Cucumber::Rails::Database.javascript_strategy = :truncation
FactoryGirl.reload
end
RUBY
File.open("features/support/env.rb", "w") {|f| f.write support_env}
end
generate :i18n, "ja"
run "bundle exec guard init spork"
run "bundle exec guard init rspec"
if cucumber
run "bundle exec guard init cucumber"
end
if jasmine
run "bundle exec guard init jasmine-headless-webkit"
end
run %q{cp Guardfile Guardfile.sample}
gsub_file 'Guardfile', /guard 'rspec'.*$/, "guard 'rspec', :version => 2, :cli => '--format d --color --drb', :all_on_start => false, :all_after_pass => false do"
# Generator setting
generators = <<-GENERATORS
config.generators do |g|
g.template_engine :haml
g.test_framework :rspec, :fixture => true
g.fixture_replacement :factory_girl, :dir => "spec/factories"
g.integration_tool :rspec, :fixture => true, :views => true
end
GENERATORS
application generators
# password filter
gsub_file 'config/application.rb', /:password/, ':password, :password_confirmation'
# spork hack
spork_hack = <<RUBY
### Part of a Spork hack. See http://bit.ly/arY19y
if Rails.env.test?
initializer :after => :initialize_dependency_mechanism do
# Work around initializer in railties/lib/rails/application/bootstrap.rb
ActiveSupport::Dependencies.mechanism = :load
end
end
RUBY
inject_into_file 'config/application.rb', :after => "config.assets.version = '1.0'\n" do
spork_hack
end
# lib autoload
inject_into_file 'config/application.rb', :after => 'config.autoload_paths += %W(#{config.root}/extras)' do <<-'RUBY'
config.autoload_paths += %W(#{config.root}/lib)
RUBY
end
# Commit git
git :init
git :submodule => "init"
git :add => "."
run "git add -f config/database.sample.yml"
run "git add -f tmp/.gitkeep"
run "git add -f log/.gitkeep"
git :commit => "-a -m 'First commit.'"
docs = <<-DOCS
if use Mongoid, Edit config/application.rb & spec/spec_helper.rb
http://mongoid.org/docs/installation/configuration.html
DOCS
log docs
# vim:ft=ruby
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment