Skip to content

Commit

Permalink
Add Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
eko24 committed Aug 31, 2015
1 parent 37d6d69 commit d3c8e1c
Show file tree
Hide file tree
Showing 22 changed files with 278 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'devise', '~> 3.4.0'
gem 'bootstrap-sass', '~> 3.3.5.1'
gem 'acts_as_votable', '~> 0.10.0'
gem 'acts_as_votable', '~> 0.10.0'
gem 'simple_form', '~> 3.1.1'
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ GEM
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
simple_form (3.1.1)
actionpack (~> 4.0)
activemodel (~> 4.0)
sprockets (3.3.3)
rack (~> 1.0)
sprockets-rails (2.3.2)
Expand Down Expand Up @@ -176,6 +179,7 @@ DEPENDENCIES
rails (= 4.2.3)
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
simple_form (~> 3.1.1)
sqlite3
turbolinks
tzinfo-data
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/comments.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
43 changes: 43 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]


# POST /comments
# POST /comments.json
def create
@link = Link.find(params[:link_id])
@comment = @link.comments.new(comment_params)
@comment.user = current_user

respond_to do |format|
if @comment.save
format.html { redirect_to @link, notice: 'Comment was successfully created.' }
format.json { render json: @comment, status: :created, location: @comment }
else
format.html { render action: "new" }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end

# DELETE /comments/1
# DELETE /comments/1.json
def destroy
@comment.destroy
respond_to do |format|
format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:link_id, :body, :user_id)
end
end
2 changes: 2 additions & 0 deletions app/helpers/comments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CommentsHelper
end
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 < ActiveRecord::Base
belongs_to :user
belongs_to :link
end
1 change: 1 addition & 0 deletions app/models/link.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Link < ActiveRecord::Base
acts_as_votable
belongs_to :user
has_many :comments
end
14 changes: 14 additions & 0 deletions app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<%= div_for(comment) do %>
<div class="comments_wrapper clearfix">
<div class="pull-left">
<p class="lead"><%= comment.body %></p>
<p><small>Submitted <strong><%= time_ago_in_words(comment.created_at) %> ago</strong> by <%= comment.user.email %></small></p>
</div>

<div class="btn-group pull-right">
<% if comment.user == current_user -%>
<%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-sm btn-default" %>
<% end %>
</div>
</div>
<% end %>
29 changes: 29 additions & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%= form_for(@comment) do |f| %>
<% if @comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

<ul>
<% @comment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :link_id %><br>
<%= f.number_field :link_id %>
</div>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.label :user_id %><br>
<%= f.text_field :user_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/comments/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing Comment</h1>

<%= render 'form' %>

<%= link_to 'Show', @comment %> |
<%= link_to 'Back', comments_path %>
31 changes: 31 additions & 0 deletions app/views/comments/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<p id="notice"><%= notice %></p>

<h1>Listing Comments</h1>

<table>
<thead>
<tr>
<th>Link</th>
<th>Body</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @comments.each do |comment| %>
<tr>
<td><%= comment.link_id %></td>
<td><%= comment.body %></td>
<td><%= comment.user %></td>
<td><%= link_to 'Show', comment %></td>
<td><%= link_to 'Edit', edit_comment_path(comment) %></td>
<td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Comment', new_comment_path %>
4 changes: 4 additions & 0 deletions app/views/comments/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.array!(@comments) do |comment|
json.extract! comment, :id, :link_id, :body, :user_id
json.url comment_url(comment, format: :json)
end
5 changes: 5 additions & 0 deletions app/views/comments/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New Comment</h1>

<%= render 'form' %>

<%= link_to 'Back', comments_path %>
19 changes: 19 additions & 0 deletions app/views/comments/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Link:</strong>
<%= @comment.link_id %>
</p>

<p>
<strong>Body:</strong>
<%= @comment.body %>
</p>

<p>
<strong>User:</strong>
<%= @comment.user %>
</p>

<%= link_to 'Edit', edit_comment_path(@comment) %> |
<%= link_to 'Back', comments_path %>
1 change: 1 addition & 0 deletions app/views/comments/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.extract! @comment, :id, :link_id, :body, :user_id, :created_at, :updated_at
18 changes: 17 additions & 1 deletion app/views/links/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,20 @@
Downvote
<%= @link.get_downvotes.size %>
<% end %>
</div>
</div>

<h3 class="comments_title">
<%= @link.comments.count %> Comments
</h3>

<div id="comments">
<%= render :partial => @link.comments %>
</div>

<%= simple_form_for [@link, Comment.new] do |f| %>
<div class="field">
<%= f.text_area :body, class: "form-control" %>
</div>
<br>
<%= f.submit "Add Comment", class: "btn btn-primary" %>
<% end %>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Rails.application.routes.draw do
resources :comments
devise_for :users

resources :links do
member do
put "like", to: "links#upvote"
put "dislike", to: "links#downvote"
end
resources :comments
end

root 'links#index'
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20150831212900_create_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.integer :link_id
t.text :body
t.references :user, index: true, foreign_key: true

t.timestamps null: false
end
add_index :comments, :link_id
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150831210723) do
ActiveRecord::Schema.define(version: 20150831212900) do

create_table "comments", force: :cascade do |t|
t.integer "link_id"
t.text "body"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "comments", ["link_id"], name: "index_comments_on_link_id"
add_index "comments", ["user_id"], name: "index_comments_on_user_id"

create_table "links", force: :cascade do |t|
t.string "title"
Expand Down
49 changes: 49 additions & 0 deletions test/controllers/comments_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'test_helper'

class CommentsControllerTest < ActionController::TestCase
setup do
@comment = comments(:one)
end

test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:comments)
end

test "should get new" do
get :new
assert_response :success
end

test "should create comment" do
assert_difference('Comment.count') do
post :create, comment: { body: @comment.body, link_id: @comment.link_id, user_id: @comment.user_id }
end

assert_redirected_to comment_path(assigns(:comment))
end

test "should show comment" do
get :show, id: @comment
assert_response :success
end

test "should get edit" do
get :edit, id: @comment
assert_response :success
end

test "should update comment" do
patch :update, id: @comment, comment: { body: @comment.body, link_id: @comment.link_id, user_id: @comment.user_id }
assert_redirected_to comment_path(assigns(:comment))
end

test "should destroy comment" do
assert_difference('Comment.count', -1) do
delete :destroy, id: @comment
end

assert_redirected_to comments_path
end
end
11 changes: 11 additions & 0 deletions test/fixtures/comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
link_id: 1
body: MyText
user_id:

two:
link_id: 1
body: MyText
user_id:
7 changes: 7 additions & 0 deletions test/models/comment_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class CommentTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit d3c8e1c

Please sign in to comment.