04 sending multipart_emails_using_template_handlers

22
CHAPTER 4 Sending Multipart Emails Using Template Handlers Crafting Rails4 Application

Upload: kazuyuki-ikeda

Post on 29-Jun-2015

435 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: 04 sending multipart_emails_using_template_handlers

CHAPTER 4 Sending Multipart Emails Using Template Handlers

Crafting Rails4 Application

Page 2: 04 sending multipart_emails_using_template_handlers

概要の説明(p.63)• template handlerがmarkdownを使い、plain-textとhtml viewsを1つのtemplateとしtemplate handlerを開発する。

• 上記のtemplate handerではruby codeが解釈できないのでtemplateとERBをconvertして、markdown compilerを利用したtemplate handleを開発する。

• generators and configureで新しいtemplate handlerをデフォルトで作成します。

Page 3: 04 sending multipart_emails_using_template_handlers

Markdown templateから生成したHTMLはこんな感じです。

Page 4: 04 sending multipart_emails_using_template_handlers

4.1 Playing with the Template-Handler API

• Markdown + ERB handlerに入る前にいくつかのtemplate handlersを作成してAPIの理解を進めます。

Page 5: 04 sending multipart_emails_using_template_handlers

craft new plug-in(p.64)• template handerの開発 $ rails plugin new handlers

Page 6: 04 sending multipart_emails_using_template_handlers

integration testに必要なroutesとcontrollerとtemplateを追加

Page 7: 04 sending multipart_emails_using_template_handlers

integration testの作成(p.65)

Page 8: 04 sending multipart_emails_using_template_handlers

templatesでrbを利用できるようにしたい。

• ActionView::Template.register_template_handlerに拡張子とhandler objectを渡す。hanlder objectはcall()を返して、かつStringを返しますので、lambdaを渡します。

• :source.to_procとlambda { |template| template.source }は同じ

Page 9: 04 sending multipart_emails_using_template_handlers

ちなみにActionView::Template.register_template_han

dler

Page 10: 04 sending multipart_emails_using_template_handlers

def test(*a, b)の動作• def test(*a, b) p a p bend

• 末尾の引数が、testメソッド内でbとして扱われ、それ以外はa[]として格納される。

Page 11: 04 sending multipart_emails_using_template_handlers

String Template Handler

• インスタンス変数(@what)を含んだtemplateを利用できるようにする。

Page 12: 04 sending multipart_emails_using_template_handlers

integration testを作成

Page 13: 04 sending multipart_emails_using_template_handlers

4.2 Building a Template handler with Markdown + ERB(p.66)

• Markdown syntaxをHTMLに変換できるRDiscountを使う.

Page 14: 04 sending multipart_emails_using_template_handlers

Markdown template handler p.67まずはMark downでかかれたtemplateの作成とgemspecにrdiscount gemを追加します。

Page 15: 04 sending multipart_emails_using_template_handlers

templateとtemplate handlerの追加

Page 16: 04 sending multipart_emails_using_template_handlers

gem rdiscountを追加

Page 17: 04 sending multipart_emails_using_template_handlers

MERB Template Handler p.68続いて、Mark DownとERB template handler

を開発する。

Page 18: 04 sending multipart_emails_using_template_handlers

lambdaを利用せずに、moduleを定義してcall()返すようにします。

Page 19: 04 sending multipart_emails_using_template_handlers

template handlerにより生成される文字列はMarkdown syntaxが含まれていて、RDicountを利用して

HTMLに変換されています。

Page 20: 04 sending multipart_emails_using_template_handlers

begin/endでなぜ囲んでいるのか?

Page 21: 04 sending multipart_emails_using_template_handlers

begin/endに挟むことでRubyで例外が発生した際にrender handlerで無効なruby codeを生成するようになっている。

Page 22: 04 sending multipart_emails_using_template_handlers

Multipart Emails p.69