comment editing, looks weird for now but it works

This commit is contained in:
joshua stein 2012-07-04 19:55:45 -05:00
parent cd568eaa38
commit f8dd75fde4
4 changed files with 72 additions and 10 deletions

View file

@ -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;

View file

@ -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

View file

@ -1,5 +1,6 @@
<div class="comment_form_container">
<%= 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 @@
<span class="fakea">Markdown formatting available</span>
</div>
<%= button_tag "Post Comment", :class => "comment-post",
:type => "button" %>
<%= button_tag "#{comment.new_record?? "Post" : "Update"} Comment",
:class => "comment-post", :type => "button" %>
&nbsp;
<%= button_tag "Preview Comment", :class => "comment-preview",
:type => "button" %>

View file

@ -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"