Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] コメントをつけられるようにする [closes #35] #64

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions app/assets/stylesheets/posts.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
22 changes: 22 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ def index
end

def show
@comment = @post.comments.new
@comments = @post.comments
end

def new
Expand Down
4 changes: 4 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Comment < ApplicationRecord
belongs_to :post
belongs_to :commented_by, class_name: "User"
end
1 change: 1 addition & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Post < ApplicationRecord
belongs_to :created_by, class_name: "User"
has_many :comments, dependent: :destroy

validates :title, presence: true

Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions app/views/posts/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
18 changes: 18 additions & 0 deletions test/features/comment_creation_test.rb
Original file line number Diff line number Diff line change
@@ -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