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 + } } 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 ed0eeb5..3aae6ba 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -6,6 +6,8 @@ def index end def show + @comment = @post.comments.new + @comments = @post.comments end def new 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) diff --git a/app/views/posts/show.html.slim b/app/views/posts/show.html.slim index ef118c5..5581e6e 100644 --- a/app/views/posts/show.html.slim +++ b/app/views/posts/show.html.slim @@ -14,6 +14,21 @@ | #{@post.created_by.nickname} .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 + .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 = 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" 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