Table of Contents
In Rails, I upgraded Cells gem from 3.11.3 to 4.0.2. And I upgraded Rails from 4.1.9 to 4.2.4 together. Here, I wrote what I did about Cells upgrade.
Basic instruction is in From Cells 3 to Cells 4 Upgrading Guide. It’s not easy to upgrade it.
Environment
- Ruby 2.2.3
- Template Engine: Slim
Story
ある人が外注に出していたプログラムを なぜか私がデザイン、マークアップ含めて更新することになり、 Rails と Gem のバージョンを更新した。
その Rails のアプリケーションにテストコードは一切なかった。
リリース前のプログラムに対して変更を行いました。
What I did
- Create ApplicationCell
- Change helper to include
- Change render_cell to cell
- Change file name
- Change partial view rendering way
Create ApplicationCell
If you use functions relates to device or form tags, the application will stop working after upgrading to Cells 4. Then I created ApplicationCell
as a parent class of all Cell class.
アプリケーションを見ながら 次のような ApplicationCell
を作りました。 include
で取り込んでいるモジュールは、 エラーが出る度に順次取り込んでいったものです。
self.helper_method
is called by devise
, so it is necessary. Cells では 既に意味のないメソッドになっているので、 何も処理は行いません。
Change helper to include
In each cell class, there was a line “helper ApplicationHelper
“.
Webrick を実行してページを開くと undefined method と表示されました。 Then, change helper
statement to include
statement with sed
command.
Change render_cell to cell
Cells 4 から メソッドが変わりました。 ビューの中で使用されている render_cell
を cell
に書き換えました。
いままで書いていた人がコードを綺麗に保っていたので、 sed
コマンドを使って簡単に変更できました。 書き方が何通りもある場合は、簡単にはいきません。
Before
After
Change file name
There were cell templates named like method_name.html.slim
, but Cells 4 method cell(:cell_name).(:method_name)
search template named method_name.slim
. It caused exception.
In cells/lib/cell/view_model.rb
of Cells 4, find_template
method generates template name. Generated template name by that method doesn’t contain the string 'html'
.
I removed “html” from template file name.
Change partial view rendering way
I know what I did is quick and dirty way.
In old code, cell template renders partial view app/views/shared/something.html.slim
.
Before
But it doesn’t work in Cells 4.
cells/lib/cell/templates.rb
と cells/lib/cell/view_model.rb
を見ながら次のようにコードを変更しました。
After
I had not only to create prefixes and view value, but change view value.
app/views
内のテンプレートを呼び出す際のメソッドを ApplicationCell
に作ったほうがよかったかもしれません。
Cell の中に self.view_paths += ['app/views']
を加えると、 app/views/cell_name
の中でテンプレートを探すようになります。 つまり、次のように変更しても動くようになります。