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 @@
  • Source: <%= post_source_tag(post) %>
  • Rating: <%= post.pretty_rating %>
  • -
  • Score: <%= post.score %> <% if CurrentUser.is_gold? %>(vote <%= link_to "up", post_votes_path(:post_id => post.id, :score => "up"), :remote => true, :method => :post %>/<%= link_to "down", post_votes_path(:post_id => post.id, :score => "down"), :remote => true, :method => :post %>)<% end %>
  • +
  • Score: <%= post.score %> <% if CurrentUser.is_gold? %>(vote <%= link_to "up", post_votes_path(:post_id => post.id, :score => "up"), :remote => true, :method => :post %>/<%= link_to "down", post_votes_path(:post_id => post.id, :score => "down"), :remote => true, :method => :post %><%= link_to "unvote", unvote_post_path(post), :remote => true, :method => :put, :id => "unvote-link-for-post-#{post.id}", :class => "unvote-post-link" %>)<% end %>
  • Favorites: <%= post.fav_count %> <% if CurrentUser.is_gold? %> <%= link_to "»".html_safe, "#", :id => "show-favlist-link" %> diff --git a/app/views/posts/unvote.js.erb b/app/views/posts/unvote.js.erb new file mode 100644 index 000000000..7b0f62421 --- /dev/null +++ b/app/views/posts/unvote.js.erb @@ -0,0 +1,8 @@ +<% if @error %> + Danbooru.notice("<%= j @error.to_s %>"); +<% else %> + Danbooru.notice("Unvoted successfully"); + $("#score-for-post-<%= @post.id %>").html(<%= @post.score %>); +<% end %> +$("#vote-links-for-post-<%= @post.id %>").show(); +$("#unvote-link-for-post-<%= @post.id %>").hide(); \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 1f3ed4dd0..8243013b2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -154,6 +154,7 @@ Danbooru::Application.routes.draw do put :revert put :copy_notes get :show_seq + put :unvote end end resources :post_appeals