Skip to content

Instantly share code, notes, and snippets.

@hyeomans
Forked from samnang/rails31init.md
Last active December 12, 2015 09:29
Show Gist options
  • Save hyeomans/4751896 to your computer and use it in GitHub Desktop.
Save hyeomans/4751896 to your computer and use it in GitHub Desktop.

Install Rails

gem install rails

generate new app, skipping Test::Unit file generation

rails new my_app -T

Set up Gemfile

gem 'jquery-rails'
gem 'haml-rails'
gem 'simple_form'

group :development do
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'meta_request' #https://github.com/dejan/rails_panel
end


group :test, :development do
  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"
  gem 'awesome_print'
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

Setup spork.

spork --bootstrap

Setup the correct spec_helper.rb

This is the full spec_helper.rb

require 'rubygems'
require 'spork'
require 'database_cleaner'

#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'

Spork.prefork do
  # 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.  

  # This file is copied to spec/ when you run 'rails generate rspec:install'
  ENV["RAILS_ENV"] ||= 'test'
  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|
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.start

    config.mock_with :rspec

    # 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 true, the base class of anonymous controllers will be inferred
    # automatically. This will be the default behavior in future versions of
    # rspec-rails.
    config.infer_base_class_for_anonymous_controllers = false

    # Run specs in random order to surface order dependencies. If you find an
    # order dependency and want to debug it, you can fix the order by providing
    # the seed, which is printed after each run.
    #     --seed 1234
    config.order = "random"

    config.include FactoryGirl::Syntax::Methods

    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.filter_run :focus => true
    config.run_all_when_everything_filtered = true
  end
end

Spork.each_run do
  # This code will be run each time you run your specs.
  DatabaseCleaner.clean
  FactoryGirl.reload
end

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

Order matters in your Guardfile

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