ruby-on-rails-logo
I’ve recently started doing most of my server side web development work in Ruby as opposed to PHP. This has little to do with preference. I accepted a new software developer position and it just so happens they the product is built with Ruby. I happen to really like working with PHP and I have no plans of abandoning the PHP community. So no flame wars please.

Having said that I am a ruby noob, and in order to ramp up quickly I’ve decided to build my next personal project using Ruby and the Rails framework. It turns out setting up a production quality project does take some work. I’ve documented my process here so that I can refer to it in the future. Hopefully it will help someone else with their process. This document is adapted mostly from http://ruby.railstutorial.org/

This document assumes you have already installed Ruby and RVM. If not please do so first. See my Setting up RVM post for help.  In this document we accomplish the following:

  1. Setting up testing framework with guard, rspec and spork
  2. Setting up a git repository
  3. Deploying with Heroku

Lets get started

First lets create the application. We will be using rspec for testing so we’ll use the --skip-test-unit flag

rails new stylistcity-h --skip-test-unit
cd stylistcity-h

The Rails Gemfile

The next step is to update the gemfile to automatically install all our gems. Replace your Gemfile code with the following

source 'https://rubygems.org'

gem 'rails', '3.2.9'

group :development, :test do
  gem 'sqlite3', '1.3.5'
  gem 'rspec-rails', '2.11.0'
  gem 'guard-rspec', '1.2.1'
  gem 'guard-spork', '1.2.0'
  gem 'spork', '0.9.2'
end

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '3.2.5'
  gem 'coffee-rails', '3.2.2'
  gem 'uglifier', '1.2.3'
end

gem 'jquery-rails', '2.0.2'

group :test do
  gem 'capybara', '1.1.2'
  # mac os x specific gems
  gem 'rb-fsevent', '0.9.1', :require => false
  gem 'growl', '1.0.3'

  #linux specific gems
  #gem 'rb-inotify', '0.8.8'
  #gem 'libnotify', '0.5.9'

  #windows soecific gems
  #gem 'rb-fchange', '0.0.5'
  #gem 'rb-notifu', '0.0.4'
  #gem 'win32console', '1.3.0'
end

group :production do
  gem 'pg', '0.12.2'
end

Remember to uncomment your os specific gems

Now lets go ahead and install our gems
bundle install --without production

Setting up the testing environment

The next step is to setup rspec for testing. Many rails shops use rspec instead of the develop rails testing framework.

Install rspec


rails generate rspec:install
bundle exec guard init rspec

Setup Spork

The Spork server speeds up testing by preloading the rails environment.
bundle exec spork --bootstrap

Edit the Spork.prefork block in the spec/spec_helper.rb file

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|
    # == 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

    # 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
  end
end

Configure RSpec to automatically use Spork

Edit the .rspec file with the following

--colour
--drb

Spork is especially useful when used with Guard, which we can arrange as follows:
bundle exec guard init spork

Replace the  the Gardfile with the following

# A sample Guardfile
# More info at https://github.com/guard/guard#readme

require 'active_support/core_ext'

guard 'spork', :rspec_env => { 'RAILS_ENV' => 'test' } do
 watch('config/application.rb')
 watch('config/environment.rb')
 watch(%r{^config/environments/.+\.rb$})
 watch(%r{^config/initializers/.+\.rb$})
 watch('Gemfile')
 watch('Gemfile.lock')
 watch('spec/spec_helper.rb')
 watch('test/test_helper.rb')
 watch('spec/support/')
end

guard 'rspec', :version => 2, :all_after_pass => false, :cli => '--drb' do
 watch(%r{^spec/.+_spec\.rb$})
 watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
 watch('spec/spec_helper.rb') { "spec" }

# Rails example
 watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
 watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
 watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
 watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
 watch('config/routes.rb') { "spec/routing" }
 watch('app/controllers/application_controller.rb') { "spec/controllers" }

 # Capybara request specs
 watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }

 # Turnip features and steps
 watch(%r{^spec/acceptance/(.+)\.feature$})
 watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end

Now lets run guard and spork together
bundle exec guard

This it for setting up a proper testing environment. Now is also probably a good time to change your readme file especially if you have to push to github

Setting up Git

Create a git repo

git init
git add .
$ git commit -m "Initial commit"

You may optionally want to push to a hosted git server such as Github or BitBucket

Deploying to Heroku

Eventually you will want to deploy your application to ruby server. I have tried several solutions out there, but when it comes to deploying ruby code, Heroku is by far the easiest solution.

Create a heroku account at http://www.heroku.com/ then perform the following steps


heroku login
heroku create
git push heroku master

Thats it!, you how have a rails skeleton app complete with automated testing tools, git repository and easy deployment