fixes #1263 for comments
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
this.initialize_response_link();
|
||||
this.initialize_reply_links();
|
||||
this.initialize_expand_links();
|
||||
this.initialize_vote_links();
|
||||
}
|
||||
|
||||
if ($("#c-posts").length && $("#a-show").length) {
|
||||
@@ -84,6 +85,10 @@
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Danbooru.Comment.initialize_vote_links = function() {
|
||||
$(".unvote-comment-link").hide();
|
||||
}
|
||||
})();
|
||||
|
||||
$(document).ready(function() {
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
@@ -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>
|
||||
<% end %>
|
||||
|
||||
7
app/views/comments/unvote.js.erb
Normal file
7
app/views/comments/unvote.js.erb
Normal 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 %>
|
||||
@@ -80,6 +80,9 @@ Danbooru::Application.routes.draw do
|
||||
get :search
|
||||
get :index_all
|
||||
end
|
||||
member do
|
||||
put :unvote
|
||||
end
|
||||
end
|
||||
resources :counts do
|
||||
collection do
|
||||
|
||||
Reference in New Issue
Block a user