Cells 3.7 released, taataa.

Is it a good sign when a minor release doesn’t yield any stunning new features? When there’s nothing breathtaking to talk about? I guess with Cells it is – the gem finally seems to get into what it should have been 6 years ago: a mature view components framework for Rails.

The “3.7 release”:https://github.com/apotonick/cells/commit/0c0aeedd3d05de571d8265bc2d3bc2582c370a53, again, lost code – it’s goal was *getting rid of the @#options@ hang-over* which succeeded. No more state in your cell, except if _you_ choose to do so.

Here’s what changed.

h3. Rails 3.x

Well, needless to say that 3.7 still runs with Rails 3.0 and 3.1. We were able to incorporate a “couple of changes into Rails 3.1”:https://github.com/rails/rails/commit/cbaad674f13067c52fa8c1a24dc498e570db4eed to make Cells’ life even easier. Thanks, José for being such a patient maintainer and happy birthday to you 😉

h3. Caching Inheritance and Conditionals

“Arthur Gunn brought us the @:if@ option”:https://github.com/apotonick/cells/commit/30aff3772ccde884468778f0975cbed2f08f5883 for caching. So, if you ever needed conditional caching in your cell, here it is.

class CommentsCell  proc { |cell, opts| opts[:enable_cache] }

To pass in the required options, I’d use @#render_cell@.

render_cell(:comments, :show, :enable_cache => false)

See how easy it is to bypass (or enable) caching using @:if@? Also, don’t forget that the second block parameter is the options passed to @#render_cell@, which brings us to the next core change.

BTW, as a nice extension and in good OOP manners “cpb made the cache configurations inheritable”:https://github.com/apotonick/cells/commit/4c62dd002add6e5d83df611f9d7b5c6852d05d72. A cell derived from @CommentsCell@ will automatically inherit the cache setting for @#show@ – no need for redundancy.

h3. No more Options!

Luckily, we were able to remove the @#options@ behaviour from Cells – it added unnecessary state, and we try to avoid internal state wherever possible (unlike the Rails core). Where you used to access the _magic_ @options@ hash in a state method *you now use explicit state-args*.

class CommentsCell < Cell::Base
  def show(user, comments)
    @user, @comments = user, comments

Since the method expects arguments (“state-args”), here’s how you’d pass them into.

render_cell(:comments, :show, user, @comments)

Any additional argument after the state name (2nd argument) is passed to the state. The rest is up to you.

If you want the old behaviour with the options hash that was “simply there”, just include the @Deprecations@ module.

class CommentsCell < Cell::Base
  include Deprecations
  def show
    @user = options.first

h3. Test your View Assigns

Another small addition is the @#view_assigns@ method in @Cell::TestCase@. This works just like in conventional controller test (both Test::Unit and RSpec).

it "should assign correct values" do
  render_cell(:comments, :show)
  assert_equal @user_1, view_assigns[:user]

The @view_assigns@ hash captures all instance variables that were assigned in the rendering cycle. Some people may like that – I don’t. However, here it is.

Have fun with Cells! 😉


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s