Skip to content

Instantly share code, notes, and snippets.

@linojon
Forked from docwhat/rails31init.md
Created September 10, 2011 02:20
Show Gist options
  • Save linojon/1207843 to your computer and use it in GitHub Desktop.
Save linojon/1207843 to your computer and use it in GitHub Desktop.
Rails 3.1 with Rspec, Factory Girl, Haml, Simple Form, Devise, CanCan, Nifty, Database Cleaner, Spork, and Guard

Install Rails 3.1

gem install rails

generate new app, skipping Test::Unit file generation

rails new my_app -T

Set up Gemfile

source 'http://rubygems.org'

gem 'rails', '3.1.0'
gem 'sqlite3'
gem 'jquery-rails'
gem 'haml-rails'
gem 'simple_form'
gem 'devise'
gem 'cancan'

group :development do
  gem 'rails3-generators' # for factory_girl_rails and simple_form
  gem 'nifty-generators', :git => 'git://github.com/PaBLoX-CL/nifty-generators.git' # rails 3.1 pull request 9/2/2011
end

group :test, :development do
  gem 'ruby-debug19', :require => 'ruby-debug'
  gem "rspec-rails"
  gem 'rspec-instafail'
  gem 'rb-fsevent'
  gem 'growl'
  gem 'pry'
  gem 'factory_girl_rails'
  gem 'database_cleaner'
  gem "factory_girl_rails"
  gem "capybara"
  gem 'guard-spork'
  gem "guard-bundler"
  gem "guard-rspec"
  gem "guard-migrate"
end

Install our gems, and scope them to our app

bundle install --path vendor

Use this for all subsequent ````bundle install``` commands. Why? http://ryan.mcgeary.org/2011/02/09/vendor-everything-still-applies/

Configure generators to use the gems we want, and skip view spec generation

# in config/application.rb

config.generators do |g|
  g.test_framework :rspec, :fixture => true
  g.fixture_replacement :factory_girl, :dir => 'spec/factories'
  g.form_builder :simple_form
  g.template_engine :haml
end

turn on autoloading of lib directory and all its subdirectories

In Rails 3+, the lib directory is no longer autoloaded.

# in config/application.rb
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]

run install tasks for our gems

rails g rspec:install
rails g simple_form:install
rails g devise:install
rails g devise User
rails g cancan:ability
rails g nifty:layout --haml --scss

Setup spork.

spork --bootstrap

Then edit spec/spec_helper.rb and move everything into the prefork section.

Setup database cleaner

Edit spec/spec_helper.rb.

Put this in the prefork section:

require 'database_cleaner'
DatabaseCleaner.strategy = :truncation

Put this in the each_run section:

DatabaseCleaner.clean

Factory Girl

Comment out the config.fixture_path line in spec/spec_helper.rb...the comment above implies it shouldn't be used if we're not using ActiveRecord fixtures.

RSpec Instafail

echo '--colour
--drb
--require rspec/instafail
--format RSpec::Instafail' > .rspec

The --drb is needed for spork and --colour is for color, because it's pretty.

Pry

In your config/environments/development.rb, near the end before the YourAppName::Application.configure block, put this:

silence_warnings do
  begin
    require 'pry'
    IRB = Pry
  rescue LoadError
  end
end

Now rails c will startup using pry. You can also add bindings.pry anyplace and your app will stop there with a pry prompt until you exit it.

See Also:

Guard

Run these commands:

guard init
guard init bundler                                                                                                                                 guard init spork                                                                                                                                   guard init migrate                                                                                                                                 guard init rspec

You may have to monkey withy our Guardfile some but the order should be right.

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