We were using the same code to render links to agree and disagree, so we can extract a new component for this code. We're also adding component tests to make it easier to test whether we're breaking anything while refactoring, although the code is probably already covered by system tests. Since the votes mixin was only used in one place, we're removing it and moving most of its code to a new CSS file for the shared component.
26 lines
580 B
Ruby
26 lines
580 B
Ruby
class Shared::InFavorAgainstComponent < ApplicationComponent
|
|
attr_reader :votable
|
|
delegate :current_user, :votes_percentage, to: :helpers
|
|
|
|
def initialize(votable)
|
|
@votable = votable
|
|
end
|
|
|
|
private
|
|
|
|
def voted_classes
|
|
@voted_classes ||= css_classes_for_vote
|
|
end
|
|
|
|
def css_classes_for_vote
|
|
case current_user&.voted_as_when_voted_for(votable)
|
|
when true
|
|
{ in_favor: "voted", against: "no-voted" }
|
|
when false
|
|
{ in_favor: "no-voted", against: "voted" }
|
|
else
|
|
{ in_favor: "", against: "" }
|
|
end
|
|
end
|
|
end
|