Personal tools
You are here: Home Software Toffee Usage How to integrate Toffee into your Rails application Using Toffee in your Rails application
Document Actions

Using Toffee in your Rails application

This How-to applies to: Any version.

Detailed instructions showing the capabilities of Toffee

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::Base
  enable_toffee
  belongs_to :place
end
So basically all we've done is add "enable_toffee".

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">
<html>
<head>
<%= javascript_include_tag :defaults %>
<%= setup_toffee_includes %>
</head>
<body>
<%= yield %>
</body>
</html>
This produces the following in the browser, attractive eh!

Picture 1.png

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">
<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>
The links will appear above the welcome text, then clicking on "People" will bring up the following screen.

Picture 2.png

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"...

Picture 3.png

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::Base
  enable_toffee
  has_many :people

  def to_s
    "#{name}, #{county}"
  end
end
Now lets refresh that form...

Picture 4.png

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

Picture 5.png

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...

Picture 6.png

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...

filters.png

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.





by dfitzgibbon last modified 2007-04-30 15:39

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: