use standard create/update comment routes

This commit is contained in:
Serge Paquet 2014-01-15 22:55:31 -05:00
parent 42ce1e3b4c
commit 79b64c3883
5 changed files with 26 additions and 41 deletions

View file

@ -147,8 +147,9 @@ var _Lobsters = Class.extend({
},
previewComment: function(form) {
var action = $(form).attr("action").replace(/(post|update)/, "preview");
$.post(action, $(form).serializeArray(), function(data) {
var params = $(form).serializeArray();
params.push({"name": "preview", "value": "true"});
$.post($(form).attr("action"), params, function(data) {
$(form).closest('.comment').replaceWith($.parseHTML(data));
});
},

View file

@ -2,7 +2,7 @@ class CommentsController < ApplicationController
COMMENTS_PER_PAGE = 20
before_filter :require_logged_in_user_or_400,
:only => [ :create, :preview, :preview_new, :upvote, :downvote, :unvote ]
:only => [ :create, :preview, :upvote, :downvote, :unvote ]
# for rss feeds, load the user's tag filters if a token is passed
before_filter :find_user_from_rss_token, :only => [ :index ]
@ -28,7 +28,7 @@ class CommentsController < ApplicationController
end
# prevent double-clicks of the post button
if !params[:preview].present? &&
if params[:preview].blank? &&
(pc = Comment.where(:story_id => story.id, :user_id => @user.id,
:parent_comment_id => comment.parent_comment_id).first)
if (Time.now - pc.created_at) < 5.minutes
@ -40,27 +40,19 @@ class CommentsController < ApplicationController
end
end
if comment.valid? && !params[:preview].present? && comment.save
if comment.valid? && params[:preview].blank? && comment.save
comment.current_vote = { :vote => 1 }
render :partial => "comments/postedreply", :layout => false,
:content_type => "text/html", :locals => { :comment => comment }
else
comment.previewing = true
comment.upvotes = 1
comment.current_vote = { :vote => 1 }
render :partial => "commentbox", :layout => false,
:content_type => "text/html", :locals => {
:comment => comment, :show_comment => comment }
preview comment
end
end
def preview_new
params[:preview] = true
return create
end
def edit
if !((comment = find_comment) && comment.is_editable_by_user?(@user))
return render :text => "can't find comment", :status => 400
@ -113,7 +105,7 @@ class CommentsController < ApplicationController
comment.comment = params[:comment]
if comment.save
if params[:preview].blank? && comment.save
votes = Vote.comment_votes_by_user_for_comment_ids_hash(@user.id,
[comment.id])
comment.current_vote = votes[comment.id]
@ -121,30 +113,12 @@ class CommentsController < ApplicationController
render :partial => "comments/comment", :layout => false,
:content_type => "text/html", :locals => { :comment => comment }
else
comment.previewing = true
comment.current_vote = { :vote => 1 }
render :partial => "commentbox", :layout => false,
:content_type => "text/html", :locals => {
:comment => comment, :show_comment => comment }
preview comment
end
end
def preview
if !((comment = find_comment) && 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 => {
:comment => comment, :show_comment => comment }
end
def unvote
if !(comment = find_comment)
return render :text => "can't find comment", :status => 400
@ -276,6 +250,15 @@ class CommentsController < ApplicationController
private
def preview(comment)
comment.previewing = true
comment.is_deleted = false # show normal preview for deleted comments
render :partial => "comments/commentbox", :layout => false,
:content_type => "text/html", :locals => {
:comment => comment, :show_comment => comment }
end
def find_comment
Comment.where(:short_id => params[:id]).first
end

View file

@ -38,6 +38,10 @@ class Comment < ActiveRecord::Base
errors.add(:base, (m[1] == "T" ? "N" : "n") + "ope" + m[2].to_s)
end
def to_param
self.short_id
end
def self.regenerate_markdown
Comment.record_timestamps = false

View file

@ -1,11 +1,12 @@
<div class="comment comment_form_container">
<%= form_tag comment.new_record? ?
"/comments/post_to/#{comment.story.short_id}" :
"/comments/#{comment.short_id}/update" do |f| %>
<%= form_for comment do |f| %>
<% if comment.errors.any? %>
<%= errors_for comment %>
<% end %>
<%= hidden_field_tag "story_id",
comment.story.short_id %>
<% if comment.parent_comment %>
<%= hidden_field_tag "parent_comment_short_id",
comment.parent_comment.short_id %>

View file

@ -51,15 +51,11 @@ Lobsters::Application.routes.draw do
post "downvote"
post "unvote"
post "preview"
post "update"
post "delete"
post "undelete"
end
end
get "/comments/page/:page" => "comments#index"
post "/comments/post_to/:story_id" => "comments#create"
post "/comments/preview_to/:story_id" => "comments#preview_new"
get "/messages/sent" => "messages#sent"
resources :messages do