After decades of struggling with myself, and after a fantastic “RubyConf Australia”:http://www.rubyconf.org.au/ with lots of love I finally managed to give cells users support for a global (brrr) @#content_for@ helper.
h3. Using It.
The new helper is added by including the “cells-capture”:https://github.com/apotonick/cells-capture gem into your @Gemfile@.
Cells need to declare explicitly that they want @#content_for@ to access the global buffer by including @Cell::Rails::Capture@. This is meant to remind users that they’re kinda breaking their encapsulation (although it feels handy, I admit).
class BassistCell < Cell::Rails include Cell::Rails::Capture def show render end end
You can now use the helper in your views – it will append passed content to the global buffer.
h3. Now What Is Your Problem?
We want cells users to have maximum comfortability, that is why the new helper is here. However, there are two problems with this approach.
* You’re changing state of a global variable from a well-encapsulated cell.
* A cached cell will mess up the global as you expect the cell to append something but it actually doesn’t (remember, there’s a cache hit).
h3. A Way Out?
render_cell(:bassist, :play) do |cell| content_for(:global_js, cell.js) end
class BassistCell < Cell::Rails cache :play def play render # usually, content_for happens here. end def js render # this could be a JS template end end
The content of the @#js@ view will be added to the @:global_js@ buffer. The cool thing: It will be invoked even when @#play@ is cached!
Anyway, let us know how you use it and enjoy.