Yesterday we released Cells 3.5.0, which is another step towards real component-orientation in Rails. The release party was a big hit.

hairbolzheimer_rocknacht_iii_189

Cells is the one and only view components framework for Rails. It has a vivid growing community and had almost 10,000 downloads in the last year. That’s cool.

What’s new?

Basically we threw out a lot of code and deprecated some mechanics – we’re really happy to have abandoned the last ugly spots in Cells.

  • First of all, I introduced a CHANGES file which keeps growing. Keep that in sight if you wanna stay up-to-date about improvements and fixes.
  • The generator now uses the standard hooks for templates and tests.
  • States now receive arguments (aka state-args) to use less instance variables.
  • The rspec-cells gem finally runs and even has Capybara support.

The Generator

Thanks to the great work of Jorge Calás the cells generator now works like all the other generators in Rails 3. What has been the cells:cell generator is now simply cell.

It has hooks for templating and testing. Cells ships with generators for ERB and Haml views and Test-Unit and Rspec test cases.

$ rails g cell ShoppingCart display -e haml -t rspec
    create  app/cells/shopping_cart_cell.rb
    invoke  haml
    create    app/cells/shopping_cart/display.html.haml
    invoke  rspec
    create    spec/cells/shopping_cart_cell_spec.rb

Love it already.

State-args are the new options

You usually pass parameters into cells to model encapsulation.

render_cell :shopping_cart, :display, 
  :user => current_user, 
  :items => current_user.items

This drastically reduces coupling. The outer world (e.g. the controller) knows about dependencies whereas the cell should not care about retrieving its domain models.

In former versions, you accessed those arguments by querying the @opts instance variable. Ugh! Don’t do this anymore. The prefered way now is state-args.

class ShoppingCartCell < Cell::Base
  def display(args)
    @items = args[:items]

The options are passed as method arguments, and that’s what arguments were made for!

If you don’t like that, you can still use the #options method in your states.

  def display
    @items = options[:items]

This is for a soft transition, but be warned that Cells 4.0 will not store arguments in instance variables anymore. Don’t overuse instance variables – this typically is a smell of improper method chaining design.

States are helpers

Complex helpers typically invoke some code and then render a partial. This is exactly what render :state in cells does.

  - @items.each_with_index do |item, i|
    render({:state => :item_link}, item, i)

You typically do this in cell views. Notice how I pass arguments into the state.

class ShoppingCartCell < Cell::Base
  # ...
  def item_link(item, i)
    link_to item, item.url,
      :class => i.odd? ? :odd : :even

The #item_link state acts as a helper method, but as opposed to a plain helper you can use inheritance here and all the other benefits real object instances bring.

rspec-cells enhancements

If you like testing (you should love it!) you can use either traditional unit tests or RSpec. Include the rspec-cells gem into your Gemfile.

gem "rspec-cells"

The packaged generator will stub out a useful spec template for you.

$ rails g cell ShoppingCart display -t rspec

You can use webrat’s matchers, or even the new capybara string matchers, if you have capybara installed.

context "rendering display" do
  subject { render_cell(:shopping_cart, :display) }
 
  it { should have_selector("h1", :text => "The Cart") }
end

Get Cells!

We’re happy these improvements are on the road – tell us what you think about it! I’ll try to recover from the release party, now.

Tags:

4 Responses to “Cells 3.5 Release Party Summary”


  1. DarkGlass

    Seems like I picked a time to start using Cells – sounds like a cool update.

    The state-args introduction I especially like.

  2. Congrats!
    i cannot wait to tray it.

  3. Thanks. Cant wait to use it. Finishing ecommerce site, and ill refactor it in.

    Great stuff


  4. Luke

    Exciting stuff. Looking forward to trying out some of the new functionality.

Leave a Reply