After decades of struggling with myself, and after a fantastic RubyConf Australia with lots of love I finally managed to give cells users support for a global (brrr)
The new helper is added by including the cells-capture gem into your
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.
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).
#content_for block is never called! This is a well-known problem in traditional Rails views where people call
#content_for in a
A Way Out?
render_cell(:bassist, :play) do |cell| content_for(:global_js, cell.js) end
Here, instead of calling
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.