Add upvote and downvote buttons beneath thumbnails on the post index page. This is disabled by default. To enable it, click the "..." menu in the top right of the page, then click "Show scores". This is currently a per-search setting, not an account setting. If you enable it in one tab, it won't be enabled in other tabs.
29 lines
553 B
Ruby
29 lines
553 B
Ruby
# frozen_string_literal: true
|
|
|
|
# This component represents the post score and upvote/downvote buttons ("🠉 5 🠋")
|
|
class PostVotesComponent < ApplicationComponent
|
|
attr_reader :post, :current_user
|
|
|
|
def initialize(post:, current_user:)
|
|
super
|
|
@post = post
|
|
@current_user = current_user
|
|
end
|
|
|
|
def can_vote?
|
|
policy(PostVote).create?
|
|
end
|
|
|
|
def current_vote
|
|
post.vote_by_current_user
|
|
end
|
|
|
|
def upvoted?
|
|
can_vote? && current_vote&.is_positive?
|
|
end
|
|
|
|
def downvoted?
|
|
can_vote? && current_vote&.is_negative?
|
|
end
|
|
end
|