diff --git a/app/assets/javascripts/comments.js b/app/assets/javascripts/comments.js index a71e5689a..de354e25e 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 (!$("#a-edit").length) { this.initialize_edit_links(); @@ -98,6 +99,10 @@ } }); } + + Danbooru.Comment.initialize_vote_links = function() { + $(".unvote-comment-link").hide(); + } })(); $(document).ready(function() { diff --git a/app/assets/javascripts/posts.js b/app/assets/javascripts/posts.js index d466151e5..7fdbb7b89 100644 --- a/app/assets/javascripts/posts.js +++ b/app/assets/javascripts/posts.js @@ -222,6 +222,8 @@ e.preventDefault(); }); + + $(".unvote-post-link").hide(); } Danbooru.Post.initialize_post_relationship_previews = 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/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 7b995bcc2..3a1f320fe 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -86,6 +86,13 @@ class PostsController < ApplicationController render :nothing => true end + def unvote + @post = Post.find(params[:id]) + @post.unvote! + rescue PostVote::Error => x + @error = x + end + private def tag_query params[:tags] || (params[:post] && params[:post][:tags]) diff --git a/app/models/comment.rb b/app/models/comment.rb index 40a04c32e..45029b1c6 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/models/post.rb b/app/models/post.rb index d84def7b1..a039fff35 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -652,6 +652,24 @@ class Post < ActiveRecord::Base raise PostVote::Error.new("You have already voted for this post") end end + + def unvote! + if can_be_voted_by?(CurrentUser.user) + raise PostVote::Error.new("You have not voted for this post") + else + vote = votes.where("user_id = ?", CurrentUser.user.id).first + + if vote.score == 1 + Post.update_all("score = score - 1, up_score = up_score - 1", {:id => id}) + self.score -= 1 + else + Post.update_all("score = score + 1, down_score = down_score + 1", {:id => id}) + self.score += 1 + end + + vote.destroy + end + end end module CountMethods 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 a72285e99..603203681 100644 --- a/app/views/comments/partials/show/_comment.html.erb +++ b/app/views/comments/partials/show/_comment.html.erb @@ -24,6 +24,7 @@ <% end %>