Introduction

Know how you can’t just use ‘form_for’ in rails with a newly initialized ActiveResource object? Ever get annoyed at having to add default attributes to your newly initialized ActiveResource objects? Fortify gives you the power to DRY’ly define default attributes for your ActiveResource derived classes.

Motivation

Let’s suppose you’re going to create a form for creating Books. However, your Book is an ActiveResource::Base derived class:

# app/models/book.rb
class Book < ActiveResource::Base
end

# app/controllers/books_controller.rb
class BooksController < ActiveResource::Base
  def new
    @book = Book.new
  end
end

# app/view/books/new.haml
- form_for @book do |f|
  .field
    = f.label :title
    = f.text_field :title

  .field
    = f.label :author
    = f.text_field :author

  .field
    = f.label :genre
    = f.text_field :genre

This, of course fails. Why? When you create a new Book object, unlike ActiveRecord (which has the luxury of querying the database to determine the object’s attributes), ActiveResource starts off as a blank slate. In other words:

console> b = Book.new
console> b.attributes.inspect
  ==> {}

To get this to work, you would have to give your new Book object some default attributes:

# app/controllers/books_controller.rb
class BooksController < ActiveResource::Base
  def new
    @book = Book.new :title => nil, :author => nil, :genre => nil
  end
end

As you can imagine, this approach might not be very DRY. This is where fortify steps in.

Usage

First, install fortify by adding the following line to your config/environment.rb and then running “sudo rake gems:install”:

config.gem :fortify, :lib => 'fortify', :source => 'http://gemcutter.org'

Next, go back to your book model and add some fortification to it. You can do this in one of three ways. Let’s first assume that we just want to make sure the Book model has “author”, “title”, and “genre” attributes.

# app/models/book.rb
class Book < ActiveResource::Base
  self.site = ''
  fortify :author, :title, :genre
end

Now, your controller / views as you originally specified them will work, since now:

console> b = Book.new
console> b.attributes.inspect
  ==> {:author => nil, :title => nil, :genre => nil}

What if, however, we wanted default values for each of those attributes?

# app/models/book.rb
class Book < ActiveResource::Base
  self.site = ''
  fortify :author => 'Anonymous', :genre => 'Unknown', :title => 'Untitled'
end

In this case, a Book.new would give you:

console> b = Book.new
console> b.attributes.inspect
  ==> {:author => 'Anonymous', :title => 'Untitled', :genre => 'Unknown'}

Lastly, what if we only wanted a default value for the :author attribute? Try this:

# app/models/book.rb
class Book < ActiveResource::Base
  self.site = ''
  fortify do |attrs|
    attrs.author = 'Anonymous'
    attrs.title
    attrs.genre
  end
end

Console would report something like this:

console> b = Book.new
console> b.attributes.inspect
  ==> {:author => 'Anonymous', :title => nil, :genre => nil}