Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add comments

  • Loading branch information...
commit 73b49fb1b3d248c5067ff60015b29eeaed5788a6 1 parent f6158ab
@barthezslavik authored
View
3  app/assets/javascripts/comments.js.coffee
@@ -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://jashkenas.github.com/coffee-script/
View
3  app/assets/stylesheets/comments.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the Comments controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
69 app/assets/stylesheets/scaffolds.css.scss
@@ -0,0 +1,69 @@
+body {
+ background-color: #fff;
+ color: #333;
+ font-family: verdana, arial, helvetica, sans-serif;
+ font-size: 13px;
+ line-height: 18px;
+}
+
+p, ol, ul, td {
+ font-family: verdana, arial, helvetica, sans-serif;
+ font-size: 13px;
+ line-height: 18px;
+}
+
+pre {
+ background-color: #eee;
+ padding: 10px;
+ font-size: 11px;
+}
+
+a {
+ color: #000;
+ &:visited {
+ color: #666;
+ }
+ &:hover {
+ color: #fff;
+ background-color: #000;
+ }
+}
+
+div {
+ &.field, &.actions {
+ margin-bottom: 10px;
+ }
+}
+
+#notice {
+ color: green;
+}
+
+.field_with_errors {
+ padding: 2px;
+ background-color: red;
+ display: table;
+}
+
+#error_explanation {
+ width: 450px;
+ border: 2px solid red;
+ padding: 7px;
+ padding-bottom: 0;
+ margin-bottom: 20px;
+ background-color: #f0f0f0;
+ h2 {
+ text-align: left;
+ font-weight: bold;
+ padding: 5px 5px 5px 15px;
+ font-size: 12px;
+ margin: -7px;
+ margin-bottom: 0px;
+ background-color: #c00;
+ color: #fff;
+ }
+ ul li {
+ font-size: 12px;
+ list-style: square;
+ }
+}
View
83 app/controllers/comments_controller.rb
@@ -0,0 +1,83 @@
+class CommentsController < ApplicationController
+ # GET /comments
+ # GET /comments.json
+ def index
+ @comments = Comment.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @comments }
+ end
+ end
+
+ # GET /comments/1
+ # GET /comments/1.json
+ def show
+ @comment = Comment.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @comment }
+ end
+ end
+
+ # GET /comments/new
+ # GET /comments/new.json
+ def new
+ @comment = Comment.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @comment }
+ end
+ end
+
+ # GET /comments/1/edit
+ def edit
+ @comment = Comment.find(params[:id])
+ end
+
+ # POST /comments
+ # POST /comments.json
+ def create
+ @comment = Comment.new(params[:comment])
+
+ respond_to do |format|
+ if @comment.save
+ format.html { redirect_to @comment, 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
+
+ # PUT /comments/1
+ # PUT /comments/1.json
+ def update
+ @comment = Comment.find(params[:id])
+
+ respond_to do |format|
+ if @comment.update_attributes(params[:comment])
+ format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @comment.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /comments/1
+ # DELETE /comments/1.json
+ def destroy
+ @comment = Comment.find(params[:id])
+ @comment.destroy
+
+ respond_to do |format|
+ format.html { redirect_to comments_url }
+ format.json { head :no_content }
+ end
+ end
+end
View
2  app/helpers/comments_helper.rb
@@ -0,0 +1,2 @@
+module CommentsHelper
+end
View
3  app/models/comment.rb
@@ -0,0 +1,3 @@
+class Comment < ActiveRecord::Base
+ attr_accessible :commentable_id, :commentable_type, :content
+end
View
10 app/views/comments/_form.html.haml
@@ -0,0 +1,10 @@
+= simple_form_for(@comment) do |f|
+ = f.error_notification
+
+ .inputs
+ = f.input :content
+ = f.input :commentable_id
+ = f.input :commentable_type
+
+ .actions
+ = f.button :submit
View
7 app/views/comments/edit.html.haml
@@ -0,0 +1,7 @@
+%h1 Editing comment
+
+= render 'form'
+
+= link_to 'Show', @comment
+\|
+= link_to 'Back', comments_path
View
23 app/views/comments/index.html.haml
@@ -0,0 +1,23 @@
+%h1 Listing comments
+
+%table
+ %tr
+ %th Content
+ %th Commentable
+ %th Commentable type
+ %th
+ %th
+ %th
+
+ - @comments.each do |comment|
+ %tr
+ %td= comment.content
+ %td= comment.commentable_id
+ %td= comment.commentable_type
+ %td= link_to 'Show', comment
+ %td= link_to 'Edit', edit_comment_path(comment)
+ %td= link_to 'Destroy', comment, :confirm => 'Are you sure?', :method => :delete
+
+%br
+
+= link_to 'New Comment', new_comment_path
View
5 app/views/comments/new.html.haml
@@ -0,0 +1,5 @@
+%h1 New comment
+
+= render 'form'
+
+= link_to 'Back', comments_path
View
15 app/views/comments/show.html.haml
@@ -0,0 +1,15 @@
+%p#notice= notice
+
+%p
+ %b Content:
+ = @comment.content
+%p
+ %b Commentable:
+ = @comment.commentable_id
+%p
+ %b Commentable type:
+ = @comment.commentable_type
+
+= link_to 'Edit', edit_comment_path(@comment)
+\|
+= link_to 'Back', comments_path
View
3  config/routes.rb
@@ -1,4 +1,7 @@
Solution::Application.routes.draw do
+ resources :comments
+
+
devise_for :users
resources :categories
View
11 db/migrate/20121223172203_create_comments.rb
@@ -0,0 +1,11 @@
+class CreateComments < ActiveRecord::Migration
+ def change
+ create_table :comments do |t|
+ t.text :content
+ t.integer :commentable_id
+ t.string :commentable_type
+
+ t.timestamps
+ end
+ end
+end
View
10 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121223164912) do
+ActiveRecord::Schema.define(:version => 20121223172203) do
create_table "categories", :force => true do |t|
t.string "name"
@@ -19,6 +19,14 @@
t.datetime "updated_at", :null => false
end
+ create_table "comments", :force => true do |t|
+ t.text "content"
+ t.integer "commentable_id"
+ t.string "commentable_type"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
create_table "samples", :force => true do |t|
t.string "name"
t.text "description"
View
11 test/fixtures/comments.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ content: MyText
+ commentable_id: 1
+ commentable_type: MyString
+
+two:
+ content: MyText
+ commentable_id: 1
+ commentable_type: MyString
View
49 test/functional/comments_controller_test.rb
@@ -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: { commentable_id: @comment.commentable_id, commentable_type: @comment.commentable_type, content: @comment.content }
+ 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
+ put :update, id: @comment, comment: { commentable_id: @comment.commentable_id, commentable_type: @comment.commentable_type, content: @comment.content }
+ 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
View
7 test/unit/comment_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class CommentTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
4 test/unit/helpers/comments_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class CommentsHelperTest < ActionView::TestCase
+end
Please sign in to comment.
Something went wrong with that request. Please try again.