Skip to content

Commit 5a2496f

Browse files
committed
Merge branch 'main' into user-pages-show-articles-and-comments
2 parents f247871 + 206fc52 commit 5a2496f

File tree

6 files changed

+79
-5
lines changed

6 files changed

+79
-5
lines changed

app/controllers/articles_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def index
66

77
def show
88
@article = Article.find(params[:id])
9-
@comments = @article.comments.order(created_at: :desc).limit(20)
9+
@comments = @article.comments.order(created_at: :desc)
1010
@comment = Comment.new
1111
end
1212

app/controllers/comments_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class CommentsController < ApplicationController
2-
before_action :authorized, only: [:create]
2+
before_action :authorized
33

44
def create
55
@article = Article.find(params[:article_id])
@@ -9,6 +9,7 @@ def create
99
if @comment.save
1010
redirect_to @article
1111
else
12+
@comments = @article.comments.order(created_at: :desc)
1213
render "articles/show", status: :unprocessable_entity
1314
end
1415
end

config/routes.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
Rails.application.routes.draw do
22
root "articles#index"
33
resources :articles do
4-
resources :comments, only: [:create, :index]
4+
resources :comments, only: [:create]
55
end
6-
resources :users
7-
resources :articles
86
resources :users, only: [:create, :show]
97
get "/signup", to: "users#new"
108
get "/login", to: "sessions#new"

db/seeds.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# Clear existing records
1111
User.destroy_all
1212
Article.destroy_all
13+
Comment.destroy_all
1314

1415
# Create users
1516
users = [
@@ -37,3 +38,14 @@
3738
user: users.sample
3839
)
3940
end
41+
42+
articles = Article.all
43+
44+
# Create comments
45+
90.times do |i|
46+
Comment.create!(
47+
text: "Sample Comment #{i + 1}",
48+
article: articles.sample,
49+
user: users.sample
50+
)
51+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require "test_helper"
2+
3+
class CommentsControllerTest < ActionDispatch::IntegrationTest
4+
def setup
5+
@user = create(:user)
6+
@article = create(:article, user: @user)
7+
@comment_params = {text: "This is a test comment"}
8+
end
9+
test "should not create comment when not logged in" do
10+
assert_no_difference "Comment.count" do
11+
post article_comments_path(@article), params: {comment: @comment_params}
12+
end
13+
assert_redirected_to login_path
14+
end
15+
16+
describe "when logged in" do
17+
before do
18+
@user = create(:user) # There are errors if I don't add this line. didn't need this in articles_controller_test
19+
log_in_as(@user)
20+
end
21+
22+
it "should create comment when logged in" do
23+
assert_difference("Comment.count", 1) do
24+
post article_comments_path(@article), params: {comment: @comment_params}
25+
end
26+
assert_redirected_to @article
27+
end
28+
it "should not create comment with invalid params" do
29+
assert_no_difference "Comment.count" do
30+
post article_comments_path(@article), params: {comment: {text: ""}}
31+
end
32+
assert_response :unprocessable_entity
33+
end
34+
end
35+
end

test/models/comment_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require "test_helper"
2+
3+
class CommentTest < ActiveSupport::TestCase
4+
def setup
5+
@user = create(:user)
6+
@article = create(:article, user: @user)
7+
@comment = Comment.new(text: "This is a test comment", article: @article, user: @user)
8+
end
9+
10+
test "should be valid" do
11+
assert @comment.valid?
12+
end
13+
14+
test "text should be present" do
15+
@comment.text = " "
16+
assert_not @comment.valid?
17+
end
18+
19+
test "should belong to an article" do
20+
@comment.article = nil
21+
assert_not @comment.valid?
22+
end
23+
24+
test "should belong to a user" do
25+
@comment.user = nil
26+
assert_not @comment.valid?
27+
end
28+
end

0 commit comments

Comments
 (0)