From 13995cfd391f441d03aa24285d347b3a0a5e0fc2 Mon Sep 17 00:00:00 2001
From: albert
Date: Tue, 13 Sep 2011 18:41:50 -0400
Subject: [PATCH] fixed comment quoting
---
app/assets/javascripts/comments.js | 25 +++++++++++++++++++
app/controllers/comments_controller.rb | 7 ++++++
app/models/comment.rb | 4 +++
.../comments/partials/show/_comment.html.erb | 4 +--
4 files changed, 38 insertions(+), 2 deletions(-)
diff --git a/app/assets/javascripts/comments.js b/app/assets/javascripts/comments.js
index 4e5e71486..2de43bab1 100644
--- a/app/assets/javascripts/comments.js
+++ b/app/assets/javascripts/comments.js
@@ -6,6 +6,31 @@
this.initialize_response_link();
this.initialize_preview_button();
this.hide_threshold_comments();
+ this.initialize_reply_links();
+ }
+
+ Danbooru.Comment.quote_message = function(data) {
+ var stripped_body = data["body"].replace(/\[quote\](?:.|\n|\r)+?\[\/quote\](?:\r\n|\r|\n)*/gm, "");
+ return "[quote]\n" + data["creator_name"] + " said:\n" + stripped_body + "\n[/quote]\n\n";
+ }
+
+ Danbooru.Comment.quote = function(e) {
+ $.get(
+ "/comments/" + $(e.target).data('comment-id') + ".json",
+ function(data) {
+ var $link = $(e.target);
+ var $div = $link.closest("div.comments-for-post");
+ var $textarea = $div.find("textarea")
+ $textarea.val(Danbooru.Comment.quote_message(data));
+ $div.find("a.expand-comment-response").trigger("click");
+ $textarea.focus();
+ }
+ );
+ e.preventDefault();
+ }
+
+ Danbooru.Comment.initialize_reply_links = function() {
+ $(".reply-link").click(Danbooru.Comment.quote);
}
Danbooru.Comment.initialize_response_link = function() {
diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb
index 20100c6cb..b7a33c00c 100644
--- a/app/controllers/comments_controller.rb
+++ b/app/controllers/comments_controller.rb
@@ -36,6 +36,13 @@ class CommentsController < ApplicationController
respond_with(@comment)
end
+ def show
+ @comment = Comment.find(params[:id])
+ respond_with(@comment) do |format|
+ format.json {render :json => @comment.to_json(:methods => [:creator_name])}
+ end
+ end
+
private
def index_for_post
@post = Post.find(params[:post_id])
diff --git a/app/models/comment.rb b/app/models/comment.rb
index 9dfff6fcb..6bd4f0c80 100644
--- a/app/models/comment.rb
+++ b/app/models/comment.rb
@@ -48,6 +48,10 @@ class Comment < ActiveRecord::Base
decrement!(:score)
end
end
+
+ def creator_name
+ creator.name
+ end
end
Comment.connection.extend(PostgresExtensions)
diff --git a/app/views/comments/partials/show/_comment.html.erb b/app/views/comments/partials/show/_comment.html.erb
index ddd8a7d0a..91454a0a5 100644
--- a/app/views/comments/partials/show/_comment.html.erb
+++ b/app/views/comments/partials/show/_comment.html.erb
@@ -6,11 +6,11 @@
-
+
<%= format_text(comment.body) %>