When to use Cells- and when to use Apotomo

If you ever found yourself in the situation where you wished a Rails controller action could “contain” some other action, or a view could call and render actions, you were desperate.

So you were looking through the docs, searching for something like

class DesktopController < ApplicationController
  def dashboard
    @stats = render :controller => 'statistics', :action => 'overview_box'
    @links = render :controller => 'navigation', :action => 'quick_links'
    # ...
  end
end

You were in a familiar situation, where you encountered Rails’ paucity of components.

However, Rails wouldn’t be Rails if there didn’t exist a plugin already filling this gap. That’s what Cells does. It introduces something new, which looks and feels exactly like a controller, but is superfast and lightweight. That’s a cell.

Quick- how would that look with cells?

You would first put your embedd’able stuff into cells. This really looks like a Rails controller.

# file: app/cells/statistics_cell.rb
 
class StatisticsCell < Cell::Base
  def overview_box
    @rows = Statistic.find_overview_rows_for(Time.now)
    nil
  end
end

You want to render a view for that cell “action”. That’s something like

# file: app/cells/statistics/overview_box.html.erb
 
<h3>Hot stats!</h3>
<% @rows.each do |row| %>
  <%= h row.to_html %>
<% end %>

Finally, rendering the cell in a controller would be

class DesktopController < ApplicationController
  def dashboard
    @stats = render_cell(:statistics, :overview_box)
  end

That is what you were looking for, isn’t it?

So, use Cells whenever…

  • you want to use a “controller action” in several places in your app, or even in different apps
  • you’re creating a page consisting of building blocks, like a dashboard
  • a partial is too dumb, and you need object-oriented abstraction
  • a helper method is smart enough, but could need a view template

Cells are like controllers, so they even can use helpers or contain forms. That’s cool, that’s somehow like a widget, doing whatever it is supposed to do but remaining independent from the outer world, the page.

Speaking of widgets, we get to Apotomo. This plugin is build on top of cells and provides real stateful widgets, with cells. These widgets look like cells, but are persistent state-machines which are listening to events and know when and how to update itself on the page.

And… when to use Apotomo over cells?

Scratching the surface, you should write a real widget instead of a cell as soon as you want to

  • write an interactive cell
  • update a cell via AJAX if something happens, like a form submission
  • update multiple cells via AJAX at the same time
  • manage nested cells - like a tree of cells

Nah- that’s not the end! Apotomo can do much more for you and your clean, fast application. But reading the title of this post, I think it’s enough for today. Now go and try it.

And, hey! Did I mention that both Cells and Apotomo run fine with Rails 2.1 and 2.2? The 2.3 port is on the way, promised.

One Response to “When to use Cells- and when to use Apotomo”

  1. links for 2009-03-30 « Brent Sordyl’s Blog Says:

    [...] Rails - when to use Cells- and when to use Apotomo Cells are like controllers, so they even can use helpers or contain forms. That’s cool, that’s somehow like a widget, doing whatever it is supposed to do but remaining independent from the outer world, the page. (tags: rubyonrails) Possibly related posts: (automatically generated)Tag Game # 2SEO Page Titles and Meta Tags are Alive and WellBrand TagsViolet brings Mir:ror to the States, let the RFID superfluity begin! [...]

Leave a Reply