This tutorial updates the POD Demo from the Hobo screencasts to Hobo 0.6. It also demonstrates how to use Hobo as a plugin as well as a gem. For more information on Hobo, see Hobo.

Introduction

You can choose whether you want to run Hobo as a gem or a plugin. Even though it takes fewer steps to use the gem, I prefer the plugin since it runs on the latest svn. No need to wait for the Hobo team to release a gem.

Please edit this tutorial if you see something that can be improved! Don't worry -- if I don't like your changes I'll just revert them. :)

Create The Project

Start the screencast. Tom first executes these three commands:

 (don't run these)
 $ sudo gem install hobo
 $ hobo pod
 $ mysqladmin create pod_development

Instead of doing that, follow these 6 easy steps.

1. Create the Database

 $ mysqladmin create pod_development

2. (Option 1) Create project with Hobo gem:

If you want to use Hobo as a Gem, just do what Tom does. Notice that the gem is now uploaded to RubyForge; there's no need to download it separately the way Tom does in the screencast.

 $ sudo gem install hobo
 $ hobo pod
 $ cd pod

>> Now skip to Start Using Hobo >>.

2. (Option 2) Create project with generate-hobo-plugin:

I've written a script that will create an Edge Rails environment with the Hobo plugin installed and ready to go. It downloads Rails -- no need to have it installed.

Right-click this link and choose Save As: http://u32.net/junq/generate-hobo-plugin

 $ chmod a+x generate-hobo-plugin
 $ ./generate-hobo-plugin pod

>> Now skip to Start Using Hobo >>.

2. (Option 3) Create project with Hobo plugin manually:

Doing everything manually can be a good learning experience. This is basically what the generate-hobo-plugin script does:

note: I did it this way and things broke. The login and signup actions disappeared for some reason, I suggest using the 'hobo' script instead (as Tom does in the screencast 'hobo pod'), you will still have to edit in routes to get the login and signup links on the top of the page but at least the actions will be there behind them. Info on getting the routes fixed is here... [1]

bjn>>

Install Edge Rails

 $ mkdir -p pod/vendor
 $ cd pod
 $ svn co http://dev.rubyonrails.org/svn/rails/trunk vendor/rails
 $ ruby vendor/rails/railties/bin/rails .

Install the Hobo Plugin

 $ script/plugin install svn://hobocentral.net/hobo/trunk

If the trunk is broken and you want to use a branch, do this instead:

  $ script/plugin install svn://hobocentral.net/hobo/branches/tom_sandbox
  $ mv vendor/plugins/tom_sandbox vendor/plugins/hobo

Hobo 0.6 still uses classic pagination. Hopefully soon it will switch to the will_paginate plugin (check the status page).

 $ script/plugin install svn://errtheblog.com/svn/plugins/classic_pagination

Run the Hobo Generators

Now, open the Generators view and run:

 $ script/generate hobo --add-routes
 $ script/generate hobo_rapid --import-tags
 $ script/generate hobo_user_model user
 $ script/generate hobo_front_controller front --add-routes --delete-index
 $ script/generate hobo_model_controller user

If any of these methods bomb out, it's likely that trunk is broken and you'll need to try a different branch.

You're probably not using Aptana but, if you are, this is how you execute the first command:

<img src="./Hobo-generate.png" alt="Hobo-generate.png" />

Now the infrastructure should be all set up! Let's continue with the screencast >>.

Start Using Hobo

Now that the project has been set up, we can actually start using the Hobo framework.

3. Run the Generators

Now, let's run some generators as Tom shows.

  $ script/generate hobo_model advert
  $ script/generate hobo_model category
  $ script/generate hobo_model_controller advert
  $ script/generate hobo_model_controller category

4. Declare Your Columns

There's no need to perform manual migrations anymore! Hit pause and read through this section, Then hit play and ignore everything Tom says about migrations. :)

The data model used to be defined in the migration scripts. Hobo moves this to where it should be: directly inside your models! If you declare your columns inside your models, Hobo will generate the migration scripts for you.

Add to app/models/advert.rb:

 fields do
   title       :string
   body        :text
   timestamps
 end

Add to app/models/category.rb:

 fields do
   name :string
 end

5. Edit Your Models

Just do exactly what Tom does in the screencast.

For app/models/user.rb:

  • Add "has_many :adverts"
  • Uncomment "# login == 'admin'". The 'admin' user is not subject to permission checks.
  • Add "creator.guest?" to creatable_by? so that only guests can create users.
  • Add "updater == self" to updatable_by? so you can only update yourself
  • Add "deleter == self" to deletable_by? so you can only delete yourself

For app/models/advert.rb:

  • Add "belongs_to :user"
  • Add "belongs_to :category"
  • Add "set_creator_attr :user". This tells Hobo to use the user column as the creator attribute. (what is the creator attribute?)
  • Add "creator == user and not creator.guest?" to creatable_by? so an advert cannot be created by a guest or any other user. You must create your own advert. NOTE: if the single argument to this method is called 'user', change it to 'creator'.
  • Add "updater == user and same_fields?(new, :user)" to updatable_by? so that your advert can only be edited by you, yet you cannot change the advert's user to somebody else. Change 'user' to 'updater'.
  • Add "deleter == user" to deletable_by? Change 'user' to 'deleter'.

For app/models/category.rb:

  • Add "has_many :adverts". Only the administrator can create/update/delete categories but anyone can view them.

6. Create The Migration

Now we'll update the database to match our models.

 $ script/generate hobo_migration create_initial_tables

Tell Hobo to both create and perform the migration.

And we're done! Stop the clock.

Try it Out

Start the Server

Fire up the application.

Regular: run $ script/server --port=3002
Aptana: click on the Servers tab, select podServer and hit the play button.

Then browse to http://localhost:3002.

If you get a "Cookie Violation" error, that's no big deal. It's just Edge Rails being safer than sorry. Hit shift-reload.

Use The New Application

(just following Tom's interactions from the screencast)

  • Sign up as user=admin, password=whatever
  • BUG: notice how it says "Create a Adverts" instead of "Create a New Advert"? This didn't happen in 0.4.
  • Create the Cars and Computers categories.
  • Log out and sign up as new user "tom"
  • Notice how you can't create a category but you can create an advert.
  • Create an advert. Title="MacBook Pro!", Body="Nice and shiny"
  • Try updating all the fields on the advert. Even though this is far too much ajax, notice that 0.6 behaves just like the demo.
  • Check that the search is working. Search for 'shiny'. BUG: this doesn't work.

TODO

  • What's with the cosmetic "Create a Adverts" bug?
  • Search is broken -- it just keeps recursively loading the search page.
  • All the lines in "Run the Hobo Generators" are absurd. Isn't there something like script/generate hobo_all?

Edit History

  • Add step numbers 1-6
  • Split the Aptana information into Aptana.
  • Now the tutorial should apply to both gem and plugin.
  • Use Edge Rails directly rather than starting from the Rails gem.
  • Show how to handle migrations in 0.6.