Make it so that when a user removes their own vote, the vote is soft deleted (the is_deleted flag is set) instead of hard deleted. Changes: * Add is_deleted flag to comment votes. * Relax uniqueness constraint so you can have multiple deleted votes on the same comment. You can still only have one active vote on the comment. * Add `soft_delete` method to Deletable concern.
36 lines
1.2 KiB
Ruby
36 lines
1.2 KiB
Ruby
class CommentVotesController < ApplicationController
|
|
respond_to :js, :json, :xml, :html
|
|
|
|
def index
|
|
@comment_votes = authorize CommentVote.visible(CurrentUser.user).paginated_search(params, count_pages: true)
|
|
@comment_votes = @comment_votes.includes(:user, comment: [:creator, post: [:uploader]]) if request.format.html?
|
|
|
|
respond_with(@comment_votes)
|
|
end
|
|
|
|
def create
|
|
@comment = Comment.find(params[:comment_id])
|
|
|
|
@comment.with_lock do
|
|
@comment_vote = authorize CommentVote.new(comment: @comment, score: params[:score], user: CurrentUser.user)
|
|
|
|
CommentVote.active.where(comment: @comment, user: CurrentUser.user).each do |vote|
|
|
vote.soft_delete!(updater: CurrentUser.user)
|
|
end
|
|
|
|
@comment_vote.save
|
|
end
|
|
|
|
flash.now[:notice] = @comment_vote.errors.full_messages.join("; ") if @comment_vote.errors.present?
|
|
respond_with(@comment_vote)
|
|
end
|
|
|
|
def destroy
|
|
# XXX should find by comment vote id.
|
|
@comment_vote = authorize CommentVote.active.find_by!(comment_id: params[:comment_id], user: CurrentUser.user)
|
|
@comment_vote.soft_delete(updater: CurrentUser.user)
|
|
|
|
respond_with(@comment_vote)
|
|
end
|
|
end
|