From badc3122f03a24b46a0a7c0f8231c5c49a01b956 Mon Sep 17 00:00:00 2001 From: Toks Date: Sat, 29 Jun 2013 14:50:22 -0400 Subject: [PATCH] fixes #1263 for comments --- app/assets/javascripts/comments.js | 5 ++ app/controllers/comments_controller.rb | 7 +++ app/models/comment.rb | 49 +++++++++++++------ app/views/comment_votes/create.js.erb | 5 +- .../comments/partials/show/_comment.html.erb | 1 + app/views/comments/unvote.js.erb | 7 +++ config/routes.rb | 3 ++ 7 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 app/views/comments/unvote.js.erb diff --git a/app/assets/javascripts/comments.js b/app/assets/javascripts/comments.js index 305ea34a6..7d90c32a6 100644 --- a/app/assets/javascripts/comments.js +++ b/app/assets/javascripts/comments.js @@ -6,6 +6,7 @@ this.initialize_response_link(); this.initialize_reply_links(); this.initialize_expand_links(); + this.initialize_vote_links(); } if ($("#c-posts").length && $("#a-show").length) { @@ -84,6 +85,10 @@ } }) } + + Danbooru.Comment.initialize_vote_links = function() { + $(".unvote-comment-link").hide(); + } })(); $(document).ready(function() { diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 6f64a7e3c..2eb7f4629 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -63,6 +63,13 @@ class CommentsController < ApplicationController end end + def unvote + @comment = Comment.find(params[:id]) + @comment.unvote! + rescue CommentVote::Error => x + @error = x + 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 a2973cf22..41c414fad 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -69,7 +69,41 @@ class Comment < ActiveRecord::Base end end + module VoteMethods + def vote!(val) + numerical_score = val == "up" ? 1 : -1 + vote = votes.create(:score => numerical_score) + + if vote.errors.empty? + if vote.is_positive? + update_column(:score, score + 1) + elsif vote.is_negative? + update_column(:score, score - 1) + end + end + + return vote + end + + def unvote! + vote = votes.where("user_id = ?", CurrentUser.user.id).first + + if vote + if vote.is_positive? + update_column(:score, score - 1) + else + update_column(:score, score + 1) + end + + vote.destroy + else + raise CommentVote::Error.new("You have not voted for this comment") + end + end + end + extend SearchMethods + include VoteMethods def initialize_creator self.creator_id = CurrentUser.user.id @@ -122,21 +156,6 @@ class Comment < ActiveRecord::Base do_not_bump_post == "1" end - def vote!(val) - numerical_score = val == "up" ? 1 : -1 - vote = votes.create(:score => numerical_score) - - if vote.errors.empty? - if vote.is_positive? - update_column(:score, score + 1) - elsif vote.is_negative? - update_column(:score, score - 1) - end - end - - return vote - end - def editable_by?(user) creator_id == user.id || user.is_janitor? end diff --git a/app/views/comment_votes/create.js.erb b/app/views/comment_votes/create.js.erb index 37f935818..13f89d0dc 100644 --- a/app/views/comment_votes/create.js.erb +++ b/app/views/comment_votes/create.js.erb @@ -4,6 +4,7 @@ <% if @comment_vote.is_negative? %> $(".comment[data-comment-id=<%= @comment.id %>]").remove(); <% end %> - $("#comment-vote-up-link-for-<%= @comment.id %>").remove(); - $("#comment-vote-down-link-for-<%= @comment.id %>").remove(); <% end %> +$("#comment-vote-up-link-for-<%= @comment.id %>").hide(); +$("#comment-vote-down-link-for-<%= @comment.id %>").hide(); +$("#comment-unvote-link-for-<%= @comment.id %>").show(); \ No newline at end of file diff --git a/app/views/comments/partials/show/_comment.html.erb b/app/views/comments/partials/show/_comment.html.erb index 208dd5b0e..7f558ef69 100644 --- a/app/views/comments/partials/show/_comment.html.erb +++ b/app/views/comments/partials/show/_comment.html.erb @@ -24,6 +24,7 @@ <% end %> + <% end %> <% end %> diff --git a/app/views/comments/unvote.js.erb b/app/views/comments/unvote.js.erb new file mode 100644 index 000000000..754b5568a --- /dev/null +++ b/app/views/comments/unvote.js.erb @@ -0,0 +1,7 @@ +<% if @error %> + Danbooru.error("<%= j @error.to_s %>"); +<% else %> + $("#comment-vote-up-link-for-<%= @comment.id %>").show(); + $("#comment-vote-down-link-for-<%= @comment.id %>").show(); + $("#comment-unvote-link-for-<%= @comment.id %>").hide(); +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 8243013b2..630c6ce5f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -80,6 +80,9 @@ Danbooru::Application.routes.draw do get :search get :index_all end + member do + put :unvote + end end resources :counts do collection do