trailblazer / cells

View components for Ruby and Rails.
https://trailblazer.to/2.1/docs/cells.html
3.07k stars 236 forks source link

Including translation helper can break form rendering in a cell. #322

Closed johnlane closed 9 years ago

johnlane commented 9 years ago

I have an initializer where I set up application-wide preferences. One thing that it does is include Cell helpers that I want to use in all Cells. Specifically, it does this:

# Include helpers that should be accessible to all cells
class Cell::Concept
  include ActionView::Helpers::TranslationHelper
  include Cell::Translation
end

The presnce of include ActionView::Helpers::TranslationHelper causes an error when rendering a form in a cell:

undefined method `output_buffer=' for #<Thing::Cell:0xb518d8cc>

The problem can be worked around by including ActionView::Helpers::TranslationHelper in each cell but it should be possible to include it once into the base class.

here is a small test Rails application that demonstrates the issue. The HEAD is in the broken state. Check out the prior commit to "fix" it.

apotonick commented 9 years ago

Your initializer is wrong, it must be

Cell::Concept.class_eval do
  include ActionView::Helpers::TranslationHelper
end

and not

class Cell::Concept
  include ActionView::Helpers::TranslationHelper
end
apotonick commented 9 years ago

Oh hang on, I still had trouble. I could make it work with

Cell::Concept.class_eval do
  include ActionView::Helpers::TranslationHelper
  include Cell::Erb
end

Thanks for the test app, BTW! :heart:

apotonick commented 9 years ago

I found out the problem. The Cells railtie is run after your initializer and Rails helper modules override our fixes. There's three fixes:

  1. Stop using Rails and lead a happier life.
  2. Make your initializer be run after the Cells railtie (no idea, I thought that was the default anyway).
  3. Introduce ApplicationCell < Cell::Concept where you include your additional global helpers.
johnlane commented 9 years ago

So is the class class_eval approach an option 4 to the above list or does 3 (ApplicationCell) supercede that?

I've tried both methods and both work.

which one to choose - is it just a matter of personal taste ?

apotonick commented 9 years ago

The class_eval didn't work for me! :blush: It's because of the loading order of railties and all that, but I had the urge to post it anyway, so it is a solution, but only in an initializer that's run after the cell railtie. Confused enough? :laughing: