From cf20a9656936529755932d810f1a82b162111f1d Mon Sep 17 00:00:00 2001 From: Rob Nagle Date: Sat, 3 Apr 2010 22:15:48 -0700 Subject: [PATCH] Ran scaffold for comments. --- app/controllers/comments_controller.rb | 85 ++++++++++++++++++++ app/helpers/comments_helper.rb | 2 + app/models/comment.rb | 2 + app/views/comments/edit.html.erb | 24 ++++++ app/views/comments/index.html.erb | 24 ++++++ app/views/comments/new.html.erb | 23 ++++++ app/views/comments/show.html.erb | 18 +++++ app/views/layouts/comments.html.erb | 17 ++++ config/routes.rb | 4 + db/migrate/20100404051540_create_comments.rb | 15 ++++ test/fixtures/comments.yml | 11 +++ test/functional/comments_controller_test.rb | 45 +++++++++++ test/unit/comment_test.rb | 8 ++ test/unit/helpers/comments_helper_test.rb | 4 + 14 files changed, 282 insertions(+) create mode 100644 app/controllers/comments_controller.rb create mode 100644 app/helpers/comments_helper.rb create mode 100644 app/models/comment.rb create mode 100644 app/views/comments/edit.html.erb create mode 100644 app/views/comments/index.html.erb create mode 100644 app/views/comments/new.html.erb create mode 100644 app/views/comments/show.html.erb create mode 100644 app/views/layouts/comments.html.erb create mode 100644 db/migrate/20100404051540_create_comments.rb create mode 100644 test/fixtures/comments.yml create mode 100644 test/functional/comments_controller_test.rb create mode 100644 test/unit/comment_test.rb create mode 100644 test/unit/helpers/comments_helper_test.rb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..92acb07 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -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 diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 0000000..0ec9ca5 --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,2 @@ +module CommentsHelper +end diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..45b2d38 --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,2 @@ +class Comment < ActiveRecord::Base +end diff --git a/app/views/comments/edit.html.erb b/app/views/comments/edit.html.erb new file mode 100644 index 0000000..62aa765 --- /dev/null +++ b/app/views/comments/edit.html.erb @@ -0,0 +1,24 @@ +

Editing comment

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

+ <%= f.label :content %>
+ <%= f.text_area :content %> +

+

+ <%= f.label :link_id %>
+ <%= f.text_field :link_id %> +

+

+ <%= f.label :user_id %>
+ <%= f.text_field :user_id %> +

+

+ <%= f.submit 'Update' %> +

+<% end %> + +<%= link_to 'Show', @comment %> | +<%= link_to 'Back', comments_path %> \ No newline at end of file diff --git a/app/views/comments/index.html.erb b/app/views/comments/index.html.erb new file mode 100644 index 0000000..6b7cd63 --- /dev/null +++ b/app/views/comments/index.html.erb @@ -0,0 +1,24 @@ +

Listing comments

+ + + + + + + + +<% @comments.each do |comment| %> + + + + + + + + +<% end %> +
ContentLinkUser
<%=h comment.content %><%=h comment.link_id %><%=h comment.user_id %><%= link_to 'Show', comment %><%= link_to 'Edit', edit_comment_path(comment) %><%= link_to 'Destroy', comment, :confirm => 'Are you sure?', :method => :delete %>
+ +
+ +<%= link_to 'New comment', new_comment_path %> \ No newline at end of file diff --git a/app/views/comments/new.html.erb b/app/views/comments/new.html.erb new file mode 100644 index 0000000..2d5a7d1 --- /dev/null +++ b/app/views/comments/new.html.erb @@ -0,0 +1,23 @@ +

New comment

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

+ <%= f.label :content %>
+ <%= f.text_area :content %> +

+

+ <%= f.label :link_id %>
+ <%= f.text_field :link_id %> +

+

+ <%= f.label :user_id %>
+ <%= f.text_field :user_id %> +

+

+ <%= f.submit 'Create' %> +

+<% end %> + +<%= link_to 'Back', comments_path %> \ No newline at end of file diff --git a/app/views/comments/show.html.erb b/app/views/comments/show.html.erb new file mode 100644 index 0000000..d0dfa53 --- /dev/null +++ b/app/views/comments/show.html.erb @@ -0,0 +1,18 @@ +

+ Content: + <%=h @comment.content %> +

+ +

+ Link: + <%=h @comment.link_id %> +

+ +

+ User: + <%=h @comment.user_id %> +

+ + +<%= link_to 'Edit', edit_comment_path(@comment) %> | +<%= link_to 'Back', comments_path %> \ No newline at end of file diff --git a/app/views/layouts/comments.html.erb b/app/views/layouts/comments.html.erb new file mode 100644 index 0000000..41d104d --- /dev/null +++ b/app/views/layouts/comments.html.erb @@ -0,0 +1,17 @@ + + + + + + Comments: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + diff --git a/config/routes.rb b/config/routes.rb index 41be3ba..dcde6f9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,8 @@ ActionController::Routing::Routes.draw do |map| + map.resources :comments + + map.resources :shares + map.resources :shares map.resources :links diff --git a/db/migrate/20100404051540_create_comments.rb b/db/migrate/20100404051540_create_comments.rb new file mode 100644 index 0000000..2c48691 --- /dev/null +++ b/db/migrate/20100404051540_create_comments.rb @@ -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 diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml new file mode 100644 index 0000000..608d73e --- /dev/null +++ b/test/fixtures/comments.yml @@ -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 diff --git a/test/functional/comments_controller_test.rb b/test/functional/comments_controller_test.rb new file mode 100644 index 0000000..03d4af8 --- /dev/null +++ b/test/functional/comments_controller_test.rb @@ -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 diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb new file mode 100644 index 0000000..47dee99 --- /dev/null +++ b/test/unit/comment_test.rb @@ -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 diff --git a/test/unit/helpers/comments_helper_test.rb b/test/unit/helpers/comments_helper_test.rb new file mode 100644 index 0000000..2518c16 --- /dev/null +++ b/test/unit/helpers/comments_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class CommentsHelperTest < ActionView::TestCase +end