The `use_helpers` method was added in ViewComponent 3.8.0, and it's included by default in all components since version 3.11.0. Note we sometimes delegated the `can?` method to the controller instead of the helpers, for no particularly reason. We're unifying that code as well.
49 lines
954 B
Ruby
49 lines
954 B
Ruby
class Shared::VoteButtonComponent < ApplicationComponent
|
|
attr_reader :votable, :value, :options
|
|
use_helpers :current_user, :can?
|
|
|
|
def initialize(votable, value:, **options)
|
|
@votable = votable
|
|
@value = value
|
|
@options = options
|
|
end
|
|
|
|
private
|
|
|
|
def path
|
|
if already_voted?
|
|
polymorphic_path(vote)
|
|
else
|
|
polymorphic_path(vote, value: value)
|
|
end
|
|
end
|
|
|
|
def default_options
|
|
if already_voted?
|
|
{
|
|
"aria-pressed": true,
|
|
method: :delete,
|
|
remote: can?(:destroy, vote)
|
|
}
|
|
else
|
|
{
|
|
"aria-pressed": false,
|
|
method: :post,
|
|
remote: can?(:create, vote)
|
|
}
|
|
end
|
|
end
|
|
|
|
def vote
|
|
@vote ||= Vote.find_or_initialize_by(votable: votable, voter: current_user, vote_flag: parsed_value)
|
|
end
|
|
|
|
def already_voted?
|
|
vote.persisted?
|
|
end
|
|
|
|
def parsed_value
|
|
value == "yes"
|
|
end
|
|
end
|