diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..bff074e --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -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 diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb new file mode 100644 index 0000000..0c53467 --- /dev/null +++ b/app/controllers/posts_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index c5476d5..3ec0642 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb new file mode 100644 index 0000000..1f07689 --- /dev/null +++ b/spec/controllers/comments_controller_spec.rb @@ -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 diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb new file mode 100644 index 0000000..61c7e00 --- /dev/null +++ b/spec/controllers/posts_controller_spec.rb @@ -0,0 +1,96 @@ +require 'spec_helper' + +describe PostsController do + + let(:post_create) { create(:post) } + let(:valid_attributes) { attributes_for(:post) } + let(:invalid_attributes) { attributes_for(:post, title: nil, content: nil) } + + describe 'GET #index' do + it "> 모든 Post로 instance 변수를 할당한다." do + posts = [post_create, create(:post)] + get :index + expect(assigns(:posts)).to match_array(posts) + end + end + + describe 'GET #show' do + it "> 요청한 Post로 instance 변수를 할당한다." do + get :show, id: post_create + expect(assigns(:post)).to eq post_create + end + end + + describe 'POST #create' do + context "1) params가 유효할 때" do + it "> 새로운 Post를 생성한다." do + expect { + post :create, post: valid_attributes + }.to change(Post, :count).by(1) + end + + it "> 새로운 Post로 instance 변수를 할당한다." do + post :create, post: valid_attributes + expect(assigns(:post)).to be_a(Post) + expect(assigns(:post)).to_not be_new_record + end + end + + context "2) params가 유효하지 않을 때" do + it "> 새로운 Post를 생성하지 않는다." do + expect { + post :create, post: invalid_attributes + }.to_not change(Post, :count) + end + + it "> 새로운 Post를 instance 변수에 할당한다." do + post :create, post: invalid_attributes + expect(assigns(:post)).to be_a(Post) + expect(assigns(:post)).to be_new_record + end + end + end + + describe 'PATCH #update' do + context "1) params가 유효할 때" do + it "> 요청한 post를 업데이트한다." do + patch :update, id: post_create, post: attributes_for(:post, title: "Wonderfulday isn't it?", content: "Yes!!!") + post_create.reload + expect(post_create.title).to eq("Wonderfulday isn't it?") + expect(post_create.content).to eq("Yes!!!") + end + + it "> 요청한 post를 instance 변수에 할당한다." do + patch :update, id: post_create, post: valid_attributes + post_create.reload + expect(assigns(:post)).to eq post_create + end + end + + context "2) params가 유효하지 않을 때" do + it "> 요청한 post를 업데이트하지 않는다." do + patch :update, id: post_create, post: invalid_attributes + post_create.reload + expect(post_create.title).to eq("post title") + expect(post_create.content).to eq("post content") + end + + it "> 요청한 post를 instance 변수에 할당한다." do + patch :update, id: post_create, post: invalid_attributes + post_create.reload + expect(assigns(:post)).to eq post_create + end + end + + end + + describe 'DELETE #destroy' do + it "> 요청한 post를 삭제한다." do + post_create + expect { + delete :destroy, id: post_create + }.to change(Post, :count).by(-1) + end + end + +end diff --git a/spec/factories/posts.rb b/spec/factories/posts.rb index 04e71ef..75523de 100644 --- a/spec/factories/posts.rb +++ b/spec/factories/posts.rb @@ -19,8 +19,8 @@ FactoryGirl.define do factory :post do - title "title" - content "content" + title "post title" + content "post content" # app/models.post.rb에 after_create 선언 해주었습니다. # after(:create) do |post|