diff --git a/app/assets/javascripts/application.js.erb b/app/assets/javascripts/application.js.erb index 9d12b7a..0870009 100644 --- a/app/assets/javascripts/application.js.erb +++ b/app/assets/javascripts/application.js.erb @@ -145,8 +145,8 @@ var _Lobsters = Class.extend({ }, previewComment: function(form) { - $(form).parent().load($(form).attr("action").replace(/^\/comments/, - "/comments/preview"), $(form).serializeArray()); + $(form).parent().load($(form).attr("action").replace(/(post|update)/, + "preview"), $(form).serializeArray()); }, fetchURLTitle: function(button, url_field, title_field) { @@ -224,6 +224,12 @@ $(document).ready(function() { return false; }); + $("a.comment_editor").live("click", function() { + var li = $(this).parents("li.comment").first(); + li.load("/comments/" + $(li).attr("data-shortid") + "/edit", + { "edit": 1 }); + }); + $("#story_tags_a").select2({ formatSelection: function(what) { return what.id; diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index f60f229..881c63c 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -49,12 +49,63 @@ class CommentsController < ApplicationController :comment => comment, :show_comment => comment } end end - - def preview + + def preview_new params[:preview] = true return create end + def edit + if !((comment = Comment.find_by_short_id(params[:comment_id])) && + comment.is_editable_by_user?(@user)) + return render :text => "can't find comment", :status => 400 + end + + render :partial => "commentbox", :layout => false, + :content_type => "text/html", :locals => { :story => comment.story, + :comment => comment } + end + + def update + if !((comment = Comment.find_by_short_id(params[:comment_id])) && + comment.is_editable_by_user?(@user)) + return render :text => "can't find comment", :status => 400 + end + + comment.comment = params[:comment] + + if comment.save + # TODO: render the comment again properly, it's indented wrong + + render :partial => "postedreply", :layout => false, + :content_type => "text/html", :locals => { :story => comment.story, + :show_comment => comment } + else + comment.previewing = true + comment.current_vote = { :vote => 1 } + + render :partial => "commentbox", :layout => false, + :content_type => "text/html", :locals => { :story => comment.story, + :comment => comment, :show_comment => comment } + end + end + + def preview + if !((comment = Comment.find_by_short_id(params[:comment_id])) && + comment.is_editable_by_user?(@user)) + return render :text => "can't find comment", :status => 400 + end + + comment.comment = params[:comment] + + comment.previewing = true + comment.current_vote = { :vote => 1 } + + render :partial => "commentbox", :layout => false, + :content_type => "text/html", :locals => { :story => comment.story, + :comment => comment, :show_comment => comment } + end + def unvote if !(comment = Comment.find_by_short_id(params[:comment_id])) return render :text => "can't find comment", :status => 400 diff --git a/app/views/comments/_commentbox.html.erb b/app/views/comments/_commentbox.html.erb index 929d2bc..a49225b 100644 --- a/app/views/comments/_commentbox.html.erb +++ b/app/views/comments/_commentbox.html.erb @@ -1,5 +1,6 @@