Ruby Lab Day: Making a Todo App Tutorial – Part 1

December 17, 2012

Part 1 – Case Study on Making a Todo Demonstration Application

What we’ll be covering overall

As the year comes to a close, I thought what better way to ring in the new year than to make a small rails tutorial on my blog? This tutorial will cover the building of a simple todo list demonstration application using Rails 3.2.9.

What part 1 covers

Part 1 will be covering the initial setup you have to go through to get up and running. There’s a fair amount to cover, but for some of the more straightforward tasks (such as installing rails or rvm), I’ll ask you to search online for it or provide links to resources that cover this, as these types of topics have been done to death. If you’d rather see an expanded version of these topics instead of links or searching for them, feel free to leave me a comment or email me and I’d be happy to expand on the topic.

Mac or Linux?

The tutorial also assumes you’re using a Linux or Macintosh system. I personally used an Ubuntu 10.04 Linux virtual machine. It also assumes you follow at least preliminary steps 1-4 (installing git is optional). RVM, bundler, and rubygems

Preliminary steps

  1. Install rvm – Ryan Biggs has written up a nice post here on how to install ruby, rails, and rvm for Ubuntu Linux. There’s also the actual documentation for installing rvm here. RVM helps keep your installations “clean” and prevents them from potentially interfering with each other. You don’t have to worry about remembering what version of rails (or gems) you used for a particular project.

  2. Install the latest version of ruby and the latest version of rails. (I suggest following Ryan’s guide above for installing ruby, rails, and rvm).

  3. Install rubygems – This is pretty straightforward. Michael Hartl talks about it in the Rails tutorial (available for free online) or you can search for it.

  4. Install bundler (should automatically be installed when you install ruby 1.9.32)

  5. Create new gemset and new rvmrc file. I called my gemset tododemo329. You can name yours anything you like.

rvm --rvmrc --create use 1.9.3-p194@tododemo329

  1. Install the git version control system (do an Internet search for this)

  2. Create a new git repo and clone

*Note: Installing git is optional for the purposes of this tutorial, but at some point it is highly recommended you get familiar with it as pretty much everyone in the Rails community uses it (you can’t contribute patches to the rails framework if you don’t know git).

Initialize the rails application

rails new tododemo

Install the required gems

Edit the gemfile and add the following lines:

gem 'jquery-rails'
gem 'execjs'
gem 'libv8', '~> 3.11.8'
gem 'therubyracer' #needed for rspec install
gem 'devise', '~> 2.1.2'</code>

group :test, :development do

gem 'annotate', '~> 2.4.1.beta'
gem 'faker','~>1.0.1&#8242;
gem 'rspec-rails', '>= 2.6.1'
gem 'autotest'
gem 'autotest-rails-pure'
gem 'factory_girl_rails', '~>1.6.0' #higher version than in Rails tutorial
gem 'guard-rspec', '~>2.3.0'
gem 'guard-spork', '~>1.2.0' #higher version than in Rails tutorial
gem 'spork', '~>0.9.2'

#Added 2/14/12 Tues for debugging

#gem 'linecache19', '0.5.13'
#gem 'ruby-debug-base19', '0.11.26'
#gem 'ruby-debug19', require: 'ruby-debug'

end

group :test do

gem 'capybara', '~>1.1.2' #used in Rails 3.2.1 tutorial

#Linux system dependent gems for use with guard, Rails 3.2 tutorial

gem 'rb-inotify', '0.8.8'
gem 'libnotify', '0.5.9'
gem 'cucumber-rails', '1.2.1', :require=>false
gem 'database_cleaner', '0.7.0'

end

Now type “bundle install” at the command line.

Why the commented lines?

Those are gems used to help install ruby-debug. It can sometimes help you troubleshoot issues during development. But they have to be manually installed as of this writing (December 2012), so we leave them commented out for now and address it later on in this tutorial.

How to Install Ruby-Debug on Ubuntu Linux

I run an Ubuntu Linux virtual machine from my Windows desktop. As of this writing (December 2012), you have to manually install ruby-debug if you’re running on ruby version 1.9.3. Here’s how you do it (hat tip to this gist by bocrisy1):

#To install ruby-debug on Ubuntu ruby-1.9.3 you need to download from http://rubyforge.org/frs/?group_id=8883

linecache19-0.5.13.gem

ruby_core_source-0.1.5.gem

ruby-debug19-0.11.6.gem

ruby-debug-base19-0.11.26.gem

Then in your console

export RVM_SRC=/your/path/to/ruby-1.9.3

# Note, your source path should be something like /home/user/.rvm/src/ruby-1.9.3-p0

gem install archive-tar-minitar

gem install ruby_core_source-0.1.5.gem --with-ruby-include=$RVM_SRC

gem install linecache19-0.5.13.gem --with-ruby-include=$RVM_SRC

gem install ruby-debug-base19-0.11.26.gem --with-ruby-include=$RVM_SRC

gem install ruby-debug19-0.11.6.gem --with-ruby-include=$RVM_SRC

Now that we have our gems installed via rvm, it’s time to do some basic configuration/installation to get them to work with our rails application.

Install RSpec

To use RSpec instead of Test::Unit, do:

rails generate rspec:install

Install Guard

Michael Hartl talks about how to do install Guard in the Rails tutorial. (available for free online)

At the command prompt, type:

bundle exec guard init rspec

Then initialize Guard so that it works with RSpec. Open app/Guardfile in your editor and add the following (also delete the autogenerated code in the Guardfile you see or comment it out):

##Modified 12/5/12 using Rails Tutorial (3.2), Listing 3.34
guard 'rspec', :version => 2, :all_after_pass => false do
  watch(%r{^spec/.+\_spec.rb$})&lt;br />
  watch(%r{^lib/(.+).rb$}) { |m| "spec/lib/#{m[1]}\_spec.rb" }
  watch('spec/spec_helper.rb') { "spec" }</code>

  # 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' }

  ##Added 12/5/12 using Rails Tutorial (3.2), Listing 3.34

  watch (%r{^app/controllers/(.+)\_(controller).rb\$}) do |m|
    [(m[1\]\[/\_pages/\] ? 'spec/requests/#{m[1]}\_spec.rb' : 'spec/requests/#{m[1].singularize}\_pages_spec.rb')]
  end

  watch(%r{^app/views/(.+)/}) do |m|
    (m\[1\]\[/\_pages/\] ? 'spec/requests/#{m[1]}\_spec.rb' :
    'spec/requests/#{m[1].singularize}\_pages_spec.rb')
  end

  # Capybara features specs

  watch(%r{^app/views/(.+)/.\*.(erb|haml)\$}) { |m| 'spec/features/#{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

Add require ‘active_support/core_ext’ to the top of the Guardfile.

Note: I left in some of the sample Guardfile stuff because it watches the config/routes.rb file, which Hartl’s current tutorial does not seem to do.

Install Spork to Speed Up Tests

At the command prompt, type:

bundle exec spork --bootstrap

Final Gemfile listing

source 'https://rubygems.org'

gem 'rails', '~>3.2.9'

# Bundle edge Rails instead:

# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'

# Gems used only for assets and not required

# in production environments by default.

group :assets do
  gem 'sass-rails', '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'
gem 'execjs'
gem 'libv8', '~> 3.11.8'
gem 'therubyracer' #needed for rspec install
gem 'devise', '~> 2.1.2'

group :test, :development do
  gem 'annotate', '~> 2.4.1.beta'
  gem 'faker','~>1.0.1&#8242;
  gem 'rspec-rails', '>= 2.6.1'
  gem 'autotest'
  gem 'autotest-rails-pure'
  gem 'factory_girl_rails', '~>1.6.0' #higher version than in Rails tutorial
  gem 'guard-rspec', '~>2.3.0'
  gem 'guard-spork', '~>1.2.0' #higher version than in Rails tutorial
  gem 'spork', '~>0.9.2'

  #Added 2/14/12 Tues for debugging

  gem 'linecache19', '0.5.13'
  gem 'ruby-debug-base19', '0.11.26'
  gem 'ruby-debug19', require: 'ruby-debug'
end

group :test do
  gem 'capybara', '~>1.1.2' #used in Rails 3.2.1 tutorial

  #Linux system dependent gems for use with guard, Rails 3.2 tutorial
  gem 'rb-inotify', '0.8.8'
  gem 'libnotify', '0.5.9'
  gem 'cucumber-rails', '1.2.1', :require=>false
  gem 'database_cleaner', '0.7.0'
end

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'debugger'

Sidebar: Using Git

We’ll be using a version control system called Git throughout this tutorial. Use of this is optional, but it is recommended you learn Git at some point since that is currently what the Ruby on Rails world uses to manage source code.

git init .
git add .
git commit -m "initial setup and installation"

Resources:

I’m keeping a public repo of this application at my github account. You can download the source code there.

Sources:
  1. https://gist.github.com/1331533

  2. http://railsapps.github.com/installing-rails.html


Profile picture

Written by Bruce Park who lives and works in the USA building useful things. He is sometimes around on Twitter.