Synthetize a Blog in 15 seconds with a rails template

Ryan Bates, who is best known for his amazing free screencasts series, has recently updated a famous screencast where Ruby on Rails is used for building a web blog application in 15 minutes only. The original screencast, recorded by David Heinemer Hansson during a live coding session, popularized the combined power of Ruby language and Rails framework, showing how to produce working software under a such extremely short time constraint and, above all, without traces of unneeded complexity.

In the new screencast, Ryan has been able to pack up basic http authentication for administrative actions, AJAX, XML atom feed generation, elements of troubleshooting with ruby-debug and TDD. Is that impressive, or what?

That said, I am here to give an example of how a template fits the story and how it can let you slash that record. First, what a template is? Where does it come from? A template is simply a ruby file containing some commands for automatize programmatically some repetitive activities that any rails programmer usually takes after the generation of a new web application. Templates have been integrated into the Rails core since version 2.3 (that is edge at the time of writing this).

Before templates the smart rails programmer took the way of the so called starter app to have a skeleton for commonly used plugins and gems. Among the most popular there are:

Very useful indeed, but not quite flexible since you still have to manage the burden of mixin’ and matchin’ any different customized setup.

So for maximum flexibity Jeremy McAnally of entp came up with rg, that eventually went into the rails core.

Suprails from Bradley Grzesiak is another project of the same kind that I would like to mention, because it shared analogous philosophy of rg.

Anyway, a template offers fine control over the carried actions. For example, let’s say that you would like to automatize the simple nevertheless repetitive process of git initialization for a new repository. We can achieve that with the following git-init template:

# git-init.template

# create a git ignore file
file ".gitignore", <<-GITIGNORE
.DS_Store
log/*.log
tmp/**/*
vendor/rails
config/database.yml
db/*.sqlite3
GITIGNORE

# create void .gitignore files to track empty directories
`touch log/.gitignore tmp/.gitignore vendor/.gitignore`

# perform the first commit
git :init
git :add => "."
git :commit => "-a -m 'initial import'"

You then run:

$ rails blog
$ cd blog
$ rake rails:freeze:edge
$ rake rails:template LOCATION=../git-init.template

And you should see something like that:

(in /Users/miky/code/blog)
    applying  template: ../git-init.template
        file  .gitignore
     running  git init
     running  git add .
     running  git commit -a -m 'initial import'
     applied  ../git-init.template

Now after my somewhat wordily introduction, it’s the time for finding a good reason for the title here.

So download a rails template that I’ve obtained annotating Ryan’s screencast, place it in the proper directory, start the count down and:

  1. run
    $ rake rails:template LOCATION=../blog.template
    
  2. wait until finished
  3. run
    $ script/server
    

    Point your browser to http://localhost:3000/posts and enjoy!

Look at the template to see how it is done. If you would like to learn available commands, I advice you to read a very informative post from Pratik or the one from Jeremy itself.

Thank you for your attention and I hope you’ve found something useful.