Add component in order to reduce duplicated code
Co-authored-by: Javi Martín <javim@elretirao.net>
This commit is contained in:
@@ -3,24 +3,16 @@
|
||||
|
|
||||
|
||||
<span class="in-favor">
|
||||
<%= button_to vote_in_favor_against_path("yes"),
|
||||
method: user_already_voted_with("yes") ? "delete" : "post",
|
||||
remote: remote_submit("yes"),
|
||||
"aria-pressed": pressed?("yes"),
|
||||
title: t("votes.agree") do %>
|
||||
<span class="show-for-sr"><%= t("votes.agree") %></span>
|
||||
<% end %>
|
||||
<%= render Shared::VoteButtonComponent.new(comment,
|
||||
value: "yes",
|
||||
title: t("votes.agree")) %>
|
||||
<%= comment.total_likes %>
|
||||
</span>
|
||||
|
||||
<span class="against">
|
||||
<%= button_to vote_in_favor_against_path("no"),
|
||||
method: user_already_voted_with("no") ? "delete" : "post",
|
||||
remote: remote_submit("no"),
|
||||
"aria-pressed": pressed?("no"),
|
||||
title: t("votes.disagree") do %>
|
||||
<span class="show-for-sr"><%= t("votes.disagree") %></span>
|
||||
<% end %>
|
||||
<%= render Shared::VoteButtonComponent.new(comment,
|
||||
value: "no",
|
||||
title: t("votes.disagree")) %>
|
||||
<%= comment.total_dislikes %>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
@@ -1,45 +1,7 @@
|
||||
class Comments::VotesComponent < ApplicationComponent
|
||||
attr_reader :comment
|
||||
delegate :can?, :current_user, to: :helpers
|
||||
|
||||
def initialize(comment)
|
||||
@comment = comment
|
||||
end
|
||||
|
||||
def pressed?(value)
|
||||
case current_user&.voted_as_when_voted_for(comment)
|
||||
when true
|
||||
value == "yes"
|
||||
when false
|
||||
value == "no"
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def vote_in_favor_against_path(value)
|
||||
if user_already_voted_with(value)
|
||||
vote = comment.votes_for.find_by!(voter: current_user)
|
||||
|
||||
comment_vote_path(comment, vote, value: value)
|
||||
else
|
||||
comment_votes_path(comment, value: value)
|
||||
end
|
||||
end
|
||||
|
||||
def user_already_voted_with(value)
|
||||
current_user&.voted_as_when_voted_for(comment) == parse_vote(value)
|
||||
end
|
||||
|
||||
def parse_vote(value)
|
||||
value == "yes" ? true : false
|
||||
end
|
||||
|
||||
def remote_submit(value)
|
||||
if user_already_voted_with(value)
|
||||
can?(:destroy, comment.votes_for.new(voter: current_user))
|
||||
else
|
||||
can?(:create, comment.votes_for.new(voter: current_user))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,25 +1,17 @@
|
||||
<div class="in-favor-against">
|
||||
<div class="in-favor">
|
||||
<%= button_to vote_in_favor_against_path("yes"),
|
||||
title: t("votes.agree"),
|
||||
"aria-label": agree_aria_label,
|
||||
"aria-pressed": pressed?("yes"),
|
||||
method: user_already_voted_with("yes") ? "delete" : "post",
|
||||
remote: true do %>
|
||||
<span class="show-for-sr"><%= t("votes.agree") %></span>
|
||||
<% end %>
|
||||
<%= render Shared::VoteButtonComponent.new(votable,
|
||||
value: "yes",
|
||||
"aria-label": agree_aria_label,
|
||||
title: t("votes.agree")) %>
|
||||
<span class="percentage"><%= votes_percentage("likes", votable) %></span>
|
||||
</div>
|
||||
|
||||
<div class="against">
|
||||
<%= button_to vote_in_favor_against_path("no"),
|
||||
title: t("votes.disagree"),
|
||||
"aria-label": disagree_aria_label,
|
||||
"aria-pressed": pressed?("no"),
|
||||
method: user_already_voted_with("no") ? "delete" : "post",
|
||||
remote: true do %>
|
||||
<span class="show-for-sr"><%= t("votes.disagree") %></span>
|
||||
<% end %>
|
||||
<%= render Shared::VoteButtonComponent.new(votable,
|
||||
value: "no",
|
||||
"aria-label": disagree_aria_label,
|
||||
title: t("votes.disagree")) %>
|
||||
<span class="percentage"><%= votes_percentage("dislikes", votable) %></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
class Shared::InFavorAgainstComponent < ApplicationComponent
|
||||
attr_reader :votable
|
||||
delegate :current_user, :votes_percentage, to: :helpers
|
||||
delegate :votes_percentage, to: :helpers
|
||||
|
||||
def initialize(votable)
|
||||
@votable = votable
|
||||
@@ -15,32 +15,4 @@ class Shared::InFavorAgainstComponent < ApplicationComponent
|
||||
def disagree_aria_label
|
||||
t("votes.disagree_label", title: votable.title)
|
||||
end
|
||||
|
||||
def pressed?(value)
|
||||
case current_user&.voted_as_when_voted_for(votable)
|
||||
when true
|
||||
value == "yes"
|
||||
when false
|
||||
value == "no"
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def vote_in_favor_against_path(value)
|
||||
if user_already_voted_with(value)
|
||||
vote = Vote.find_by!(votable: votable, voter: current_user)
|
||||
polymorphic_path(vote)
|
||||
else
|
||||
polymorphic_path(Vote.new(votable: votable), value: value)
|
||||
end
|
||||
end
|
||||
|
||||
def user_already_voted_with(value)
|
||||
current_user&.voted_as_when_voted_for(votable) == parse_vote(value)
|
||||
end
|
||||
|
||||
def parse_vote(value)
|
||||
value == "yes" ? true : false
|
||||
end
|
||||
end
|
||||
|
||||
3
app/components/shared/vote_button_component.html.erb
Normal file
3
app/components/shared/vote_button_component.html.erb
Normal file
@@ -0,0 +1,3 @@
|
||||
<%= button_to path, default_options.merge(options) do %>
|
||||
<span class="show-for-sr"><%= options[:title] %></span>
|
||||
<% end %>
|
||||
48
app/components/shared/vote_button_component.rb
Normal file
48
app/components/shared/vote_button_component.rb
Normal file
@@ -0,0 +1,48 @@
|
||||
class Shared::VoteButtonComponent < ApplicationComponent
|
||||
attr_reader :votable, :value, :options
|
||||
delegate :current_user, :can?, to: :helpers
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user