More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

180
関西Ruby会議02 株式会社万葉 続・現場で役立つ Ruby on Rails パターン (株)万葉 大場寧子 2009117日土曜日

description

The presentation document of a speech at Kansai Ruby Kaigi #02.

Transcript of More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

Page 1: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

関西Ruby会議02

株式会社万葉

続・現場で役立つRuby on Rails

パターン(株)万葉 大場寧子

2009年11月7日土曜日

Page 2: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

•Ruby会議2009•Pragmatic Patterns of Ruby on Rails - 現場で役立つRuby on Railsパターン

2009年11月7日土曜日

Page 3: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

自己紹介

•プログラマ•Rails アプリケーションを開発•Akasaka.rb•株式会社万葉

2009年11月7日土曜日

Page 4: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

提供

株式会社万葉Everyleaf Corp.

2009年11月7日土曜日

Page 5: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

Ruby on Rails逆引きクイックリファレンス

2009年11月7日土曜日

Page 6: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

サイン会やります

2009年11月7日土曜日

Page 7: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

作ってるもの

•Web家計簿「小槌」•iPhoneアプリ「iCarta」•プラグイン類 •ナレジオン (株)ユーフィット

2009年11月7日土曜日

Page 8: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

Web家計簿「小槌」• http://www.kozuchi.net• http://github.com/everyleaf/kozuchi

2009年11月7日土曜日

Page 9: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

iCarta

2009年11月7日土曜日

Page 10: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

プラグイン

•html5jp_graphs•sub_resources•record_with_operator•i18n_ext•image_upload (RubyForge)

http://github.com/nay

2009年11月7日土曜日

Page 11: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

http://knowledgeon.com/(株)ユーフィットグループウェア

10/15 にリリース

ナレジオン

2009年11月7日土曜日

Page 12: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

ナレジオンの特徴•ひととおりの機能の揃ったグループウェア•気軽な情報公開ができる•しっかりした開示制限と横断的な検索•ワークフロー

2009年11月7日土曜日

Page 13: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

Railsにしては人数の多い開発体制

(10人強)

2009年11月7日土曜日

Page 14: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

Rubyでの開発•少人数が「勝ち」と言われる

2009年11月7日土曜日

Page 15: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

必要なら仕方ない

•短期間に多くの機能•並行開発

2009年11月7日土曜日

Page 16: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

開発体制を工夫

•アジャイルっぽく•ペアプログラミング•分担を固定しない•朝会•合意作り

2009年11月7日土曜日

Page 17: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

合意作りが大事

•大事なことは合意を作る•書いておく•合意≠遵守•合意≠既成事実

2009年11月7日土曜日

Page 18: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

そうした現場で気づいた技術ネタを

今日喋ります

2009年11月7日土曜日

Page 19: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

Ruby会議2009で話したことの

おさらい

2009年11月7日土曜日

Page 20: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

アプリケーションが大きく複雑に

なると

2009年11月7日土曜日

Page 21: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

メンテナンスが大変になる

2009年11月7日土曜日

Page 22: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

色々な人間がコードを触る

2009年11月7日土曜日

Page 23: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

人によって書き方が違ったり

2009年11月7日土曜日

Page 24: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

変更しづらくなる

2009年11月7日土曜日

Page 25: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

変更できることは大事

•小さくはじめられる•素早いサービスの開始•後から育てられる•ビジネスの変化についていく

2009年11月7日土曜日

Page 26: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

さらに

2009年11月7日土曜日

Page 27: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

Rails案件Rails技術者の

増加

2009年11月7日土曜日

Page 28: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

メンテナンスすべきRailsプロジェクト

の増加

2009年11月7日土曜日

Page 29: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

変更しやすさを維持する

仕組みが必要!

2009年11月7日土曜日

Page 30: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

それには?

2009年11月7日土曜日

Page 31: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

コードを良い状態に保つことが重要

2009年11月7日土曜日

Page 32: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

良い状態

•いい設計•読みやすい、わかりやすい•間違いにすぐ気づく

2009年11月7日土曜日

Page 33: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

そこで実装パターン

•Rails における実装パターンを見つけ出し、共有する•効率がよい•一貫性がある

2009年11月7日土曜日

Page 34: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

具体的には

•ARオブジェクトから検索を始める•ARオブジェクトをフィルターで得る•ビジネスロジックをモデルに移動する方法•コールバックの活用

2009年11月7日土曜日

Page 35: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

Ruby会議2009で話しました :)

ということを

2009年11月7日土曜日

Page 36: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

本題

2009年11月7日土曜日

Page 37: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

今日の話

1.本当は奥が深い検証の話2.RESTfulとの付き合い方

2009年11月7日土曜日

Page 38: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

その前に

•Rubyをよく使っている人?•Railsをよく使っている人?

2009年11月7日土曜日

Page 39: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

1. 本当は奥が深い

検証の話

2009年11月7日土曜日

Page 40: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

Rails の検証は良くできている

save

検証 保存入力画面

エラー情報2009年11月7日土曜日

Page 41: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

コード例:モデル

class Book < ActiveRecord::Base validates_presence_of :titleend

2009年11月7日土曜日

Page 42: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

コード例:画面

<%= error_messages_for :book %>

2009年11月7日土曜日

Page 43: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

任意の検証が書けるvalidate :check_title

privatedef check_title errors.add(:title, 'Rubyがないよ!')

unless title.to_s =~ /Ruby/end

2009年11月7日土曜日

Page 44: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

とても便利

•豊富な検証メソッド•自由に追加できる•エラーメッセージを扱える•不正状態で保存するのを防止

2009年11月7日土曜日

Page 45: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

しかし

2009年11月7日土曜日

Page 46: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

最初の印象よりも奥が深い

2009年11月7日土曜日

Page 47: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

私の遭遇した課題

•乱用されることがある•検証ニーズが場面によって違う場合に困っている人を見かける

2009年11月7日土曜日

Page 48: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

検証についての課題

1)何を「検証」すればいいのか2)場面によって検証ニーズが違うとき

2009年11月7日土曜日

Page 49: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

1)何を「検証」すれば

いいのか2009年11月7日土曜日

Page 50: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

Rails の ActiveRecord の

「検証」とは?

2009年11月7日土曜日

Page 51: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

ActiveRecordオブジェクトが不正な状態で

永続化されないようにするための仕組み

一般的な定義

2009年11月7日土曜日

Page 52: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

いいえ

2009年11月7日土曜日

Page 53: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

ActiveRecordオブジェクトが

ユーザーに許可された操作によって不正な状態で

永続化されないようにするための仕組み

私の定義

2009年11月7日土曜日

Page 54: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

ユーザーに許可された操作によって

2009年11月7日土曜日

Page 55: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

Railsの検証の失敗は、予期された、ユーザーに対して丁寧に対応すべき出来事としてデザインされている

2009年11月7日土曜日

Page 56: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

検証失敗は正規ルート

•saveでfalseが返る•美しいメッセージを用意•表示用ヘルパー

2009年11月7日土曜日

Page 57: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

従って

2009年11月7日土曜日

Page 58: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

これは対象外

•ユーザーによる不正な操作で生じる不正な状態の検出•バグや故障によって生じる不正な状態の検出

2009年11月7日土曜日

Page 59: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

つまり

モデルの検証

validate

2009年11月7日土曜日

Page 60: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

乱用されがち(過度の期待)

2009年11月7日土曜日

Page 61: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

乱用の例1

モデルの検証=

Validate

2009年11月7日土曜日

Page 62: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

ユーザーが解決できない

エラーメッセージ

弊害1

2009年11月7日土曜日

Page 63: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

開発者が不具合に気づきにくい

弊害2

2009年11月7日土曜日

Page 64: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

弊害3

•無駄にきれいなメッセージ•無駄に丁寧な例外処理

無駄な労力

2009年11月7日土曜日

Page 65: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

改善策

モデルの検証

validate

コールバックで例外2009年11月7日土曜日

Page 66: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

コールバック

•before_validation•after_validation•before_save•after_save•etc...

2009年11月7日土曜日

Page 67: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

before_save :check_parent_id

privatedef check_parent_id return true unless parent raise “bad parent_id!” unless parent.user_id != user_idend

コード例

2009年11月7日土曜日

Page 68: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

before_save :check_parent_id

privatedef check_parent_id return true unless parent raise IllegalOperationError, “bad parent_id!” unless parent.user_id != user_idend

専用の例外クラス

2009年11月7日土曜日

Page 69: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

そのほか

•initializeや代入時に不正状態をはじくことも

2009年11月7日土曜日

Page 70: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

乱用の例2

2009年11月7日土曜日

Page 71: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

validate

乱用の例2

モデルの検証

Controller層のエラー処理

2009年11月7日土曜日

Page 72: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

検証をエラー処理全般に

利用する?

検証≠エラー処理2009年11月7日土曜日

Page 73: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

まとめ何を「検証」するか

•モデルの状態•ユーザーに許可した操作によって起こりえる状態不正

2009年11月7日土曜日

Page 74: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

2)場面によって検証ニーズが違うとき

2009年11月7日土曜日

Page 75: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

例•コマンドラインツール•管理者と一般人•ウィザード形式での検証•機能による違い•etc...

2009年11月7日土曜日

Page 76: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

あるモデルの「検証」は1パターンではない

2009年11月7日土曜日

Page 77: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

でもActiveRecordの検証の通り道は1パターン

2009年11月7日土曜日

Page 78: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

そんなときは

2009年11月7日土曜日

Page 79: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

分岐させる

検証

検証

Object

検証

save save save

機能A 機能B コンソール

2009年11月7日土曜日

Page 80: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

モデルの用語で表現

検証

検証

Object

検証

save save save

状態A 状態B 状態C

2009年11月7日土曜日

Page 81: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

「状態」を設計する

検証

検証

Object

検証

save save save

デフォルト loose? by_admin?

2009年11月7日土曜日

Page 82: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

各検証で分岐

validates_presence_of :description, :if => Proc.new {|o| !o.loose?}

2009年11月7日土曜日

Page 83: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

意外と面倒くさい

2009年11月7日土曜日

Page 84: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

自明だと思っていると痛い目にあう

2009年11月7日土曜日

Page 85: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

モデルの様々な「使われ方」

を意識するとよい

2009年11月7日土曜日

Page 86: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

典型的な局面

•そのモデルの作成/編集画面•関連モデルの作成/編集画面•単体テスト•バッチ、ツール類•コンソールでの訂正

2009年11月7日土曜日

Page 87: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

2. 本当は奥が深い

検証の話完2009年11月7日土曜日

Page 88: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

ちなみに

2009年11月7日土曜日

Page 89: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

コールバック全般についても同じ

2009年11月7日土曜日

Page 90: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

Railsで選べるコールバックの選択肢

•create/update/両方/なし•検証の有無•destroyコールバックの有無

2009年11月7日土曜日

Page 91: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

アプリケーションの要件は

もっと複雑

2009年11月7日土曜日

Page 92: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

起こりがちな問題•削除していいかどうかのチェック条件が論理削除と物理削除で違う•画面では子モデルを自動で作りたいがツールでは単体で作りたい

2009年11月7日土曜日

Page 93: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

ここでも

callback

callback

Object

callback

callback callback callback

状態A 状態B 状態C

callback

2009年11月7日土曜日

Page 94: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

二段構え要件を単純にする

うまく複雑化する

2009年11月7日土曜日

Page 95: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

2.RESTfulとの付き合い方

2009年11月7日土曜日

Page 96: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

日頃、RESTとおつきあいしてますか?

2009年11月7日土曜日

Page 97: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

RESTとは?•URLはリソース(名詞)•groups/3/notes/1•HTTPメソッドは操作(動詞)•GET → 取得したい•DELETE → 削除してください•シンプルで統一された、人間も読むことのできるI/F

2009年11月7日土曜日

Page 98: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

私のポジショニング

•REST厨ではない(多分)•きれいなI/Fは気持ちいい•現実主義者

2009年11月7日土曜日

Page 99: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

RailsでのRESTful

•とっつきにくい•実用的•ちょっと足りない•routes.rbが鬼

2009年11月7日土曜日

Page 100: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

大事なこと

•逃げちゃだめだ•逃げちゃだめだ•(ry

2009年11月7日土曜日

Page 101: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

RESTfulにするとなにが嬉しいか?

2009年11月7日土曜日

Page 102: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

Railsについていく

2009年11月7日土曜日

Page 103: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

きれいなI/Fは気持ちいい

2009年11月7日土曜日

Page 104: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

予測できる統一される

2009年11月7日土曜日

Page 105: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

メンテナンスしやすい

2009年11月7日土曜日

Page 106: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

でも難しい

2009年11月7日土曜日

Page 107: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

うまくつき合う方法

1) Rails的なツボ2) RESTの基本3) URLから考える4) routes.rbの整理

2009年11月7日土曜日

Page 108: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

1) RailsのRESTfulのツボ

2009年11月7日土曜日

Page 109: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

マッピング

前提知識

2009年11月7日土曜日

Page 110: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

コントローラ

マッピング(ルーティング)

URL

コントローラ

アクションアクション

2009年11月7日土曜日

Page 111: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

RailsのRESTfulのツボは

2009年11月7日土曜日

Page 112: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

アクション全体

よくあるアクションの

よくあるアクション

2009年11月7日土曜日

Page 113: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

map.xxxmap.connect

aaaao a

マッピングを楽に

map.resourcesmap.resource

+option

2009年11月7日土曜日

Page 114: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

よくあるアクションのURL メソッド アクション

/groups GET index

/groups POST create

/groups/3 GET show

/groups/3 PUT update

/groups/3 DELETE destroy

/groups/new GET new

/groups/3/edit GET edit

2009年11月7日土曜日

Page 115: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

マッピングを楽に

map.resources :groups

2009年11月7日土曜日

Page 116: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

その他のことは

•なんとかしてください•map.resourcesのオプション•名前付きルート•connet•最悪、何ともしなくても•/:controller/:action/:id

2009年11月7日土曜日

Page 117: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

もう1点

2009年11月7日土曜日

Page 118: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

マッピング(ルート)に名前をつけよう

2009年11月7日土曜日

Page 119: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

それをURLの名前にしよう

2009年11月7日土曜日

Page 120: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

名前に対応するメソッドがあれば便利じゃね?

2009年11月7日土曜日

Page 121: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

例'groups'という名前をつけたURLへリンクする

<%= link_to "グループ一覧",

groups_path %>

2009年11月7日土曜日

Page 122: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

そこでURL URL名 メソッド アクション

/groupsgroups

GET index

/groupsgroups

POST create

/groups/3

group

GET show

/groups/3 group PUT update

/groups/3

group

DELETE destroy

/groups/new new_group GET new

/groups/3/edit edit_group GET edit

2009年11月7日土曜日

Page 123: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

だからmap.resourcesすると自動的にメソッドが使える

<%= link_to "グループ一覧",

groups_path %>

2009年11月7日土曜日

Page 124: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

2) RESTの基本

2009年11月7日土曜日

Page 125: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

リクエスト=

名詞 + 動詞

2009年11月7日土曜日

Page 126: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

+名詞

リソースURL

動詞

HTTPメソッド

2009年11月7日土曜日

Page 127: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

名詞(リソース)

•相手は複数か、単数か?•名前は?•識別するための情報は?

2009年11月7日土曜日

Page 128: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

名詞に注目URL URL名 メソッド アクション

/groupsgroups

GET index

/groupsgroups

POST create

/groups/3

group

GET show

/groups/3 group PUT update

/groups/3

group

DELETE destroy

/groups/new new_group GET new

/groups/3/edit edit_group GET edit

2009年11月7日土曜日

Page 129: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

動詞種類 意味 特徴

GET 取得 リソースの状態を変更しない

PUT 更新/作成 URLの示すリソースが処理後も同じリソースDELETE 削除URLの示すリソースが処理後も同じリソース

POST なんか変更 上記以外

2009年11月7日土曜日

Page 130: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

動詞に注目URL URL名 メソッド アクション

/groupsgroups

GET index

/groupsgroups

POST create

/groups/3

group

GET show

/groups/3 group PUT update

/groups/3

group

DELETE destroy

/groups/new new_group GET new

/groups/3/edit edit_group GET edit

2009年11月7日土曜日

Page 131: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

つまり

•4つの動詞だけで機能を説明することを考える•適切な動詞•適切な名詞

2009年11月7日土曜日

Page 132: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

3) URLから考える

2009年11月7日土曜日

Page 133: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

URLを設計してその後で

コントローラクラスを設計する

2009年11月7日土曜日

Page 134: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

いつものやり方

•Wikiに URL、HTTPメソッド、アクション、機能の対応表を書く•レビューする

2009年11月7日土曜日

Page 135: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

すると

•きれいなURLになる•うまくコントローラを分割できる•リソースという概念を意識

2009年11月7日土曜日

Page 136: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

4) routes.rbの整理

2009年11月7日土曜日

Page 137: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

routes.rb は散らかりやすい

2009年11月7日土曜日

Page 138: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

map.xxxmap.connect

aaaao a

楽になってない部分

map.resourcesmap.resource

+option

2009年11月7日土曜日

Page 139: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

様々な要件

•管理機能には /admin •/books/3/notes/2 のような深い構造•dairy/2009/4 のような特殊なURL

2009年11月7日土曜日

Page 140: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

難しいから間違える•変なルート名 (delete_xxx_path ?)•二重の定義•同じコントローラで同じ対象リソースが:idできたり :xxx_id できたりする

2009年11月7日土曜日

Page 141: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

おすすめの方策

•with_optionsの活用•コントローラ別に記述する•sub_resources

2009年11月7日土曜日

Page 142: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

map.with_options :path_prefix => 'books', :controller => 'books’ do |books|

books.monthly_books ':year/:month', :action => 'monthly' books.daily_books ':year/:month/:date' :action => 'daily'end

with_optionsの活用

2009年11月7日土曜日

Page 143: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

コントローラ別に記述map.with_options :controller => 'books' do |books| // BooksControllerのマッピングendmap.with_options :controller => 'authors' do |authors| // AuthorsControllerのマッピングend

2009年11月7日土曜日

Page 144: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

resourcesのネストmap.resources :diaries do |d| d.resources :commentsend

diaries/3/comments/2

2009年11月7日土曜日

Page 145: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

resourcesのネスト

•コントローラが分かれていてネスト構造ならとても便利•複雑になると「コントローラ別」に記述できない

2009年11月7日土曜日

Page 146: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

ネストで散り散りの例map.resources :diaries do |d| d.resources :commentsendmap.all_comments 'diaries/comments’, :controller => 'diary_comments’, :action => 'all_comments'

2009年11月7日土曜日

Page 147: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

URL構造VS

コントローラ

どちらで階層化するか

2009年11月7日土曜日

Page 148: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

コントローラ構造優先

私の意見

2009年11月7日土曜日

Page 149: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

なぜなら•routes.rbをメンテナンスする動機はアクション•自然とコントローラを探す•コントローラ構造はみんなに把握されている•URL構造は一部の人だけ

2009年11月7日土曜日

Page 150: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

なので

•routes.rbでのネスト表記は、コントローラ別の構造を損なわないなら使う•損なうならネストを使わず自前で書く

2009年11月7日土曜日

Page 151: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

map.resources :diariesmap.with_options :controller => 'diary_comments' do |dc| dc.resources :comments, :path_prefix => 'diary/:diary_id', :name_prefix => 'diary_’ dc.all_comments 'diaries/comments’, :action => 'all_comments'end

ネストを自前で

2009年11月7日土曜日

Page 152: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

sub_resourcesプラグイン

2009年11月7日土曜日

Page 153: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

コントローラ設計

•リソースごとに1コントローラ•Railsではこれが作りやすい(map.resources、フィルター)

•状況によってリソースに対して複数コントローラ•一般用とadmin用など

2009年11月7日土曜日

Page 154: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

複数リソースを1コントローラに

•通常はしない•new_comment•delete_comment•...•→ XxxCommentsControllerへ

2009年11月7日土曜日

Page 155: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

でも本当は

2009年11月7日土曜日

Page 156: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

必要な時もあります

•複数のコントローラに共通のサブリソースCRUDをつけたい•CRUD全部じゃなくて1つアクションつけたいだけだから許してほしい

2009年11月7日土曜日

Page 157: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

汎用サブリソースの例

•タグ•画像•通知設定

2009年11月7日土曜日

Page 158: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

モデルを汎用化しているのに..

Tag

レビュー

著者

Tagging

2009年11月7日土曜日

Page 159: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

これはいやだ

•BooksTagsController•AuthorsTagsController•ReviewsTagsController•etc...

2009年11月7日土曜日

Page 160: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

こうしたい

BooksController

AuthorsController

ReviewsController

TagService

2009年11月7日土曜日

Page 161: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

Railsでのやり方

•routes.rbをゴリゴリ書くmap.book_tags '/books/:id/tags', :controller => 'books', :action => 'tags', :conditions => {:method => :get}

2009年11月7日土曜日

Page 162: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

ほかに方法はないの?

2009年11月7日土曜日

Page 163: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

map.resourcesのオプションの限界

map.resources :books, :member => {:tags => :get}

•ルート名が tags_book になる•動詞のアクションをそのままURLに含める妥協案向き (lock など)

2009年11月7日土曜日

Page 164: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

map.resourcesのオプションの限界(2)

map.resources :books, :member => {:destroy_tag => :delete}

•ルート名が destroy_tag_bookに•URLが /books/destroy_tag?tag_id = 3 に

2009年11月7日土曜日

Page 165: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

map.resourcesはサブリソース同居のコントローラを助けてくれない

2009年11月7日土曜日

Page 166: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

仕方なくゴリゴリ

•たくさんのコントローラで同じような定義•DRYじゃない•さわりたくないroutes.rb•ゴミの山

2009年11月7日土曜日

Page 167: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

大変

2009年11月7日土曜日

Page 168: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

プラグイン作りました

そこで

2009年11月7日土曜日

Page 170: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

こう書けます

map.resources :books, :sub_resources => :tags

2009年11月7日土曜日

Page 171: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

オプションつきmap.resources :books, :sub_resources => { :tags => { :only => [:index, :delete] } }

2009年11月7日土曜日

Page 172: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

複数リソースのupdate等も

スマートにマッピングURL HTTPメソッド アクション

/books/edit GET edit_all

/books PUT update_all

/books DELETE destroy_all

2009年11月7日土曜日

Page 173: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

サブリソースにも

URL HTTPメソッド アクション

/books/tags/edit GET edit_tags

/books/tags PUT update_tags

/books/tags DELETE destroy_tags

2009年11月7日土曜日

Page 174: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

使ってみてね!http://github.com/nay/

sub_resources

2009年11月7日土曜日

Page 175: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

まとめ

2009年11月7日土曜日

Page 176: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

メンテナンスしやすい

Railsコードを書くために

2009年11月7日土曜日

Page 177: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

今日の話

1.本当は奥が深い検証の話2.RESTfulとの付き合い方

2009年11月7日土曜日

Page 178: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

サイン会ジュンク堂

Ruby会議支店2009年11月7日土曜日

Page 179: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

RailsによるアジャイルWebアプリケーション開発

第三版

2009年11月7日土曜日

Page 180: More Pragmatic Patterns of Ruby on Rails at Kansai Ruby Kaigi #02

株式会社万葉

関西Ruby会議02

ご清聴ありがとうございました[email protected]

nay3

2009年11月7日土曜日