Skip to content

Commit

Permalink
Merge pull request #82 from ShinJaehun/master
Browse files Browse the repository at this point in the history
Posts Controller, Comments Controller 및 Spec 작성
  • Loading branch information
rorlab committed May 26, 2014
2 parents cf1867c + 0354ed5 commit 8dc3afb
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 45 deletions.
46 changes: 46 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class CommentsController < ApplicationController

skip_before_filter :authenticate_user!

before_action :set_post
before_action :set_comment, only: [:show, :update, :destroy]

def index
@comments = Comment.all
render json: @comments
end

def show
render json: @comment
end

def create
@comment = @post.comments.create(comment_params)
render json: @comment

end

def update
@comment.update(comment_params)
render json: @comment
end

def destroy
@comment.destroy
render json: { message: "destoryed" }
end

private
def set_post
@post = Post.find(params[:post_id])
end

def set_comment
@comment = @post.comments.find(params[:id])
end

def comment_params
params.require(:comment).permit(:content)
end

end
40 changes: 40 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class PostsController < ApplicationController

skip_before_filter :authenticate_user!

before_action :set_post, only: [:show, :update, :destroy]

def index
@posts = Post.all
render json: @posts
end

def show
render json: @post
end

def create
@post = Post.create(post_params)
render json: @post
end

def update
@post.update(post_params)
render json: @post
end

def destroy
@post.destroy
render json: { message: "destroyed" }
end

private
def set_post
@post = Post.find(params[:id])
end

def post_params
params.require(:post).permit(:title, :content)
end

end
92 changes: 49 additions & 43 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,63 @@
end


# You can have the root of your site routed with "root"
# root 'welcome#index'
concern :commentable do
resources :comments, only: [ :index, :show, :create, :update, :destroy ]
end

resources :posts, only: [ :index, :show, :create, :update, :destroy ], concerns: :commentable

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of regular route:
# get 'products/:id' => 'catalog#view'

# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products

# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end

# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end

# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end

# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable

# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end

end
end
129 changes: 129 additions & 0 deletions spec/controllers/comments_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
require 'spec_helper'

describe CommentsController do

let(:post_create) { create(:post) }
let(:comment) { create(:comment_to_post, commentable: post_create) }
let(:valid_attributes) { attributes_for(:comment) }
let(:invalid_attributes) { attributes_for(:comment, content: nil) }

describe "GET #index" do
it "> 모든 Comment들을 @comments에 할당한다." do
comments = [comment, create(:comment_to_post, commentable: post_create)]
get :index, post_id: post_create.id
expect(assigns(:post)).to eq(post_create)
expect(assigns(:comments)).to match_array(comments)
end
end

describe "GET #show" do
it "> 요청한 Comment로 instance 변수를 할당한다." do
get :show, post_id: post_create.id, id: comment
expect(assigns(:post)).to eq(post_create)
expect(assigns(:comment)).to eq comment
end
end

describe "POST #create" do
context "1) params가 유효할 때" do
def do_post
post :create, post_id: post_create.id, comment: valid_attributes
end

it "> 새로운 Comment를 생성한다." do
expect {
do_post
}.to change(Comment, :count).by(1)
end

it "> Comment instance 변수를 할당한다." do
do_post
expect(assigns(:comment)).to be_a(Comment)
expect(assigns(:comment)).to_not be_new_record
end
end

context "2) params가 유효하지 않을 때" do
def do_post
post :create, post_id: post_create.id, comment: invalid_attributes
end

it "> 새로운 Comment를 생성하지 않는다." do
expect{
do_post
}.to_not change(Comment, :count)
end

it "> 생성하지 못한 Comment로 instance 변수를 할당한다." do
do_post
expect(assigns(:comment)).to be_a(Comment)
expect(assigns(:comment)).to be_new_record
end
end

end

describe 'PATCH #update' do
context "1) params가 유효할 때" do
def do_patch
patch :update, post_id: post_create.id, id: comment,
comment: attributes_for(:comment, content: "Answer content changed")
end

it "> 요청한 Comment를 업데이트한다." do
do_patch
comment.reload
expect(comment.content).to eq "Answer content changed"
end

it "> 요청한 Post와 Comment로 instance 변수를 할당한다." do
do_patch
comment.reload
expect(assigns(:post)).to eq(post_create)
expect(assigns(:comment)).to eq comment
end
end

context "2) params가 유효하지 않을 때" do
def do_patch
patch :update, post_id: post_create.id, id: comment, comment: invalid_attributes
end

it "> 요청한 Comment를 업데이트하지 않는다." do
do_patch
comment.reload
expect(comment.content).to eq "abc edf"
end

it "> 요청한 Post와 Comment로 instance 변수를 할당한다." do
do_patch
comment.reload
expect(assigns(:post)).to eq(post_create)
expect(assigns(:comment)).to eq comment
end
end
end

describe 'DELETE #destroy' do
before(:each) do
@comment = create(:comment, commentable: post_create)
end

def do_delete
delete :destroy, post_id: post_create.id, id: @comment
end

it "> 요청한 Comment를 삭제한다." do
expect {
do_delete
}.to change(Comment, :count).by(-1)
end

it "> 요청한 Post와 삭제된 Comment로 instance 변수를 할당한다." do
do_delete
expect(assigns(:post)).to eq(post_create)
expect(assigns(:comment)).to eq(@comment)
end
end

end
Loading

0 comments on commit 8dc3afb

Please sign in to comment.