pragmatic patterns of ruby on rails - ruby kaigi2009

168
RubyKaigi2009 株式会社万葉 Pragmatic Patterns of Ruby on Rails 2009.7.17 Yasuko OHBA(大場寧子) 現場で役立つ Ruby on Rails パターン

Upload: nay

Post on 19-Jan-2015

10.771 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

Pragmatic Patternsof Ruby on Rails

2009.7.17Yasuko OHBA(大場寧子)

現場で役立つ Ruby on Rails パターン

Page 2: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

自己紹介what I do

• developer of Rails applicationsRailsアプリケーションを開発

•Akasaka.rb• Everyleaf Corporation株式会社万葉

Page 3: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

提供

株式会社万葉Everyleaf Corp.

Page 4: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

スポンサーブースsponsor booth

• first time to be a sponsorはじめてのスポンサー

• we serve candies飴あります

• you can play ‘Romantic Ruby’ !ギャルゲーあります

Page 5: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

作ってるものmy products

• home accounting web serviceWeb家計簿「小槌」http://www.kozuchi.net

• http://github.com/everyleaf/kozuchi/tree

Page 6: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

iCarta

Page 7: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

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

Page 8: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

伝えたいこと

personal message

Page 9: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

中学生くらいからプログラミング

I started programming when I

was 14 years old

Page 10: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

女子校

school for girls

Page 11: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

孤独なプログラマ

I was alone

Page 12: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

就職

got the job

Page 13: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

似た人々と出会う

find similar people

Page 14: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

でも

however

Page 15: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

エンタープライズ

‘enterprise’

Page 16: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

社外に出ない

only activitiesinside the company

Page 17: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

見積もりマネージメントアウトソージング

other things mount up

Page 18: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

どうしたらいいのか

I was lost

Page 19: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

実装がしたい

want to write codes

Page 20: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

そんな時that’s when..

Page 21: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

Rubyに出会った

Ruby was given

Page 22: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

人生変わった(^o^)/

life becomes happier

Page 23: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

Rubyでエンタープライズ

Ruby for enterprises

Page 24: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

実装してて幸せになる道

the way to live happily writing codes

Page 25: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

そして

And...

Page 26: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

素晴らしいコミュニティ

great communities

Page 27: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

仲間ができた

many friends

Page 28: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

ありがとう!thank you !!

Page 29: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

本題

today’s presentation

Page 30: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

実装パターン

coding patternswith

Ruby on Rails

Page 31: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

ターゲットthe target

• large & complicated applications複雑で大きなアプリ

•teamチームでの開発

Page 32: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

大きなアプリの課題

problems of large applications

•different coding styles書き方がバラバラ

•nasty parts made品質が揃っていない

Page 33: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

メンテナンスしにくくなる

becomes harderto maintenance

Page 34: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

コードをいい状態に保つ

keep your codes nice

Page 35: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

いいコードとは?• good designいい設計

• easy to understand読みやすい、わかりやすい

• easy to find something wrong 間違いにすぐ気づく

what is the ‘nice code’?

Page 36: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

そこで、パターン

• make and share coding patterns共通パターンを作る

• add some DSL (not too much)DSLを作る(控えめに)

coding patterns work fine

Page 37: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

もちろん効率も上がる

efficient, of course

Page 38: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

このプレゼンで触れないことwhat I won’t say

• how to get the best performance極限のパフォーマンスの追求

• ActiveRecord’s alternativesAR以外のDB層の選択肢

Page 39: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

O/R Mapper

ActiveRecord

Page 40: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

て言うか、

frankly speaking,

Page 41: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

ActiveRecord大好き

I love ActiveRecord

Page 42: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

なぜなら

because

Page 43: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

オブジェクト指向で書ける

OOP

Page 44: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

複雑さに耐えられる

still able to maintenancewhen it gets complicated

Page 45: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

実用的

pragmatic

Page 46: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

Railsの中核

the heart of RoR

Page 47: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

ActiveRecordを中心に話します

many AR features in this presentation

Page 48: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

実例紹介

examples

Page 49: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

権限のあるデータの表示

show permitted data

要件 ①

Page 50: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

ARオブジェクトから始める検索

find starts froman AR object

パターン ①

Page 51: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

IDでNoteを検索find a note by id

/note/3

def show @note = Note.find(params[:id])end

Page 52: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

ログインユーザーのNoteを検索

find a note of the current user

@note = Note.find_by_id_and_user_id( params[:id], current_user.id)raise ActiveRecord::RecordNotFound unless @note

/note/3

Page 53: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

restful_authentication

current_user?

Page 54: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

named scope?

@note = Note.written_by( curret_user.id).find(params[:id])

/note/3

Page 55: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

条件を忘れても気づきにくい

hard to notice the luck of condition!

@note = Note.find(params[:id])@note = Note.find_by_id_and_user_id( params[:id], current_user.id)@note = Note.written_by( current_user).find(params[:id])

Page 56: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

そこで

so,

Page 57: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

ARオブジェクトから始める検索

find starts froman AR object

pattern ①

Page 58: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

オブジェクト起点で検索

starts from an object

def show @note = current_user.notes.find( params[:id])end

Page 59: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

関連を使う

use the association

def show @note = current_user.notes.find( params[:id])end

Page 60: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

関連

association

class User < ActiveRecord::Base has_many :notesend

Page 61: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

問題に気づきやすい

easy to notice problems

@note = current_user.notes.find( params[:id])

@note = Note.find(params[:id])

Page 62: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

データの権利者が一目でわかる

easy to see who can access

Page 63: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

データの権利者

who can access

@note = current_user.notes.find( params[:id])

Page 64: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

ARオブジェクトを得るFilter

filters to find the starting AR object

pattern ②

Page 65: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

さっきのパターンで書くと

with the previouspattern

Page 66: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

コントローラのアクションの多くの

起点が揃う

most actions willuse the AR object

Page 67: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

例)グループのノートのCRUD

example;CRUD of the specified

group’s notes

Page 68: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

URL

•ノートの帰属するグループがURL

の前に付く• /groups/15/notes• /groups/15/notes/3

Page 69: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

class Note < ActiveRecord::Base belongs_to :groupend

Model• Note has group_idNoteが group_id を持つ

class Group < ActiveRecord::Base has_many :notesend

Page 70: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

Controllerdef index @group = Group.find(params[:group_id]) @notes = @group.notes.paginate(:page => params[:page])enddef show @group = Group.find(params[:group_id]) @note = @group.notes.find(params[:id])end

Page 71: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

重複したコード

the duplicated line

@group = Group.find(params[:group_id])

Page 72: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

Filterで記述する

write that linein a filter

Page 73: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

Filterとは?what’s the filter?

• the separated logic which would be called around actionアクションの前後に行いたい処理をアクションとは別に記述

• declarative宣言的

Page 74: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

class GroupNotesController < ApplicationController

before_filter :find_group .......actions ...... private def find_group @group = Group.find(params[:group_id]) endend

Filterで@groupを得るfind the group in a filter

Page 75: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

重複を避けられるDRY

before_filter :find_groupdef index @notes = @group.notes.paginate(:page => params[:page])enddef show @note = @group.notes.find(params[:id])end

Page 76: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

アクセス制限を変えるのも楽

easy to changeaccess control

Page 77: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

例) メンバーしか見られないようにする

example; change to allow access to members only

def find_group @group = current_user.groups.find( params[:group_id]end

Page 78: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

それだけじゃない

other merits

Page 79: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

securegroupでの

絞り込みを確実に

Page 80: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

@groupで始まれば安心safe if it starts from @group

def index @notes = @group.notes.paginate(:page => params[:page])enddef show @note = @group.notes.find(params[:id])end

Page 81: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

filter urges developersto use @group

Filterがあれば自然な強制力が

働く

Page 82: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

and

さらに

Page 83: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

読みやすい

readable

Page 84: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

コントローラ名とFilterで

概要が分かる

you can understandthe controller’s summary

from name and filters

Page 85: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

さっと把握できる

understand the summaryquickly

class GroupNotesController < ApplicationController before_filter :find_group

Page 86: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

大事なことthe points

•a good controller name良いコントローラ名

•readable filters読みやすいフィルター

Page 87: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

複雑なビジネスロジック

complicatedbusiness logics

要件 ②

Page 88: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

ビジネスロジックはモデルに書きたい

want to writebusiness logics

in models

Page 89: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

なぜなら

because

Page 90: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

モデルに集めると嬉しい点

merits of writing business logics in models

• easy to testテストしやすい

• easy to reuse再利用しやすい

• readable, easy to find the target codesコードが分散せず、読みやすい

Page 91: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

しかし

however

Page 92: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

集め方が問題

‘how’ matters a lot

Page 93: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

悪い例a bad example

class MyModel < ActiveRecord::Base def do_create(params) .... def do_destroy(params) ...end

Page 94: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

どこが悪いか?why is it bad ?

• it breaks MVCMVCを壊している

• hard to reuse再利用性が低い

Page 95: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

そこでnow

Page 96: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

コントローラからモデルへ

うまくコードを移してみましょう

let’s move codesfrom controller to model

in good way

Page 97: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

パラメータによる分岐ロジックの

移動

move the logicbranching on parameters

from C to M

pattern ③

Page 98: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

パラメータによる処理の分岐branching on parameters

def update @note.attributes = params[:note] @tags = params[:auto_tagging] == '1' ? generate_tags(@note) : []

# do saving and tagging ...end

Page 99: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

コントローラに書きがち

often written in controller

Page 100: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

モデルに持っていくには

to move to model

Page 101: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

制御条件をモデルの属性に

add an attribute for branching to the model

Page 102: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

分岐条件

the condition

params[:auto_tagging] == '1'

Page 103: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

モデルの属性にas an attribute of the model

class Note < ActiveRecord::Base attr_accessor :auto_tagging

end

Page 104: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

パラメータ構造を変える

change parameters structure

{ :note => {.....}, :auto_tagging => '1' }

{ :note => { ....., :auto_tagging => '1' }}

Page 105: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

そのためにビューでフィールド名を変更

change the view for it

<%= check_box_tag :auto_tagging %>

<% form_for :note, ... do |f |%> <%= f.check_box :auto_tagging %><% end %>

Page 106: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

params[:note]で渡せるnow it’s in params[:note]

def update @note.attributes = params[:note] if params[:auto_tagging] == '1' generate_tags(@note) end .....

Page 107: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

モデルで分岐できる

can branch in the model

class Note < ActiveRecord::Base .... if auto_tagging ... endend

Page 108: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

分岐は移動できた

finished to movebranching

Page 109: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

残るはgenerate_tagsロジックの移動

move generate_tags next

Page 110: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

付随する他モデルへの処理

の移動

move the logic processing other models

from C to M

pattern ④

Page 111: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

付随する他モデルへの処理def update generate_tags(@note)endprivatedef generate_tags(note) tags = Tag.extract(note.body) note.tag_list = tags.join(',')end

the logic processing other models

Page 112: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

これもコントローラに

書きがち

also often written in controller

Page 113: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

モデルのコールバックへput it into model’s callback

class Note < ActiveRecord::Base before_save :generate_taggings

private def generate_taggings return unless auto_tagging tags = Tag.extract(body) self.tag_list = tags.join(',') endend

Page 114: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

モデルのコールバックとは?

what’s the model’s callback?

• methods called before/after save or destroysaveやdestroyなどの前後に呼び出される処理

Page 115: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

Rails Model You

normal method call普通のメソッド呼び出し

savesave(validation)

before_save

after_save

do saving

Page 116: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

自律的なモデル

self-directive models

Page 117: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

目標を達成!now we’ve done it !

• easy to testテストしやすい

• easy to reuse再利用しやすい

• readable, easy to find the target codesコードが分散せず、読みやすい

Page 118: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

話せなかったことother patterns

• multi levels for validation多レベルの検証

• design & coding policy for STISTIの原則

• use owner object’s attributes in association関連オブジェクトに属性情報を伝える

• how to make routes.rb a little readableroutes.rb の整理

Page 119: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

最後に

the last topic

Page 120: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

Rails実装パターンの見い出し方

how to findcoding patterns

Page 121: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

私の場合

in my case

Page 122: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

Railsの中で自然に感じる書き方の追究

try to choosethe most natural style

for RoR

Page 123: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

Railsにおいて何が自然か

what is the most natural way

in Ruby on Rails?

Page 124: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

1. オブジェクト指向

1. OOP

Page 125: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

ビジネスロジックをモデルとして表現

express business logics as models

Page 126: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

誰のすべき仕事かにこだわる

always think who should do that job

Page 127: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

Note or User ?

@note = Note.find_by_id_and_user_id( params[:id], current_user.id)

@note = current_user.notes.find( params[:id])

Page 128: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

必ずしもテーブルで決めない

you can’t always decide it from tables

Page 129: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

オブジェクトの世界の中で決める

decide it in objects world

Page 130: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

2. 原則に従う

2. follow the principles of RoR

Page 131: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

Railsの原則principles of RoR

•DRY•CoC•RESTful

Page 132: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

RESTful

Page 133: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

RailsではRESTfulから逃げない方がいい

accept RESTfulin Ruby on Rails

Page 134: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

コントローラをどう分けるか?

how to designcontrollers ?

Page 135: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

“RESTful”

Page 136: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

対象リソース1種類に

1つのコントローラ

one controller fora type of resources

Page 137: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

リソースって何だ?

what’s the resources?

Page 138: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

URLで示される

you can find it in URL

/companies/everyleaf/notes/3

Page 139: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

コントローラの設計は

design of controllers

Page 140: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

URL設計から始める

starts from design of URLs

Page 141: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

ざっくり

roughly speaking,

Page 142: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

Resourceリソース

Model Classモデルクラス

1

1..*

Page 143: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

重要なのは

the point is...

Page 144: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

複数リソースのCRUDはまずい

one controller shouldprovide one resource

type’s CRUD

Page 145: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

こういうのはまずいmight have been derailed

class BlogsController < ApplicationController

def create_comment end def comments end def destroy_comment ...

Page 146: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

もうひとつ

one more

Page 147: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

モデルの基本的な流れに乗せる

write codes in model’s standard flows

Page 148: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

RubyKaigi2009

株式会社万葉

standard flows

•find•new → save (create)•find → save (update)•find → destroy

標準的な流れの種類

Page 149: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

流れの中身

inside of a flow

Page 150: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

例) createnewbuild

attribute =属性代入

before_validation

validation検証

after_validation

before_save

creation作成処理 after_save

Page 151: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

適切な場所に書く

write codes inappropriate place

Page 152: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

newbuild

attribute =属性代入

before_validation

validation検証

after_validation

before_save

creation作成処理 after_save

where, what

Page 153: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

Railsの中で自然に感じる書き方の追究

try to choosethe most natural style

for RoR

Page 154: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

基本的な書き方から大きく離れない

don’t go too farfrom the basic styles

Page 155: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

そうすれば

because

Page 156: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

Railsプログラマにとって標準的

it’s normal to Rails programmers

Page 157: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

誰でも読める

easy to readfor everyone

Page 158: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

そのためには

for that purpose

Page 159: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

Rubyの柔軟性を活用する

use Ruby’s flexibility

Page 160: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

まとめ

summary

Page 161: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

複雑で大きなアプリでは

in developinglarge & complicated

application

Page 162: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

コードをいい状態に保つのが大事

it’s importantto keep codes nice

Page 163: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

そのために

for that purpose

Page 164: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

実装パターンを意識して共有しよう

be aware of coding patterns,

and share them

Page 165: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

Railsでの自然な書き方を

目指せば

coding patternsfitting Ruby on Rails

Page 166: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

読める人の多いコードができる

make source codes easy to read for

general developers

Page 167: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

メンテナンスできる

it meanseasy to maintain

Page 168: Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009

株式会社万葉

RubyKaigi2009

ご静聴ありがとうございました

thank you !