Skip to content
Browse files

Add comment support

  • Loading branch information...
1 parent 9ab762d commit 145e7ddb5cdd4eef9adbbc5792c10f2d14424e54 Marc Lagrange committed Jun 27, 2011
View
16 app/controllers/comments_controller.rb
@@ -0,0 +1,16 @@
+class CommentsController < ApplicationController
+ before_filter :require_user
+
+ def create
+ @comment = Comment.new(params[:comment])
+ @instrument = Instrument.find(params[:instrument_id])
+ @comment.user = current_user
+ @comment.instrument = @instrument
+ if @comment.save
+ flash[:notice] = "saved"
+ redirect_to @instrument
+ else
+ redirect_to @instrument
+ end
+ end
+end
View
2 app/controllers/instruments_controller.rb
@@ -46,6 +46,8 @@ def new
def show
@instrument = Instrument.find(params[:id])
+ @comment = Comment.new
+ @comments = Comment.page(params[:page])
end
def edit
View
2 app/helpers/comments_helper.rb
@@ -0,0 +1,2 @@
+module CommentsHelper
+end
View
7 app/models/comment.rb
@@ -0,0 +1,7 @@
+class Comment < ActiveRecord::Base
+ default_scope :order => 'created_at DESC'
+ belongs_to :instrument
+ belongs_to :user
+ paginates_per 10
+ validates_presence_of :comment
+end
View
1 app/models/instrument.rb
@@ -3,6 +3,7 @@ class Instrument < ActiveRecord::Base
default_scope :order => 'created_at DESC'
belongs_to :user
has_many :table_row
+ has_many :comment
accepts_nested_attributes_for :table_row, :allow_destroy => false
paginates_per 20
has_friendly_id :name, :use_slug => true
View
1 app/models/user.rb
@@ -2,6 +2,7 @@ class User < ActiveRecord::Base
acts_as_authentic
default_scope :order => 'created_at DESC'
has_many :instruments
+ has_many :comments
paginates_per 10
has_friendly_id :login, :use_slug => true
validates_presence_of :login, :email
View
25 app/views/instruments/show.html.haml
@@ -1 +1,24 @@
-= render :partial => "inst_#{@instrument.type.downcase}"
+= render :partial => "inst_#{@instrument.type.downcase}"
+%br
+%br
+- if current_user
+ = form_for [@instrument, @comment] do |f|
+ %p.verysmall.bold= f.label :comment
+ %p= f.text_area :comment, :rows => 5, :cols => 80
+ %p= f.submit "Submit"
+%br
+%br
+- @comments.each do |comment|
+ .comment_block
+ By:
+ = link_to comment.user.login, comment.user
+ %br
+ At:
+ = comment.created_at
+ %br
+ Comment:
+ %br
+ %pre= comment.comment
+ %br
+%br
+= paginate @comments
View
1 config/routes.rb
@@ -2,6 +2,7 @@
resources :users, :user_sessions
resources :instruments do
get "raw"
+ resource :comments
end
resources :tags, :authors
View
15 db/migrate/20110627155838_create_comments.rb
@@ -0,0 +1,15 @@
+class CreateComments < ActiveRecord::Migration
+ def self.up
+ create_table :comments do |t|
+ t.text :comment
+ t.integer :instrument_id
+ t.integer :user_id
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :comments
+ end
+end
View
10 db/schema.rb
@@ -10,7 +10,15 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110627145332) do
+ActiveRecord::Schema.define(:version => 20110627155838) do
+
+ create_table "comments", :force => true do |t|
+ t.text "comment"
+ t.integer "instrument_id"
+ t.integer "user_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
create_table "instruments", :force => true do |t|
t.string "type", :null => false
View
11 test/fixtures/comments.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one: {}
+# column: value
+#
+two: {}
+# column: value
View
8 test/functional/comments_controller_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class CommentsControllerTest < ActionController::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
8 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
View
4 test/unit/helpers/comments_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class CommentsHelperTest < ActionView::TestCase
+end

0 comments on commit 145e7dd

Please sign in to comment.
Something went wrong with that request. Please try again.