votes: show votes when hovering over post score.
Make it so you can hover over a post's score to see the list of public upvotes. Also show the upvote count, the downvote count, and the upvote ratio.
This commit is contained in:
34
app/components/post_votes_tooltip_component.rb
Normal file
34
app/components/post_votes_tooltip_component.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# This component represents the tooltip that displays when you hover over a post's score.
|
||||
class PostVotesTooltipComponent < ApplicationComponent
|
||||
attr_reader :post, :current_user
|
||||
delegate :upvote_icon, :downvote_icon, to: :helpers
|
||||
|
||||
def initialize(post:, current_user:)
|
||||
super
|
||||
@post = post
|
||||
@current_user = current_user
|
||||
end
|
||||
|
||||
def votes
|
||||
@post.votes.includes(:user).order(id: :desc)
|
||||
end
|
||||
|
||||
def vote_icon(vote)
|
||||
vote.is_positive? ? upvote_icon : downvote_icon
|
||||
end
|
||||
|
||||
def upvote_ratio
|
||||
return nil if votes.length == 0
|
||||
sprintf("(%.1f%%)", 100.0 * votes.select(&:is_positive?).length / votes.length)
|
||||
end
|
||||
|
||||
def voter_name(vote)
|
||||
if policy(vote).can_see_voter?
|
||||
link_to_user(vote.user, classes: "align-middle")
|
||||
else
|
||||
tag.i("hidden", class: "align-middle")
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user