

What were global helper functions are now instance methods. Note that how that completely replaces helpers with clean object-oriented methods. This means we have to implement #body and #author_link in the cell class. # app/concepts/comment/views/show.hamlĮvery method invoked in the view is called on the cell instance.
#Tiny rails engine styles code#
While you could reference model throughout your view and strongly couple view and model, Cells makes it extremely simple to have logicless views and move presentation code to the cell instance itself.

That’s right, you can use Rails helpers in cell views. Note that you’re free to use ERB, Haml, or Slim. Using #render without any arguments will parse and interpolate the app/concepts/comment/views/show.haml template. Render # renders app/concepts/comment/views/show.haml While we already have a cleaner interface as compared to helpers accessing to global state, the real power of Cells comes when rendering views. The cell provides access to it via model and it’s your job do use it correctly. This can be an ActiveRecord model, a PORO or an array of attachments. Note that you can pass anything into a cell.
#Tiny rails engine styles how to#
It is completely up to the cell how to return the necessary markup.įollowing the Trailblazer convention, the Comment::Cell sits in app/concepts/comment/cell.rb. This will instantiate and invoke the Comment::Cell for each comment. = concept("comment/cell", comment) #=> Comment::Cell.new(comment).show Mostly, you want to use them in controller views or actions to replace a complex helper/partial mess. Cells embrace all presentation and rendering logic to present a fragment of the UI.Ĭells can be rendered anywhere in your application. Think of cells, or view models, as small Rails controllers, but without any HTTP coupling. While exposing a better performance, you step-wise encapsulate fragments into cell widgets and enforce interfaces. In other words: A cell is an object that can render a template.Ĭells are faster than ActionView. The scope of that fragment is up to you: it can embrace an entire page, a single comment container in a thread or just an avatar image link. The cells gem is completely stand-alone and can be used without Trailblazer.Ī cell is an object that represent a fragment of your UI. Out of the frustration with Rails’ view layer, its lack of encapsulation and the convoluted code resulting from partials and helpers both accessing global state, the Cells gem emerged.
