04 sending multipart_emails_using_template_handlers
TRANSCRIPT
CHAPTER 4 Sending Multipart Emails Using Template Handlers
Crafting Rails4 Application
概要の説明(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をデフォルトで作成します。
Markdown templateから生成したHTMLはこんな感じです。
4.1 Playing with the Template-Handler API
• Markdown + ERB handlerに入る前にいくつかのtemplate handlersを作成してAPIの理解を進めます。
craft new plug-in(p.64)• template handerの開発 $ rails plugin new handlers
integration testに必要なroutesとcontrollerとtemplateを追加
integration testの作成(p.65)
templatesでrbを利用できるようにしたい。
• ActionView::Template.register_template_handlerに拡張子とhandler objectを渡す。hanlder objectはcall()を返して、かつStringを返しますので、lambdaを渡します。
• :source.to_procとlambda { |template| template.source }は同じ
ちなみにActionView::Template.register_template_han
dler
def test(*a, b)の動作• def test(*a, b) p a p bend
• 末尾の引数が、testメソッド内でbとして扱われ、それ以外はa[]として格納される。
String Template Handler
• インスタンス変数(@what)を含んだtemplateを利用できるようにする。
integration testを作成
4.2 Building a Template handler with Markdown + ERB(p.66)
• Markdown syntaxをHTMLに変換できるRDiscountを使う.
Markdown template handler p.67まずはMark downでかかれたtemplateの作成とgemspecにrdiscount gemを追加します。
templateとtemplate handlerの追加
gem rdiscountを追加
MERB Template Handler p.68続いて、Mark DownとERB template handler
を開発する。
lambdaを利用せずに、moduleを定義してcall()返すようにします。
template handlerにより生成される文字列はMarkdown syntaxが含まれていて、RDicountを利用して
HTMLに変換されています。
begin/endでなぜ囲んでいるのか?
begin/endに挟むことでRubyで例外が発生した際にrender handlerで無効なruby codeを生成するようになっている。
Multipart Emails p.69