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">
|
<span class="in-favor">
|
||||||
<%= button_to vote_in_favor_against_path("yes"),
|
<%= render Shared::VoteButtonComponent.new(comment,
|
||||||
method: user_already_voted_with("yes") ? "delete" : "post",
|
value: "yes",
|
||||||
remote: remote_submit("yes"),
|
title: t("votes.agree")) %>
|
||||||
"aria-pressed": pressed?("yes"),
|
|
||||||
title: t("votes.agree") do %>
|
|
||||||
<span class="show-for-sr"><%= t("votes.agree") %></span>
|
|
||||||
<% end %>
|
|
||||||
<%= comment.total_likes %>
|
<%= comment.total_likes %>
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
<span class="against">
|
<span class="against">
|
||||||
<%= button_to vote_in_favor_against_path("no"),
|
<%= render Shared::VoteButtonComponent.new(comment,
|
||||||
method: user_already_voted_with("no") ? "delete" : "post",
|
value: "no",
|
||||||
remote: remote_submit("no"),
|
title: t("votes.disagree")) %>
|
||||||
"aria-pressed": pressed?("no"),
|
|
||||||
title: t("votes.disagree") do %>
|
|
||||||
<span class="show-for-sr"><%= t("votes.disagree") %></span>
|
|
||||||
<% end %>
|
|
||||||
<%= comment.total_dislikes %>
|
<%= comment.total_dislikes %>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,45 +1,7 @@
|
|||||||
class Comments::VotesComponent < ApplicationComponent
|
class Comments::VotesComponent < ApplicationComponent
|
||||||
attr_reader :comment
|
attr_reader :comment
|
||||||
delegate :can?, :current_user, to: :helpers
|
|
||||||
|
|
||||||
def initialize(comment)
|
def initialize(comment)
|
||||||
@comment = comment
|
@comment = comment
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -1,25 +1,17 @@
|
|||||||
<div class="in-favor-against">
|
<div class="in-favor-against">
|
||||||
<div class="in-favor">
|
<div class="in-favor">
|
||||||
<%= button_to vote_in_favor_against_path("yes"),
|
<%= render Shared::VoteButtonComponent.new(votable,
|
||||||
title: t("votes.agree"),
|
value: "yes",
|
||||||
"aria-label": agree_aria_label,
|
"aria-label": agree_aria_label,
|
||||||
"aria-pressed": pressed?("yes"),
|
title: t("votes.agree")) %>
|
||||||
method: user_already_voted_with("yes") ? "delete" : "post",
|
|
||||||
remote: true do %>
|
|
||||||
<span class="show-for-sr"><%= t("votes.agree") %></span>
|
|
||||||
<% end %>
|
|
||||||
<span class="percentage"><%= votes_percentage("likes", votable) %></span>
|
<span class="percentage"><%= votes_percentage("likes", votable) %></span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="against">
|
<div class="against">
|
||||||
<%= button_to vote_in_favor_against_path("no"),
|
<%= render Shared::VoteButtonComponent.new(votable,
|
||||||
title: t("votes.disagree"),
|
value: "no",
|
||||||
"aria-label": disagree_aria_label,
|
"aria-label": disagree_aria_label,
|
||||||
"aria-pressed": pressed?("no"),
|
title: t("votes.disagree")) %>
|
||||||
method: user_already_voted_with("no") ? "delete" : "post",
|
|
||||||
remote: true do %>
|
|
||||||
<span class="show-for-sr"><%= t("votes.disagree") %></span>
|
|
||||||
<% end %>
|
|
||||||
<span class="percentage"><%= votes_percentage("dislikes", votable) %></span>
|
<span class="percentage"><%= votes_percentage("dislikes", votable) %></span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
class Shared::InFavorAgainstComponent < ApplicationComponent
|
class Shared::InFavorAgainstComponent < ApplicationComponent
|
||||||
attr_reader :votable
|
attr_reader :votable
|
||||||
delegate :current_user, :votes_percentage, to: :helpers
|
delegate :votes_percentage, to: :helpers
|
||||||
|
|
||||||
def initialize(votable)
|
def initialize(votable)
|
||||||
@votable = votable
|
@votable = votable
|
||||||
@@ -15,32 +15,4 @@ class Shared::InFavorAgainstComponent < ApplicationComponent
|
|||||||
def disagree_aria_label
|
def disagree_aria_label
|
||||||
t("votes.disagree_label", title: votable.title)
|
t("votes.disagree_label", title: votable.title)
|
||||||
end
|
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
|
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