HoboFields and Rails 3

Posted by Tom on 2010-08-24

Update: I should of known better than to post this late at night. Now improved with actual working instructions!

Folks, thanks to Domizio’s great work, HoboFields is ready to try out with Rails 3. Here’s a quick guide to getting it installed and running the migration generator. You’ll need git to grab the latest code, as nothing has been released as a gem, but apart from that you won’t have to do anything too technical. Please note, Rails 3 RC2 just dropped today, but right now we’re still on RC1, so please make sure you have that version of Rails installed if you want to try this out.

OK, let’s grab the code:

git clone http://github.com/tablatom/hobo.git

The Rails 3 work is happening on the cunningly named rails3 branch, so you now need to

cd hobo
git checkout origin/rails3

Next, we use Rake to generate and install the gems for HoboSupport and HoboFields. Note that this step requires the jeweler gem, so you have first have to:

gem install jeweler

Once you’ve got jeweler installed:

cd hobo_support
rake install
cd ../hobo_fields
rake install

That’s it! We’re ready to try a Rails 3 app with HoboFields. Assuming you have Rails 3 installed, use the new syntax for the rails command to create a new app:

cd somewhere/for/your/test/app
rails new test_hobofields
cd test_hobofields

Now, the way to add a gem to a Rails 3 app, is to add the gem to Bundler’s Gemfile. Edit that file, and add this line, at the end:

gem 'hobo_fields', '1.3.0.pre1'

If you now run

rails generate

You should see two Hobo generators listed in the available generators. Let’s create a Hobo model (note the nice new namespaced generator syntax).

rails generate hobo:model book name:string description:text

You can see the fields block has been added to the normal model template if you look at app/models/book.rb:

class Book < ActiveRecord::Base

  fields do
    name        :string
    description :text
  end

end

Now, let’s watch HoboFields work its magic and migrate the database for us.

rails generate hobo:migration

At the prompt, choose “m”, then give the migration a name. If everything is going according to plan, the local SQLite development database should be migrated to include the books table. We can check at the SQLite prompt:

rails dbconsole

Then at the SQLite prompt:

.schema

You should see

CREATE TABLE "books" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "description" text);
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");

Score! And did you notice? Domizio even gave us a colored prompt in the migration generator. Too much : )



(edit)