Cells v4.1.0 Release Notes

Release Date: 2016-05-08 // over 4 years ago
  • API Fix/Changes

    • ๐Ÿšš All Rails code removed. Make sure to use Cells-rails if you want the old Rails behavior.
    • ๐Ÿ— The builds feature is now optional, you have to include Builder in your cell.

      class CommentCell < Cell::ViewModel
        include Cell::Builder
      
        builds do |..|
      
    • A basic, rendering #show method is now provided automatically.

    • ViewModel#render now accepts a block that can be yielded in the view.

    • Passing a block to ViewModel#call changed. Use tap if you want the "old" behavior (which was never official or documented).

      Comment::Cell.new(comment).().tap { |cell| }
      

      The new behavior is to pass that block to your state method. You can pass it on to render, and then yield it in the template.

      def show(&block)
        render &block # yield in show.haml
      end
      

      Note that this happens automatically in the default ViewModel#show method.

    • ๐Ÿ’… Concept#cell now will resolve a concept cell (Song::Cell), and not the old-style suffix cell (SongCell). The same applies to Concept#concept.

      concept("song/cell", song).cell("song/cell/composer") #=> resolves to Song::Cell::Composer
      

      This decision has been made in regards of the upcoming Cells 5. It simplifies code dramatically, and we consider it unnatural to mix concept and suffix cells in applications.

    • In case you were using @parent_controller, this doesn't exist anymore (and was never documented, either). Use context[:controller].

    • ::self_contained! is no longer included into ViewModel. Please try using Trailblazer::Cell instead. If you still need it, here's how.

      class SongCell < Cell::ViewModel
        extend SelfContained
        self_contained!
      
    • ๐Ÿ—„ Cell::Concept is deprecated and you should be using the excellent Trailblazer::Cell class instead, because that's what a concept cell tries to be in an awkward way. The latter is usable without Trailblazer.

      We are hereby dropping support for Cell::Concept (it still works).

    • ๐Ÿ—„ Deprecating :collection_join and :method for collections.

    Awesomeness

    • ๐Ÿš… Introduced the concept of a context object that is being passed to all nested cells. This object is supposed to contain dependencies such as current_user, in Rails it contains the "parent_controller" under the context[:controller] key.

      Simple provide it as an option when rendering the cell.

      cell(:song, song, context: { current_user: current_user })
      

      The #context method allows to access this very hash.

      def role
        context[:current_user].admin? "Admin" : "A nobody"
      end
      
    • The cell helper now allows to pass in a constant, too.

      cell(Song::Cell, song)
      
    • ๐Ÿ†• New API for :collection. If used in views, this happens automatically, but here's how it works now.

      cell(:comment, collection: Comment.all).() # will invoke show.
      cell(:comment, collection: Comment.all).(:item) # will invoke item.
      cell(:comment, collection: Comment.all).join { |cell, i| cell.show(index: i) }
      

      Basically, a Collection instance is returned that optionally allows to invoke each cell manually.

    • Layout cells can now be injected to wrap the original content.

      cell(:comment, Comment.find(1), layout: LayoutCell)
      

      The LayoutCell will be instantiated and the show state invoked. The content cell's content is passed as a block, allowing the layout's view to yield.

      This works with :collection, too.