Allow undo votes in comments votes component
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user