Skip to content

Commit

Permalink
Ran scaffold for comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
oneforwonder committed Apr 4, 2010
1 parent 58a654b commit cf20a96
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 0 deletions.
85 changes: 85 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
class CommentsController < ApplicationController
# GET /comments
# GET /comments.xml
def index
@comments = Comment.all

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @comments }
end
end

# GET /comments/1
# GET /comments/1.xml
def show
@comment = Comment.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @comment }
end
end

# GET /comments/new
# GET /comments/new.xml
def new
@comment = Comment.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @comment }
end
end

# GET /comments/1/edit
def edit
@comment = Comment.find(params[:id])
end

# POST /comments
# POST /comments.xml
def create
@comment = Comment.new(params[:comment])

respond_to do |format|
if @comment.save
flash[:notice] = 'Comment was successfully created.'
format.html { redirect_to(@comment) }
format.xml { render :xml => @comment, :status => :created, :location => @comment }
else
format.html { render :action => "new" }
format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
end
end
end

# PUT /comments/1
# PUT /comments/1.xml
def update
@comment = Comment.find(params[:id])

respond_to do |format|
if @comment.update_attributes(params[:comment])
flash[:notice] = 'Comment was successfully updated.'
format.html { redirect_to(@comment) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /comments/1
# DELETE /comments/1.xml
def destroy
@comment = Comment.find(params[:id])
@comment.destroy

respond_to do |format|
format.html { redirect_to(comments_url) }
format.xml { head :ok }
end
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
2 changes: 2 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Comment < ActiveRecord::Base
end
24 changes: 24 additions & 0 deletions app/views/comments/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<h1>Editing comment</h1>

<% form_for(@comment) do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :content %><br />
<%= f.text_area :content %>
</p>
<p>
<%= f.label :link_id %><br />
<%= f.text_field :link_id %>
</p>
<p>
<%= f.label :user_id %><br />
<%= f.text_field :user_id %>
</p>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>

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

<table>
<tr>
<th>Content</th>
<th>Link</th>
<th>User</th>
</tr>

<% @comments.each do |comment| %>
<tr>
<td><%=h comment.content %></td>
<td><%=h comment.link_id %></td>
<td><%=h comment.user_id %></td>
<td><%= link_to 'Show', comment %></td>
<td><%= link_to 'Edit', edit_comment_path(comment) %></td>
<td><%= link_to 'Destroy', comment, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New comment', new_comment_path %>
23 changes: 23 additions & 0 deletions app/views/comments/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h1>New comment</h1>

<% form_for(@comment) do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :content %><br />
<%= f.text_area :content %>
</p>
<p>
<%= f.label :link_id %><br />
<%= f.text_field :link_id %>
</p>
<p>
<%= f.label :user_id %><br />
<%= f.text_field :user_id %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>

<%= link_to 'Back', comments_path %>
18 changes: 18 additions & 0 deletions app/views/comments/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<p>
<b>Content:</b>
<%=h @comment.content %>
</p>

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

<p>
<b>User:</b>
<%=h @comment.user_id %>
</p>


<%= link_to 'Edit', edit_comment_path(@comment) %> |
<%= link_to 'Back', comments_path %>
17 changes: 17 additions & 0 deletions app/views/layouts/comments.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>Comments: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
</head>
<body>

<p style="color: green"><%= flash[:notice] %></p>

<%= yield %>

</body>
</html>
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
ActionController::Routing::Routes.draw do |map|
map.resources :comments

map.resources :shares

map.resources :shares

map.resources :links
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/20100404051540_create_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreateComments < ActiveRecord::Migration
def self.up
create_table :comments do |t|
t.text :content
t.integer :link_id
t.integer :user_id

t.timestamps
end
end

def self.down
drop_table :comments
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://ar.rubyonrails.org/classes/Fixtures.html

one:
content: MyText
link_id: 1
user_id: 1

two:
content: MyText
link_id: 1
user_id: 1
45 changes: 45 additions & 0 deletions test/functional/comments_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'test_helper'

class CommentsControllerTest < ActionController::TestCase
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 => { }
end

assert_redirected_to comment_path(assigns(:comment))
end

test "should show comment" do
get :show, :id => comments(:one).to_param
assert_response :success
end

test "should get edit" do
get :edit, :id => comments(:one).to_param
assert_response :success
end

test "should update comment" do
put :update, :id => comments(:one).to_param, :comment => { }
assert_redirected_to comment_path(assigns(:comment))
end

test "should destroy comment" do
assert_difference('Comment.count', -1) do
delete :destroy, :id => comments(:one).to_param
end

assert_redirected_to comments_path
end
end
8 changes: 8 additions & 0 deletions test/unit/comment_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'test_helper'

class CommentTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
4 changes: 4 additions & 0 deletions test/unit/helpers/comments_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class CommentsHelperTest < ActionView::TestCase
end

0 comments on commit cf20a96

Please sign in to comment.