New Rails App - 2012
Intro
I write one of these every couple of years, just to have a reference of things I like to have in a basic rails app. Anyone else reading this should probably look at http://railsapps.github.com/ or something similar.
Assumptions
- git installed
- rvm installed
- ruby 1.9.3 installed for rvm
- numerous alias for git
ga git-add
gca 'git commit -a
- you will do Git commits for each action
Start with a folder and rvm
➥ mkdir newapp
➥ cd newapp
[andy@airy-deux ~/newapp] -♢
➥ git init
Initialized empty Git repository in /Users/andy/newapp/.git/
[andy@airy-deux ~/newapp] (master) -♢
➥ echo > .rvmrc 'rvm use --create 1.9.3@newapp > /dev/null'
➥ pushd ..
➥ popd
~/newapp
==============================================================================
= NOTICE =
==============================================================================
= RVM has encountered a new or modified .rvmrc file in the current directory =
= This is a shell script and therefore may contain any shell commands. =
= =
= Examine the contents of this file carefully to be sure the contents are =
= safe before trusting it! ( Choose v[iew] below to view the contents ) =
==============================================================================
Do you wish to trust this .rvmrc file? (/Users/andy/newapp/.rvmrc)
y[es], n[o], v[iew], c[ancel]> yes
[andy@airy-deux ~/newapp] (master) @newapp-♢
Basic Gemfile so we can install rails in our gemset
Create a Gemfile that looks like:
source :rubygems
gem "rails"
Now install rails using bundler
➥ bundle
Fetching gem metadata from http://rubygems.org/.........
...
Installing rails (3.2.8)
Your bundle is complete! ...
and create your rails application (using -T because we will use rspce and -f because we want to overwrite our Gemfile
➥ rails new . -T -f
This definitely deserves a git commit,
➥ ga .
[andy@airy-deux ~/newapp] (master) @newapp-♢
➥ gca -m 'ran "rails new . -T -f'
Test tools
Add the following to our Gemfile
group :test do
gem 'coderay'
gem 'debugger'
gem 'cucumber-rails'
gem 'rspec-rails'
gem 'database_cleaner'
end
After another bundle command we can run the generators for cucumber and rspec, doing a commit for each one.
➥ rails g rspec:install
create .rspec
create spec
create spec/spec_helper.rb
➥ ga .
➥ gca -m "ran 'rails g rspec:install'"
[master 6877eb7] ran 'rails g rspec:install'
2 files changed, 39 insertions(+)
create mode 100644 .rspec
create mode 100644 spec/spec_helper.rb
and now for cucumber
➥ rails g cucumber:install
...
➥ ga .
➥ gca -m "ran 'rails g cucumber:install'"
[master 93477a8] ran 'rails g cucumber:install'
5 files changed, 146 insertions(+), 1 deletion(-)
create mode 100644 config/cucumber.yml
create mode 100644 features/support/env.rb
create mode 100644 lib/tasks/cucumber.rake
create mode 100755 script/cucumber
Its most important to be able to debug our features (and specs), to help with this I’ll add some step definitions for debugging that also use coderay to show pages in the console
Make a file features/step_definitions/debub_steps.rb
with content
require 'coderay'
require 'ruby-debug'
module DebugStepHelper
def peek
output = CodeRay.scan(page.body, :html).term
print '-- peeking at body'
print output
print '-- end peeking at body'
end
end
World(DebugStepHelper)
Then 'peek' do
peek_body
end
Then /^pending (.+)/ do |msg|
pending msg
end
Then "debug" do
debugger
1
end
Now create a feature to exercise this:
# features/home.feature
Feature: Visit site
Scenario: Visit the site
When I visit the site
Then debug
and some steps for this:
# features/step_definitions/home_steps.rb
When /^I visit the site$/ do
visit '/'
end
Now run your features and you should stop in the debugger. Using peek we can see the source for rails default homepage formatted by coderay
➥ c
Using the default profile...
Feature: Visit site
Scenario: Visit the site # features/home.feature:3
When I visit the site # features/step_definitions/home_steps.rb:1
...
(rdb:1) peek
-- peeking at body<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails: Welcome aboard</title>
...
Template tools
I like haml so add following to Gemfile then run bundler.
gem "haml-rails", "~> 0.3.4"
If by now you’re thinking this is a hell of alot of work just to start a new rails application, well I agree. This is why there are so many rails templating tools out there. However we shouldn’t forget just how much we’ve got from this setup.
Markdown Handler
I love markdown and I want my rails apps to work with markdown templates.
create a markdown handler
#lib/markdown_handler.rb
require 'rdiscount'
module MarkdownHandler
def self.erb
@erb ||= ActionView::Template.registered_template_handler(:erb)
end
def self.call(template)
compiled_source = erb.call(template)
"RDiscount.new(begin;#{compiled_source};end).to_html"
end
end
ActionView::Template.register_template_handler :md, MarkdownHandler
now register the handler using an initializer
#config/initializers/markdown_templates.rb
require 'markdown_handler'
finally add the rdiscount gem to the Gemfile
gem 'rdiscount'
Now you can create .html.md
containing Markdown, which will be rendered
by RDiscount into html.