Filters For Your Cells with cells-filters

{{{
There’s nothing like giving a quick update about “Cells”:https://github.com/apotonick/cells on a rainy sunday evening. Almost a year ago we were happy “to introduce state-args into Cells”:http://nicksda.apotomo.de/2011/02/cells-3-5-release-party-summary/. State-args help to prevent saving state in instance variables and instead pass variables from @#render_cell@ as arguments, making your code more explicit.

One big problem was that filters couldn’t take advantage of that feature, thanks to a design flaw in Rails’ callback implementation.

h3. Example?

Consider the following call to render a freaky bassist cell.

render_cell :bassist, :play, "C-sharp", "4/4"

The cell class wants to process the incoming data in a @before_filter@.

class BassistCell < Cell::Rails
  include Cell::Filters

  before_filter :prepare

  def prepare(state, tone, timing)
    @before = "In #{tone} and #{timing}"
  end

What now works with the new “cells-filters”:https://github.com/apotonick/cells-filters gem is that filters (here, a before filter) can receive the state-args. The implementation is very simple using the “hooks gem”:https://github.com/apotonick/hooks for easy peasy hooks and callbacks in Ruby.

However, we introduce a dependency to another gem, that’s why cells-filters was released as a separate gem. Go and try it! We might need to add some more behaviour to it, but please don’t ask me to completely bloat it.

You can still use the old-school filters from the Rails @Callbacks@ module but you won’t be able to retrieve state-args. I don’t feel like fixing this in the Rails core itself as “the implementation is a bit too complex”:https://github.com/rails/rails/blob/3-2-stable/activesupport/lib/active_support/callbacks.rb#L145 for me.

h3. Cells Development and a Roadmap.

Dudes, Cells is getting more and more of a standard in Rails development. In the last year, 2011, we had almost “90.000 downloads”:http://gemcutter.org/gems/cells – this is awesome! Thank you so much.

We have more plans with Cells for 2012, here’s a short summary what we’re planning.

* Get rid of the Rails dependency at all. We “already got @Cell::Base@ to use cells without a controller”:http://nicksda.apotomo.de/2011/12/mounting-a-cell-to-a-route-with-cells-3-8/. Now we want to go one step further and make cells completely decoupled from Rails. Imagine cells (and, numerous “helper” gems like formtastic) being renderable in Sinatra or without a framework at all.
* Use “tilt”:https://github.com/rtomayko/tilt as an alternative rendering engine. The Rails rendering layer is outdated and should use an abstracting rendering gem like tilt – until we can achieve this in the core, we will provide this in cells. That’ll make huge parts of cells completely independent from Rails.
* Screencasts! Yeah, I’m working on a screencast series about cells, best practices and interesting solutions users sent in over the recent years.

Beside that, we’re permanently trying to incorporate the cells architecture into the Rails core. Let’s see where this will lead to in Rails 4.0. Happy new year, and… Cheers!
}}}

10 thoughts on “Filters For Your Cells with cells-filters

  1. I also use cells! In some projects just as a replacement for complex helpers, and in some others I use it to let clients insert bits of non-trival functionality via a WYSIWYG editor in the CMS. Cells are awesome!

    Like

  2. I’m using cells in production, also apotomo to make the ajaxy stuff easier — both help make my view code DRY.

    Like

  3. We use Cells in production as key component for Railsyard CMS (www.railsyardcms.org).

    BTW is there a way in filters to access to the params passed as symbols like :user in the following example?

    @current_user %>

    That would be backward compatible with current cells implementation

    Like

  4. Sorry the example code has been escaped in the comment, I’m talking about the way we currently pass arguments to a cell like:

    render_cell :cart, :display, :user => @current_user

    Like

Leave a comment