merge branch changeable-votes

This commit is contained in:
Toks
2013-06-29 15:12:38 -04:00
15 changed files with 124 additions and 18 deletions

View File

@@ -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() {

View File

@@ -222,6 +222,8 @@
e.preventDefault();
});
$(".unvote-post-link").hide();
}
Danbooru.Post.initialize_post_relationship_previews = function() {

View File

@@ -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])

View File

@@ -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])

View File

@@ -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

View File

@@ -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

View File

@@ -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();

View File

@@ -24,6 +24,7 @@
<% end %>
<li id="comment-vote-up-link-for-<%= comment.id %>"><%= link_to "Vote up", comment_votes_path(:comment_id => comment.id, :score => "up"), :method => :post, :remote => true %></li>
<li id="comment-vote-down-link-for-<%= comment.id %>"><%= link_to "Vote down", comment_votes_path(:comment_id => comment.id, :score => "down"), :method => :post, :remote => true %></li>
<li id="comment-unvote-link-for-<%= comment.id %>" class="unvote-comment-link"><%= link_to "Unvote", unvote_comment_path(comment.id), :method => :put, :remote => true %></li>
<% end %>
</menu>
<% if comment.editable_by?(CurrentUser.user) %>

View File

@@ -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 %>

View File

@@ -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();

View File

@@ -13,7 +13,7 @@
</li>
<li>Source: <%= post_source_tag(post) %></li>
<li>Rating: <%= post.pretty_rating %></li>
<li>Score: <span id="score-for-post-<%= post.id %>"><%= post.score %></span> <% 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 %></li>
<li>Score: <span id="score-for-post-<%= post.id %>"><%= post.score %></span> <% if CurrentUser.is_gold? %>(<span id="vote-links-for-post-<%= post.id %>">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 %></span><%= link_to "unvote", unvote_post_path(post), :remote => true, :method => :put, :id => "unvote-link-for-post-#{post.id}", :class => "unvote-post-link" %>)<% end %></li>
<li>Favorites: <span id="favcount-for-post-<%= post.id %>"><%= post.fav_count %></span>
<% if CurrentUser.is_gold? %>
<%= link_to "&raquo;".html_safe, "#", :id => "show-favlist-link" %>

View File

@@ -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();

View File

@@ -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

View File

@@ -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")

View File

@@ -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