From 801347976e62e4cb45ca81a01a6a6edc0ea00e88 Mon Sep 17 00:00:00 2001 From: Skud Date: Fri, 8 Feb 2013 17:24:06 +1100 Subject: [PATCH] Added edit/delete buttons to comment display --- app/models/ability.rb | 1 - app/views/comments/_single.html.haml | 12 +++++++++-- app/views/comments/show.html.haml | 20 ++++++++----------- app/views/posts/_single.html.haml | 2 +- ...30208034248_require_fields_for_comments.rb | 12 +++++------ db/schema.rb | 10 +++++----- spec/views/comments/show.html.haml_spec.rb | 17 ++++++---------- 7 files changed, 36 insertions(+), 38 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index 19c89e177..63f25f328 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -25,7 +25,6 @@ class Ability can :create, Post can :update, Post, :author_id => member.id can :destroy, Post, :author_id => member.id - can :create, Comment can :update, Comment, :author_id => member.id can :destroy, Comment, :author_id => member.id diff --git a/app/views/comments/_single.html.haml b/app/views/comments/_single.html.haml index a568b049f..a44f1f450 100644 --- a/app/views/comments/_single.html.haml +++ b/app/views/comments/_single.html.haml @@ -4,13 +4,21 @@ .span1 = render :partial => "shared/avatar", :locals => { :member => comment.author } .span7 - .post-meta + .comment-meta Posted by = link_to comment.author.login_name, member_path(comment.author) at = comment.created_at - .post-body + .comment-body :markdown #{ strip_tags comment.body } + - if can? :edit, comment or can? :destroy, comment + .comment-actions + - if can? :edit, comment + = link_to 'Edit', edit_comment_path(comment), :class => 'btn' + - if can? :destroy, comment + = link_to 'Delete', comment, method: :delete, | + data: { confirm: 'Are you sure?' }, :class => 'btn' + diff --git a/app/views/comments/show.html.haml b/app/views/comments/show.html.haml index 79b7ff31e..dade3ac57 100644 --- a/app/views/comments/show.html.haml +++ b/app/views/comments/show.html.haml @@ -1,15 +1,11 @@ += content_for :title, @comment.post.subject + %p#notice= notice -%p - %b Post: - = @comment.post_id -%p - %b Author: - = @comment.author_id -%p - %b Body: - = @comment.body += render :partial => "posts/single", :locals => { :post => @comment.post } -= link_to 'Edit', edit_comment_path(@comment) -\| -= link_to 'Back', comments_path +%h2 Showing 1 comment + += render :partial => "single", :locals => { :comment => @comment } + +=link_to "View all comments", post_path(@comment.post) diff --git a/app/views/posts/_single.html.haml b/app/views/posts/_single.html.haml index 99d158406..2040f02be 100644 --- a/app/views/posts/_single.html.haml +++ b/app/views/posts/_single.html.haml @@ -4,7 +4,7 @@ .span1 = render :partial => "shared/avatar", :locals => { :member => post.author } .span7 - - if subject + - if defined?(subject) %h3= link_to strip_tags(post.subject), post .post-meta diff --git a/db/migrate/20130208034248_require_fields_for_comments.rb b/db/migrate/20130208034248_require_fields_for_comments.rb index 74c26b4af..c1adea261 100644 --- a/db/migrate/20130208034248_require_fields_for_comments.rb +++ b/db/migrate/20130208034248_require_fields_for_comments.rb @@ -1,17 +1,17 @@ class RequireFieldsForComments < ActiveRecord::Migration def up change_table :comments do |t| - t.change :post_id, :string, :null => false - t.change :author_id, :string, :null => false - t.change :body, :string, :null => false + t.change :post_id, :integer, :null => false + t.change :author_id, :integer, :null => false + t.change :body, :text, :null => false end end def down change_table :comments do |t| - t.change :post_id, :string, :null => true - t.change :author_id, :string, :null => true - t.change :body, :string, :null => true + t.change :post_id, :integer, :null => true + t.change :author_id, :integer, :null => true + t.change :body, :text, :null => true end end end diff --git a/db/schema.rb b/db/schema.rb index 54eca6ad1..2c8ae9f27 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -14,11 +14,11 @@ ActiveRecord::Schema.define(:version => 20130208034248) do create_table "comments", :force => true do |t| - t.string "post_id", :null => false - t.string "author_id", :null => false - t.string "body", :null => false - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.integer "post_id", :limit => 255, :null => false + t.integer "author_id", :limit => 255, :null => false + t.text "body", :limit => 255, :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "crops", :force => true do |t| diff --git a/spec/views/comments/show.html.haml_spec.rb b/spec/views/comments/show.html.haml_spec.rb index 3760f18ff..9c2af0496 100644 --- a/spec/views/comments/show.html.haml_spec.rb +++ b/spec/views/comments/show.html.haml_spec.rb @@ -2,18 +2,13 @@ require 'spec_helper' describe "comments/show" do before(:each) do - @comment = assign(:comment, stub_model(Comment, - :post_id => 1, - :author_id => 2, - :body => "MyText" - )) + controller.stub(:current_user) { nil } + @comment = assign(:comment, FactoryGirl.create(:comment)) + render end - it "renders attributes in

" do - render - # Run the generator again with the --webrat flag if you want to use webrat matchers - rendered.should match(/1/) - rendered.should match(/2/) - rendered.should match(/MyText/) + it "renders the comment" do + rendered.should contain @comment.author.login_name + rendered.should contain @comment.body end end