RailsのCells 3系と4系の違い

自分用のメモです。

Cellsって何?

Nick Suttererさんが開発しているコンポーネントフレームワークRailsの仕組みの一つ部分テンプレートに専用コントローラーをつけたもの(として開発されていた)。

3系と4系

Ruby on Railsのアプリケーションが大規模化することで、Ruby on Railsの規約と規範と実際に開発したいアプリケーションの間のギャップが開きつつあり、いろいろな提案がされている(ここにリンクされているような話)。

そうした背景の下、Nick SuttererさんはTrailblazerというRuby on Railsをベース(wrap?)する形のアーキテクチャを提案し、そのアーキテクチャの実現ツールとしてCellsを大幅に刷新した。その刷新したのがCells 4系。

GitHub: apotonick/cellsによると2014年の11月ごろからCells 4.0がmasterブランチになっている。一方、3系は Ruby on Rails 4.2系に対応させた 3.11.3が最終版。ForkのNetworkを見るかぎり、Nickさん自身が3系のメンテナンスをすることはなさそう(Pull Requestがあったら対応する程度みたい)。

3系と4系の大きな違いは、4系ではActionViewレンダリングエンジンとして使っていないこと。この結果、RailsのActionHelperを使うときには明示的にincludeしないと使えない(らしい)

Starting with Cells 4.0 we no longer use ActionView as a template engine. Removing this jurassic dependency cuts down Cells' rendering code to less than 50 lines and improves rendering speed by 300%!
README.mdより)

The difference to conventional Rails views is that every method called in a view is directly called on the cell instance. The cell instance is the rendering context. This allows a very object-oriented and clean way to implement views.

Helpers as known from conventional Rails where methods and variables get copied between view and controller no longer exist in Cells.

Note that you can still use helpers like link_to and all the other friends - you have to include them into the cell class, though.
README.mdより)

問題は、どこで何をincludeしたら使えるのかが私の知識不足でわからないという点。

Cells 4系の導入

Ruby Gemには4系がないので、GitHubから導入する。Gemfileに以下を追加する。

gem 'cells', github: 'apotonick/cells'

あとはいつもどおり bundle install 。

Cells 4系の使い方

README.mdの以下の部分で渡された @comment にどうやってアクセスするのか?

= cell(:comment, @comment)

app/cells/comment_cell.rbにて @comment は model に格納されている。

class CommentCell < Cell::ViewModel
  def show
    render
  end

private

  def body
    model.body
  end

  def author_link
    link_to model.author.name, model.author
  end
end

app/cells/comment_cell/show.erb にて

<h1>Comment</h1>
<%= body -%>
By
<%= author_link ->

自作ヘルパーを使いたい場合

app/helpers/comments_helper.rb が存在する場合。app/cells/comment_cell.rbにincludeする。

class CommentCell < Cell::ViewModel
  include CommentsHelper

  def show
    render
  end
end

既にインクルードされているヘルパー

そんなこと覚えてない:ActionView を単体で使ってみるを参考に、Railsのヘルパーでインクルードされているものを調べてみた(Cells:lib/cell/view_model.rbソースコードでも調べられる)

app/cells/comment_cell/show.erbにて

<%= self.class.ancestors.map(&:to_s).grep(/Helper/) -%>

上記の結果は以下のとおり。

参考