Using Toffee in your Rails application
This How-to applies to: Any version.
For the benefit of this guide, I will be using a very very simple pretend application, containing two tables, people and places. People belong to places, and places have many people etc. This will assume that you have already followed the Installation instructions, and the toffee plugin is installed in vendor/plugins.
At the moment, the application contains hardly any code. As well as the standard Rails files that are created when the application is generated, there are two model files, person.rb and place.rb and one controller home_controller.rb.
First of all we need to enable our models so they can be used with Toffee, as below:
class Person < ActiveRecord::BaseSo basically all we've done is add "enable_toffee".
enable_toffee
belongs_to :place
end
Now we want to link to our newly enabled admin screen, so lets look at the standard layout file we're using in "layouts/application.rhtml". At the moment this contains:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">This produces the following in the browser, attractive eh!
<html>
<head>
<%= javascript_include_tag :defaults %>
<%= setup_toffee_includes %>
</head>
<body>
<%= yield %>
</body>
</html>

So we basically want a header with some links to our Toffee admin pages, to do this add the links as below:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">The links will appear above the welcome text, then clicking on "People" will bring up the following screen.
<html>
<head>
<%= javascript_include_tag :defaults %>
<%= setup_toffee_includes %>
</head>
<body>
<div>
<%= link_to "People", :controller => "toffee", :model => "person" %> |
<%= link_to "Places", :controller => "toffee", :model => "place" %>
</div>
<%= yield %>
</body>
</html>

By default it takes you to the list screen, but at the moment we don't have any people in our database. So I'll add one by clicking on "Add Person"...

As you can see it has generated a form based on the fields belonging to the model, it has also created a drop-down box listing the places, however the options don't make much sense, this is because by default it will show the results of the to_s method against the Place object. So lets add a to_s method in place.rb.
class Place < ActiveRecord::BaseNow lets refresh that form...
enable_toffee
has_many :people
def to_s
"#{name}, #{county}"
end
end

So you can see now it makes sense. I'll add a couple of people now and return to the list...

So now we have two people, however you'll notice that the place is showing an integer value, this is because by default it just shows the columns that belong to the people table, and this includes "place_id". This can of course be changed, if you add the following line in "person.rb":
@toffee_list_columns = ["first_name","last_name","date_of_birth","place.name"]Then refresh the screen and it will look like...

A new feature in Toffee is Filters, allowing users to filter the results in the list screen. For this example I've added some more people into the database. Now, by adding the following line to place.rb...
@toffee_list_filters = ["place_id", "first_name"]
You will get the following box added to the list screen...

And in the screen above, I've already filtered the list by Place (notice that it gives a drop-down automatically when its a foreign-key).
This should give you enough information to get started with Toffee.