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 @@
-<%= form_tag("/comments/#{story.short_id}", { :id => "comment_form" }) do |f| %> +<%= form_tag((comment.new_record? ? "/comments/post_to/#{story.short_id}" : +"/comments/#{comment.short_id}/update"), { :id => "comment_form" }) do |f| %> <% if comment.parent_comment_short_id.present? %> <%= hidden_field_tag "parent_comment_short_id", comment.parent_comment_short_id %> @@ -16,8 +17,8 @@ Markdown formatting available
- <%= button_tag "Post Comment", :class => "comment-post", - :type => "button" %> + <%= button_tag "#{comment.new_record?? "Post" : "Update"} Comment", + :class => "comment-post", :type => "button" %>   <%= button_tag "Preview Comment", :class => "comment-preview", :type => "button" %> diff --git a/config/routes.rb b/config/routes.rb index e6e2851..2bf6e9c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -29,14 +29,18 @@ Lobsters::Application.routes.draw do post "undelete" end post "/stories/fetch_url_title" => "stories#fetch_url_title" - + resources :comments do post "upvote" post "downvote" post "unvote" + + post "edit" + post "preview" + post "update" end - post "/comments/:story_id" => "comments#create" - post "/comments/preview/:story_id" => "comments#preview" + post "/comments/post_to/:story_id" => "comments#create" + post "/comments/preview_to/:story_id" => "comments#preview_new" resources :messages do post "keep_as_new"