Allow undo votes in comments votes component

This commit is contained in:
taitus
2023-09-20 16:10:36 +02:00
parent f87a332c3e
commit 718fcba6d8
16 changed files with 101 additions and 36 deletions

View File

@@ -18,6 +18,28 @@ class Comments::VotesComponent < ApplicationComponent
end
def vote_in_favor_against_path(value)
comment_votes_path(comment, value: value)
if user_already_voted_with(value)
vote = comment.votes_for.find_by!(voter: current_user)
comment_vote_path(comment, vote, value: value)
else
comment_votes_path(comment, value: value)
end
end
def user_already_voted_with(value)
current_user&.voted_as_when_voted_for(comment) == parse_vote(value)
end
def parse_vote(value)
value == "yes" ? true : false
end
def remote_submit(value)
if user_already_voted_with(value)
can?(:destroy, comment.votes_for.new(voter: current_user))
else
can?(:create, comment.votes_for.new(voter: current_user))
end
end
end