diff --git a/app/components/comments/votes_component.html.erb b/app/components/comments/votes_component.html.erb
index 41df0614e..8ef3a8293 100644
--- a/app/components/comments/votes_component.html.erb
+++ b/app/components/comments/votes_component.html.erb
@@ -3,24 +3,16 @@
|
- <%= 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 %>
- <%= t("votes.agree") %>
- <% end %>
+ <%= render Shared::VoteButtonComponent.new(comment,
+ value: "yes",
+ title: t("votes.agree")) %>
<%= comment.total_likes %>
- <%= 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 %>
- <%= t("votes.disagree") %>
- <% end %>
+ <%= render Shared::VoteButtonComponent.new(comment,
+ value: "no",
+ title: t("votes.disagree")) %>
<%= comment.total_dislikes %>
diff --git a/app/components/comments/votes_component.rb b/app/components/comments/votes_component.rb
index 143327f24..50cf56a86 100644
--- a/app/components/comments/votes_component.rb
+++ b/app/components/comments/votes_component.rb
@@ -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
diff --git a/app/components/shared/in_favor_against_component.html.erb b/app/components/shared/in_favor_against_component.html.erb
index 954c10ddb..fbe2fb611 100644
--- a/app/components/shared/in_favor_against_component.html.erb
+++ b/app/components/shared/in_favor_against_component.html.erb
@@ -1,25 +1,17 @@
- <%= 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 %>
- <%= t("votes.agree") %>
- <% end %>
+ <%= render Shared::VoteButtonComponent.new(votable,
+ value: "yes",
+ "aria-label": agree_aria_label,
+ title: t("votes.agree")) %>
<%= votes_percentage("likes", votable) %>
- <%= 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 %>
- <%= t("votes.disagree") %>
- <% end %>
+ <%= render Shared::VoteButtonComponent.new(votable,
+ value: "no",
+ "aria-label": disagree_aria_label,
+ title: t("votes.disagree")) %>
<%= votes_percentage("dislikes", votable) %>
diff --git a/app/components/shared/in_favor_against_component.rb b/app/components/shared/in_favor_against_component.rb
index c14f7008a..ee490ecd6 100644
--- a/app/components/shared/in_favor_against_component.rb
+++ b/app/components/shared/in_favor_against_component.rb
@@ -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
diff --git a/app/components/shared/vote_button_component.html.erb b/app/components/shared/vote_button_component.html.erb
new file mode 100644
index 000000000..19b35fc06
--- /dev/null
+++ b/app/components/shared/vote_button_component.html.erb
@@ -0,0 +1,3 @@
+<%= button_to path, default_options.merge(options) do %>
+ <%= options[:title] %>
+<% end %>
diff --git a/app/components/shared/vote_button_component.rb b/app/components/shared/vote_button_component.rb
new file mode 100644
index 000000000..f71e3e93c
--- /dev/null
+++ b/app/components/shared/vote_button_component.rb
@@ -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