15. 7 가지 표준 REST 액션들 포스트 삭제 destroy DELETE /posts/1 포스트 갱신 update PUT /posts/1 새 포스트 생성 create POST /posts 포스트 수정 edit GET /posts/1/edit 포스트 생성 new GET /posts/1/new 특정 포스트 show GET /posts/1 포스트 목록 index GET /posts 의미 대응 액션 URI
16. 리소스 표현 (representation) def index @posts = Post.find(:all) respond_to do |format| format.html # index.html.erb format.xml { render :xml => @posts } end end Desktop App. RIA (Flex ..) ActiveResource Mobile (iPhone..) HTML XML JSON . . .
17. One ‘ 리소스’ , Multi Use http://localhost:3000/posts http://localhost:3000/posts.xml http://localhost:3000/posts.json http://localhost:3000/posts.iphone . . .
19. ( 코딩하는데 ) 얼마나 걸리나요 ? class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user end $ script/generate model User $ script/generate scaffold Post $ rake db:migrate user posts
20.
21. 액티브레코드와 OR/M class Post < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :post end t.string "title" t.text "body" t.datetime "created_at" t.datetime "updated_at" t.integer "user_id" Post t.integer "post_id" t.text "body" t.string "author" t.datetime "created_at" t.datetime "updated_at" Comment *
22.
23. 액티브레코드 마이그레이션 class CreatePosts < ActiveRecord::Migration def self.up create_table :posts do |t| t.string :title t.text :body t.datetime :created_at t.datetime :updated_at t.integer :user_id end end def self.down drop_table :posts end end db/migrate/001_create_posts.rb
30. BDD(Behavior Driven Development) $ script/spec -fs specodelsost_spec.rb Post - title 은 반드시 입력해야 한다 - title 은 중복되어서는 안된다 - title 은 5 자 이상이어야 한다 (PENDING: Not Yet Implemented) - body 는 최대 1,000 자까지 쓸 수 있다 (PENDING: Not Yet Implemented) Pending: Post title 은 5 자 이상이어야 한다 (Not Yet Implemented) Post body 는 최대 1,000 자까지 쓸 수 있다 (Not Yet Implemented) Finished in 0.731 seconds 4 examples, 0 failures, 2 pending
31. RSpec describe Post do before(:each) do @post = Post.new end it "title 은 반드시 입력해야 한다 " do @post.save.should be_false end it "title 은 중복되어서는 안된다 " do @post.title = 'Original Title' @post.save @new_post = Post.new(:title => 'Original Title') @new_post.save.should_not be_true end it "title 은 5 자 이상이어야 한다 " it "body 는 최대 1,000 자까지 쓸 수 있다 " end Behavior Example
34. 스펙 먼저 ! require File.dirname(__FILE__) + '/../spec_helper' describe User do before(:each) do @user = User.create(:name => ' 김석준 ') end it “ 여러 명의 친구를 가진다 " do kim = User.create(:name => ' 김철수 ') hong = User.create(:name => ' 홍길동 ') @user.friends << kim @user.friends << hong @user.should have(2).friends end end