-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
278 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module CommentsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |