Skip to content

Commit 206fc52

Browse files
Merge pull request #17 from vigetlabs/comments
Comments
2 parents cdf2308 + dabd384 commit 206fc52

File tree

12 files changed

+150
-3
lines changed

12 files changed

+150
-3
lines changed

app/controllers/articles_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ def index
66

77
def show
88
@article = Article.find(params[:id])
9+
@comments = @article.comments.order(created_at: :desc)
10+
@comment = Comment.new
911
end
1012

1113
def new
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class CommentsController < ApplicationController
2+
before_action :authorized
3+
4+
def create
5+
@article = Article.find(params[:article_id])
6+
@comment = @article.comments.new(comment_params)
7+
@comment.user = current_user
8+
9+
if @comment.save
10+
redirect_to @article
11+
else
12+
@comments = @article.comments.order(created_at: :desc)
13+
render "articles/show", status: :unprocessable_entity
14+
end
15+
end
16+
17+
private
18+
19+
def comment_params
20+
params.require(:comment).permit(:text)
21+
end
22+
end

app/models/article.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class Article < ApplicationRecord
99
belongs_to :user
10+
has_many :comments
1011
validates :title, presence: true
1112
validates :link, presence: true
1213
end

app/models/comment.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Comment < ApplicationRecord
2+
belongs_to :article
3+
belongs_to :user
4+
validates :text, presence: true
5+
end

app/models/user.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class User < ApplicationRecord
22
has_secure_password
33
has_many :articles
4+
has_many :comments
45

56
validates :name, presence: true, uniqueness: true
67
validates :email, presence: true, uniqueness: true, format: {with: URI::MailTo::EMAIL_REGEXP, message: "must be a valid email address"}

app/views/articles/show.html.erb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
<h1><%= @article.title %></h1>
22
<p><%= link_to @article.link, @article.link %></p>
3-
<%= link_to 'Back to Home', root_path, class: 'btn btn-primary' %>
3+
4+
<%= form_with(model: [@article, @comment], local: true) do |form| %>
5+
<div class="field">
6+
<%= form.text_area :text %>
7+
</div>
8+
<div class="actions">
9+
<%= form.submit %>
10+
</div>
11+
<% end %>
12+
13+
<h2>Comments</h2>
14+
<ul>
15+
<% @comments.each do |comment| %>
16+
<li>
17+
<%= comment.text %> - Posted by <%= comment.user.name %> on <%= comment.created_at.strftime("%B %d, %Y at %I:%M %p") %>
18+
</li>
19+
<% end %>
20+
</ul>

config/routes.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Rails.application.routes.draw do
22
root "articles#index"
3-
resources :articles
3+
resources :articles do
4+
resources :comments, only: [:create]
5+
end
46
resources :users, only: [:create, :show]
57
get "/signup", to: "users#new"
68
get "/login", to: "sessions#new"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateComments < ActiveRecord::Migration[7.1]
2+
def change
3+
create_table :comments do |t|
4+
t.string :text, null: false
5+
t.references :article, null: false, foreign_key: true
6+
t.references :user, null: false, foreign_key: true
7+
t.timestamps
8+
end
9+
end
10+
end

db/schema.rb

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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)