うちのいぬ Tech Blog

Tech Blog of Uchinoinu/My dog

Crafting Rails 4 Applications - capter 2. Building Models with Active Model

こちらの記事の続きです。

tsumekoara.hateblo.jp

第二章 ActiveModelでモデルを構築する

前章では、AbstractControllerについてと、ActionMailerやActionControllerの両方を引き離し、Railsのソースを軽量化する方法を見てきました。次はActiveModelについて同様に見て行きましょう。

ActiveModelはそもそもActiveRecordとActiveResoureの間で共有された振る舞いを持つように作られています。AbstractControllerの様に、求められた機能が必要なモジュールのみを含むようにすることができます。ActiveModelはRailsのControllerで必要とされるAPIを定義することに責務を負い、その他の ORM(Object-Relational Mapper)はActiveModelを、ActiveRecordと同じようにRailsが振る舞うことを保証するように使うことができます。

それでは、この章ではControllerとViewで使えるMail Formを呼び出すプラグインを書くことでActiveModelについて見て行きましょう。Mail Formは、リクエストをPOSTし、バリデートし、指定されたメールアドレスにメールを送ることで、ハッシュのパラメーターを受け取ることを目指します。これを抽象化することで、たった数分で全機能が搭載されたコンタクトフォームを作ることができるようになります。

Work In Progress

  1. Creating Our Own Renderer

1-1. Crafting Rails 4 Applications - capter 1-1 - SatomisaTo - life with Tech and Dog

1-2. Crafting Rails 4 Applications - capter 1-2 - Writing the Renderer - SatomisaTo - life with Tech and Dog

1-3. Crafting Rails 4 Applications - capter 1-3 - Understanding the Rails Rendering Stack - SatomisaTo - life with Tech and Dog

1-4. Crafting Rails 4 Applications - capter 1-4 - Talking It to the Next Level - SatomisaTo - life with Tech and Dog

1-5. Crafting Rails 4 Applications - capter 1-5 - Wrapping Up - SatomisaTo - life with Tech and Dog