diff --git a/app/assets/javascripts/posts.js b/app/assets/javascripts/posts.js index 1b8fd5557..0545d030e 100644 --- a/app/assets/javascripts/posts.js +++ b/app/assets/javascripts/posts.js @@ -143,6 +143,8 @@ e.preventDefault(); }); + + $(".unvote-post-link").hide(); } Danbooru.Post.initialize_post_relationship_previews = function() { diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index cb4acf487..da8d673f3 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -82,6 +82,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/post.rb b/app/models/post.rb index 56d02473e..50dc7adda 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -650,6 +650,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/post_votes/create.js.erb b/app/views/post_votes/create.js.erb index 0483156a4..bfeb6a784 100644 --- a/app/views/post_votes/create.js.erb +++ b/app/views/post_votes/create.js.erb @@ -4,3 +4,5 @@ Danbooru.notice("Vote saved"); $("#score-for-post-<%= @post.id %>").html(<%= @post.score %>); <% end %> +$("#vote-links-for-post-<%= @post.id %>").hide(); +$("#unvote-link-for-post-<%= @post.id %>").show(); \ No newline at end of file diff --git a/app/views/posts/partials/show/_information.html.erb b/app/views/posts/partials/show/_information.html.erb index df06402a1..7f58ebb8f 100644 --- a/app/views/posts/partials/show/_information.html.erb +++ b/app/views/posts/partials/show/_information.html.erb @@ -13,7 +13,7 @@