Fix #4931: Add popup voter list for comments.
Show the comment's upvote and downvote count when you hover over a comment's score. For mods, show the list of voters as well.
This commit is contained in:
32
app/components/comment_votes_tooltip_component.rb
Normal file
32
app/components/comment_votes_tooltip_component.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
# This component represents the tooltip that displays when you hover over a comment's score.
|
||||
class CommentVotesTooltipComponent < ApplicationComponent
|
||||
attr_reader :comment, :current_user
|
||||
delegate :upvote_icon, :downvote_icon, to: :helpers
|
||||
|
||||
def initialize(comment:, current_user:)
|
||||
super
|
||||
@comment = comment
|
||||
@current_user = current_user
|
||||
end
|
||||
|
||||
def votes
|
||||
comment.votes.active.includes(:user).order(id: :desc)
|
||||
end
|
||||
|
||||
def upvote_count
|
||||
votes.select(&:is_positive?).length
|
||||
end
|
||||
|
||||
def downvote_count
|
||||
votes.select(&:is_negative?).length
|
||||
end
|
||||
|
||||
def upvote_ratio
|
||||
return nil if votes.length == 0
|
||||
sprintf("(%.1f%%)", 100.0 * upvote_count / votes.length)
|
||||
end
|
||||
|
||||
def vote_icon(vote)
|
||||
vote.is_positive? ? upvote_icon : downvote_icon
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user