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 %> + <% end %> <% if comment.editable_by?(CurrentUser.user) %> 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/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 8e3a3a5a1..ef2be95ed 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -81,6 +81,9 @@ Danbooru::Application.routes.draw do get :search get :index_all end + member do + put :unvote + end end resources :counts do collection do @@ -155,6 +158,7 @@ Danbooru::Application.routes.draw do put :revert put :copy_notes get :show_seq + put :unvote end end resources :post_appeals diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index af7a84392..2fbd67a16 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -102,6 +102,19 @@ class CommentTest < ActiveSupport::TestCase assert_equal(2, CommentVote.count) end + should "allow undoing of votes" do + user = FactoryGirl.create(:user) + post = FactoryGirl.create(:post) + comment = FactoryGirl.create(:comment, :post => post) + CurrentUser.scoped(user, "127.0.0.1") do + comment.vote!("up") + comment.unvote! + comment.reload + assert_equal(0, comment.score) + assert_nothing_raised {comment.vote!("down")} + end + end + should "be searchable" do c1 = FactoryGirl.create(:comment, :body => "aaa bbb ccc") c2 = FactoryGirl.create(:comment, :body => "aaa ddd") diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index a6143ff22..3935d77f8 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -1138,6 +1138,18 @@ class PostTest < ActiveSupport::TestCase assert_equal(1, post.score) end end + + should "allow undoing of votes" do + user = FactoryGirl.create(:user) + post = FactoryGirl.create(:post) + CurrentUser.scoped(user, "127.0.0.1") do + post.vote!("up") + post.unvote! + post.reload + assert_equal(0, post.score) + assert_nothing_raised {post.vote!("down")} + end + end end context "Counting:" do