From d3739489d945b1b9f15153a50e85b1e86f056ae8 Mon Sep 17 00:00:00 2001 From: Yuka Kato Date: Fri, 26 Aug 2016 18:37:06 +0900 Subject: [PATCH 1/6] Associate Post and User with Comment --- app/models/comment.rb | 4 ++++ app/models/post.rb | 1 + app/models/user.rb | 1 + 3 files changed, 6 insertions(+) create mode 100644 app/models/comment.rb diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..fba6813 --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,4 @@ +class Comment < ApplicationRecord + belongs_to :post + belongs_to :commented_by, class_name: "User" +end diff --git a/app/models/post.rb b/app/models/post.rb index 960cb9c..fb2f04a 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1,5 +1,6 @@ class Post < ApplicationRecord belongs_to :created_by, class_name: "User" + has_many :comments, dependent: :destroy validates :title, presence: true diff --git a/app/models/user.rb b/app/models/user.rb index 7aa2fe7..923d793 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,6 +2,7 @@ class User < ActiveRecord::Base include Authenticator has_many :posts, foreign_key: "created_by_id" + has_many :comments, foreign_key: "commented_by_id" class << self def find_or_create_from(auth) From ca42d98afc58796e67f2543166c395c6e45617a8 Mon Sep 17 00:00:00 2001 From: Yuka Kato Date: Fri, 2 Sep 2016 16:00:12 +0900 Subject: [PATCH 2/6] Render comments but it is not cool --- app/controllers/posts_controller.rb | 1 + app/views/posts/show.html.slim | 5 +++++ config/routes.rb | 4 +++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index ed0eeb5..120030b 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -6,6 +6,7 @@ def index end def show + @comments = @post.comments end def new diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index ef118c5..388fd4d 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -14,6 +14,11 @@ | #{@post.created_by.nickname} .author__created-at.grey-text.text-darken-1 = @post.created_at + + - @comments.each do |comment| + = comment.commented_by.nickname + == sanitize(auto_link(markdown_to_html(emojify(comment.body)))) + .row .col.s12 = link_to edit_post_path(@post), class: 'waves-effect waves-light btn' do diff --git a/config/routes.rb b/config/routes.rb index 0eaae9a..9773f6f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,9 @@ Rails.application.routes.draw do root to: "posts#index" resources :users, only: %i(index show) - resources :posts + resources :posts do + resources :comments, only: %i(index new create) + end get "login", to: "sessions#new" get "auth/:provider/callback", to: "sessions#create" From bf32b8a4f2fd9dac4fe9ea5eda6362816602b78f Mon Sep 17 00:00:00 2001 From: Yuka Kato Date: Fri, 2 Sep 2016 16:07:00 +0900 Subject: [PATCH 3/6] Apply CSS class --- app/views/posts/show.html.slim | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index 388fd4d..6a067f2 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -16,8 +16,14 @@ = @post.created_at - @comments.each do |comment| - = comment.commented_by.nickname - == sanitize(auto_link(markdown_to_html(emojify(comment.body)))) + .comment + .author + .author__nickname + = comment.commented_by.nickname + .author__created-at + = comment.created_at + .comment__body + == sanitize(auto_link(markdown_to_html(emojify(comment.body)))) .row .col.s12 From 22f7c26891f362b86affa69c0b4d699250aa515d Mon Sep 17 00:00:00 2001 From: Yuka Kato Date: Fri, 2 Sep 2016 17:13:30 +0900 Subject: [PATCH 4/6] Css classes for post author are nested under .post class, because I wanna create classes for comment author --- app/assets/stylesheets/posts.scss | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/app/assets/stylesheets/posts.scss b/app/assets/stylesheets/posts.scss index ed37598..8c425f7 100644 --- a/app/assets/stylesheets/posts.scss +++ b/app/assets/stylesheets/posts.scss @@ -77,18 +77,18 @@ ol { @extend %post-list-common; } -} -.author { - border-top: 1px solid $border-gray; - padding: 1.5rem 0; - text-align: right; -} + .author { + border-top: 1px solid $border-gray; + padding: 1.5rem 0; + text-align: right; + } -.author__nickname { - font-size: .9rem -} + .author__nickname { + font-size: .9rem + } -.author__created-at { - font-size: .8rem + .author__created-at { + font-size: .8rem + } } From 4faec029b0c39d73bbb946928b3e744e77e54084 Mon Sep 17 00:00:00 2001 From: Yuka Kato Date: Fri, 2 Sep 2016 17:14:52 +0900 Subject: [PATCH 5/6] [ci skip][WIP]Create comments --- app/controllers/comments_controller.rb | 22 ++++++++++++++++++++++ app/controllers/posts_controller.rb | 1 + app/views/posts/show.html.slim | 4 ++++ 3 files changed, 27 insertions(+) create mode 100644 app/controllers/comments_controller.rb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..1a46c4b --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,22 @@ +class CommentsController < ApplicationController + def index + @comments = Comment.all + end + + def create + post = Post.find(params[:post_id]) + @comment = post.comments.build(comment_params) + + if @comment.save + redirect_to post, notice: "Comment has been successfully created." + else + redirect_to post, alert: "Hoge!" + end + end + + private + + def comment_params + params.require(:comment).permit(:body).merge(commented_by: current_user) + end +end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 120030b..3aae6ba 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -6,6 +6,7 @@ def index end def show + @comment = @post.comments.new @comments = @post.comments end diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index 6a067f2..5581e6e 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -15,6 +15,10 @@ .author__created-at.grey-text.text-darken-1 = @post.created_at + = form_for @comment, url: post_comments_path(@post) do |f| + = f.text_field :body + = f.submit + - @comments.each do |comment| .comment .author From 09335e16be4efb11a8203418e1fc1aefd81d1246 Mon Sep 17 00:00:00 2001 From: Yuka Kato Date: Mon, 5 Sep 2016 12:41:14 +0900 Subject: [PATCH 6/6] [WIP][ci skip]Create feature --- test/features/comment_creation_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 test/features/comment_creation_test.rb diff --git a/test/features/comment_creation_test.rb b/test/features/comment_creation_test.rb new file mode 100644 index 0000000..b69844d --- /dev/null +++ b/test/features/comment_creation_test.rb @@ -0,0 +1,18 @@ +require "test_helper" + +feature "CommentCreation" do + scenario "User create comment to post" do + stub_authentication_with "alice" + + visit post_path(post) + + body = "Lorem ipsum dolor sit amet" + + fill_in "comment_body", with: body + click_button "create comment" + + within ".comment__body" do + expect(page).must_have_content "Lorem ipsum dolor sit amet" + end + end +end