From 78f372fd0bd411891a7066c266ca8bdf190b92b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 28 Sep 2021 19:10:27 +0200 Subject: [PATCH] Move votes partials to components We're going to change the code quite a bit, and with components we can easily extract methods when necessary. --- .../investments/ballot_component.html.erb | 58 ++++++++++++ .../budgets/investments/ballot_component.rb | 18 ++++ .../comments/votes_component.html.erb | 85 ++++++++++++++++++ app/components/comments/votes_component.rb | 8 ++ .../debates/votes_component.html.erb | 63 +++++++++++++ app/components/debates/votes_component.rb | 9 ++ .../proposals/votes_component.html.erb | 76 ++++++++++++++++ .../legislation/proposals/votes_component.rb | 9 ++ .../proposals/votes_component.html.erb | 47 ++++++++++ app/components/proposals/votes_component.rb | 10 +++ .../budgets/investments/_ballot.html.erb | 70 ++------------- app/views/comments/_votes.html.erb | 88 +------------------ app/views/debates/_votes.html.erb | 64 +------------- .../legislation/proposals/_votes.html.erb | 77 +--------------- app/views/proposals/_votes.html.erb | 48 +--------- app/views/proposals/vote.js.erb | 2 +- 16 files changed, 394 insertions(+), 338 deletions(-) create mode 100644 app/components/budgets/investments/ballot_component.html.erb create mode 100644 app/components/budgets/investments/ballot_component.rb create mode 100644 app/components/comments/votes_component.html.erb create mode 100644 app/components/comments/votes_component.rb create mode 100644 app/components/debates/votes_component.html.erb create mode 100644 app/components/debates/votes_component.rb create mode 100644 app/components/legislation/proposals/votes_component.html.erb create mode 100644 app/components/legislation/proposals/votes_component.rb create mode 100644 app/components/proposals/votes_component.html.erb create mode 100644 app/components/proposals/votes_component.rb diff --git a/app/components/budgets/investments/ballot_component.html.erb b/app/components/budgets/investments/ballot_component.html.erb new file mode 100644 index 000000000..189236531 --- /dev/null +++ b/app/components/budgets/investments/ballot_component.html.erb @@ -0,0 +1,58 @@ +<% reason = investment.reason_for_not_being_ballotable_by(current_user, ballot) %> +
+ <% if ballot.has_investment?(investment) %> +
+ "> + +

+ <%= investment.formatted_price %> +

+ <% if investment.should_show_ballots? %> + <%= link_to t("budgets.ballots.show.remove"), + budget_ballot_line_path(id: investment.id, + budget_id: investment.budget_id, + investments_ids: investment_ids), + class: "button button-remove-support expanded", + method: :delete, + remote: true %> + <% end %> +
+ <% else %> +
+

+ <%= investment.formatted_price %> +

+ <% if investment.should_show_ballots? %> + <%= link_to t("budgets.investments.investment.add"), + budget_ballot_lines_path(investment_id: investment.id, + budget_id: investment.budget_id, + investments_ids: investment_ids), + class: "button button-support expanded", + title: t("budgets.investments.investment.support_title"), + method: :post, + remote: true %> + <% end %> +
+ <% end %> + + <% if reason.present? && !ballot.has_investment?(investment) %> + + <% end %> +
diff --git a/app/components/budgets/investments/ballot_component.rb b/app/components/budgets/investments/ballot_component.rb new file mode 100644 index 000000000..16c82dc4e --- /dev/null +++ b/app/components/budgets/investments/ballot_component.rb @@ -0,0 +1,18 @@ +class Budgets::Investments::BallotComponent < ApplicationComponent + attr_reader :investment, :investment_ids, :ballot, :assigned_heading + delegate :current_user, :heading_link, :link_to_signin, :link_to_signup, + :link_to_verify_account, to: :helpers + + def initialize(investment:, investment_ids:, ballot:, assigned_heading:) + @investment = investment + @investment_ids = investment_ids + @ballot = ballot + @assigned_heading = assigned_heading + end + + private + + def budget + ballot.budget + end +end diff --git a/app/components/comments/votes_component.html.erb b/app/components/comments/votes_component.html.erb new file mode 100644 index 000000000..701bd225f --- /dev/null +++ b/app/components/comments/votes_component.html.erb @@ -0,0 +1,85 @@ +
+ <% if user_signed_in? %> + <%= t("comments.comment.votes", count: comment.total_votes) %> +  |  + + + <% if can?(:vote, comment) %> + <%= link_to vote_comment_path(comment, value: "yes"), + method: "post", remote: true, title: t("votes.agree") do %> + + <%= t("votes.agree") %> + + <% end %> + <% else %> + <%= link_to new_user_session_path do %> + + <%= t("votes.agree") %> + + <% end %> + <% end %> + <%= comment.total_likes %> + + + + <% if can?(:vote, comment) %> + <%= link_to vote_comment_path(comment, value: "no"), + method: "post", remote: true, title: t("votes.disagree") do %> + + <%= t("votes.disagree") %> + + <% end %> + <% else %> + + <%= t("votes.disagree") %> + + <% end %> + <%= comment.total_dislikes %> + + <% elsif !user_signed_in? %> +
+ <%= t("comments.comment.votes", count: comment.total_votes) %> +  |  + + + <% if can?(:vote, comment) %> + <%= link_to vote_comment_path(comment, value: "yes"), + method: "post", remote: true, title: t("votes.agree") do %> + + <%= t("votes.agree") %> + + <% end %> + <% else %> + <%= link_to new_user_session_path do %> + + <%= t("votes.agree") %> + + <% end %> + <% end %> + <%= comment.total_likes %> + + + + <% if can?(:vote, comment) %> + <%= link_to vote_comment_path(comment, value: "no"), + method: "post", remote: true, title: t("votes.disagree") do %> + + <%= t("votes.disagree") %> + + <% end %> + <% else %> + <%= link_to new_user_session_path do %> + + <%= t("votes.disagree") %> + + <% end %> + <% end %> + <%= comment.total_dislikes %> + +
+ + + <% end %> +
diff --git a/app/components/comments/votes_component.rb b/app/components/comments/votes_component.rb new file mode 100644 index 000000000..2287eff18 --- /dev/null +++ b/app/components/comments/votes_component.rb @@ -0,0 +1,8 @@ +class Comments::VotesComponent < ApplicationComponent + attr_reader :comment + delegate :user_signed_in?, :can?, :link_to_signin, :link_to_signup, to: :helpers + + def initialize(comment) + @comment = comment + end +end diff --git a/app/components/debates/votes_component.html.erb b/app/components/debates/votes_component.html.erb new file mode 100644 index 000000000..bf525b7c0 --- /dev/null +++ b/app/components/debates/votes_component.html.erb @@ -0,0 +1,63 @@ +<% voted_classes = css_classes_for_vote(debate_votes, debate) %> +
+
+ <% if user_signed_in? %> + <%= link_to vote_debate_path(debate, value: "yes"), + class: "like #{voted_classes[:in_favor]}", title: t("votes.agree"), method: "post", remote: true do %> + + <%= t("votes.agree") %> + + <%= votes_percentage("likes", debate) %> + <% end %> + <% else %> + + <% end %> +
+ + + +
+ <% if user_signed_in? %> + <%= link_to vote_debate_path(debate, value: "no"), class: "unlike #{voted_classes[:against]}", title: t("votes.disagree"), method: "post", remote: true do %> + + <%= t("votes.disagree") %> + + <%= votes_percentage("dislikes", debate) %> + <% end %> + <% else %> +
+ + <%= t("votes.disagree") %> + + <%= votes_percentage("dislikes", debate) %> +
+ <% end %> +
+ + + <%= t("debates.debate.votes", count: debate.votes_score) %> + + + <% if user_signed_in? && current_user.organization? %> + + <% elsif user_signed_in? && !debate.votable_by?(current_user) %> + + <% elsif !user_signed_in? %> +
+ <%= render "shared/login_to_vote" %> +
+ <% end %> +
diff --git a/app/components/debates/votes_component.rb b/app/components/debates/votes_component.rb new file mode 100644 index 000000000..19f893396 --- /dev/null +++ b/app/components/debates/votes_component.rb @@ -0,0 +1,9 @@ +class Debates::VotesComponent < ApplicationComponent + attr_reader :debate, :debate_votes + delegate :css_classes_for_vote, :current_user, :link_to_verify_account, :user_signed_in?, :votes_percentage, to: :helpers + + def initialize(debate, debate_votes:) + @debate = debate + @debate_votes = debate_votes + end +end diff --git a/app/components/legislation/proposals/votes_component.html.erb b/app/components/legislation/proposals/votes_component.html.erb new file mode 100644 index 000000000..be9cc0495 --- /dev/null +++ b/app/components/legislation/proposals/votes_component.html.erb @@ -0,0 +1,76 @@ +<% voted_classes = css_classes_for_vote(legislation_proposal_votes, proposal) %> +
+ <% if proposal.process.proposals_phase.open? %> +
+ <% if user_signed_in? %> + <%= link_to vote_legislation_process_proposal_path(process_id: proposal.process, id: proposal, value: "yes"), + class: "like #{voted_classes[:in_favor]}", title: t("votes.agree"), method: "post", remote: true do %> + + <%= t("votes.agree") %> + + <%= votes_percentage("likes", proposal) %> + <% end %> + <% else %> + + <% end %> +
+ + + +
+ <% if user_signed_in? %> + <%= link_to vote_legislation_process_proposal_path(process_id: proposal.process, id: proposal, value: "no"), class: "unlike #{voted_classes[:against]}", title: t("votes.disagree"), method: "post", remote: true do %> + + <%= t("votes.disagree") %> + + <%= votes_percentage("dislikes", proposal) %> + <% end %> + <% else %> +
+ + <%= t("votes.disagree") %> + + <%= votes_percentage("dislikes", proposal) %> +
+ <% end %> +
+ <% end %> + + + <%= t("proposals.proposal.votes", count: proposal.votes_score) %> + + + <% if user_signed_in? && current_user.organization? %> + + <% elsif user_signed_in? && !proposal.votable_by?(current_user) %> + + <% elsif !user_signed_in? %> +
+ <%= render "shared/login_to_vote" %> +
+ <% end %> + + <% if voted_for?(legislation_proposal_votes, proposal) && setting["twitter_handle"] %> + + <% end %> +
diff --git a/app/components/legislation/proposals/votes_component.rb b/app/components/legislation/proposals/votes_component.rb new file mode 100644 index 000000000..4c5663e29 --- /dev/null +++ b/app/components/legislation/proposals/votes_component.rb @@ -0,0 +1,9 @@ +class Legislation::Proposals::VotesComponent < ApplicationComponent + attr_reader :proposal, :legislation_proposal_votes + delegate :css_classes_for_vote, :current_user, :link_to_verify_account, :user_signed_in?, :voted_for?, :votes_percentage, to: :helpers + + def initialize(proposal, legislation_proposal_votes:) + @proposal = proposal + @legislation_proposal_votes = legislation_proposal_votes + end +end diff --git a/app/components/proposals/votes_component.html.erb b/app/components/proposals/votes_component.html.erb new file mode 100644 index 000000000..e38091ff5 --- /dev/null +++ b/app/components/proposals/votes_component.html.erb @@ -0,0 +1,47 @@ +
+ <%= render "proposals/supports", proposal: proposal %> + +
+ <% if voted_for?(proposal_votes, proposal) %> +
+ <%= t("proposals.proposal.already_supported") %> +
+ <% elsif user_signed_in? && proposal.votable_by?(current_user) %> + <%= link_to vote_url, + class: "button button-support small expanded", + title: t("proposals.proposal.support_title"), method: "post", remote: true do %> + <%= t("proposals.proposal.support") %> + <% end %> + <% else %> +
+ <%= t("proposals.proposal.support") %> +
+ <% end %> +
+ + <% if user_signed_in? && current_user.organization? %> + + <% elsif user_signed_in? && !proposal.votable_by?(current_user) %> +
+ +
+ <% elsif !user_signed_in? %> +
+ <%= render "shared/login_to_vote" %> +
+ <% end %> + + <% if voted_for?(proposal_votes, proposal) && setting["twitter_handle"] %> + + <% end %> +
diff --git a/app/components/proposals/votes_component.rb b/app/components/proposals/votes_component.rb new file mode 100644 index 000000000..12fa65dad --- /dev/null +++ b/app/components/proposals/votes_component.rb @@ -0,0 +1,10 @@ +class Proposals::VotesComponent < ApplicationComponent + attr_reader :proposal, :vote_url, :proposal_votes + delegate :current_user, :link_to_verify_account, :user_signed_in?, :voted_for?, to: :helpers + + def initialize(proposal, vote_url:, proposal_votes:) + @proposal = proposal + @vote_url = vote_url + @proposal_votes = proposal_votes + end +end diff --git a/app/views/budgets/investments/_ballot.html.erb b/app/views/budgets/investments/_ballot.html.erb index 7a4ea50d4..143072cb8 100644 --- a/app/views/budgets/investments/_ballot.html.erb +++ b/app/views/budgets/investments/_ballot.html.erb @@ -1,64 +1,6 @@ -<% reason = investment.reason_for_not_being_ballotable_by(current_user, ballot) %> -
- <% if ballot.has_investment?(investment) %> - -
- "> - -

- <%= investment.formatted_price %> -

- <% if investment.should_show_ballots? %> - <%= link_to t("budgets.ballots.show.remove"), - budget_ballot_line_path(id: investment.id, - budget_id: investment.budget_id, - investments_ids: investment_ids), - class: "button button-remove-support expanded", - method: :delete, - remote: true %> - <% end %> -
- - <% else %> - -
-

- <%= investment.formatted_price %> -

- <% if investment.should_show_ballots? %> - <%= link_to t("budgets.investments.investment.add"), - budget_ballot_lines_path(investment_id: investment.id, - budget_id: investment.budget_id, - investments_ids: investment_ids), - class: "button button-support expanded", - title: t("budgets.investments.investment.support_title"), - method: :post, - remote: true %> - <% end %> -
- - <% end %> - - <% if reason.present? && !ballot.has_investment?(investment) %> - - - - <% end %> -
+<%= render Budgets::Investments::BallotComponent.new( + investment: investment, + investment_ids: investment_ids, + ballot: ballot, + assigned_heading: @assigned_heading +) %> diff --git a/app/views/comments/_votes.html.erb b/app/views/comments/_votes.html.erb index 925c2d19f..e45be3c2c 100644 --- a/app/views/comments/_votes.html.erb +++ b/app/views/comments/_votes.html.erb @@ -1,87 +1 @@ -
- <% if user_signed_in? %> - <%= t("comments.comment.votes", count: comment.total_votes) %> -  |  - - - <% if can?(:vote, comment) %> - <%= link_to vote_comment_path(comment, value: "yes"), - method: "post", remote: true, title: t("votes.agree") do %> - - <%= t("votes.agree") %> - - <% end %> - <% else %> - <%= link_to new_user_session_path do %> - - <%= t("votes.agree") %> - - <% end %> - <% end %> - <%= comment.total_likes %> - - - - <% if can?(:vote, comment) %> - <%= link_to vote_comment_path(comment, value: "no"), - method: "post", remote: true, title: t("votes.disagree") do %> - - <%= t("votes.disagree") %> - - <% end %> - <% else %> - - <%= t("votes.disagree") %> - - <% end %> - <%= comment.total_dislikes %> - - - <% elsif !user_signed_in? %> - -
- <%= t("comments.comment.votes", count: comment.total_votes) %> -  |  - - - <% if can?(:vote, comment) %> - <%= link_to vote_comment_path(comment, value: "yes"), - method: "post", remote: true, title: t("votes.agree") do %> - - <%= t("votes.agree") %> - - <% end %> - <% else %> - <%= link_to new_user_session_path do %> - - <%= t("votes.agree") %> - - <% end %> - <% end %> - <%= comment.total_likes %> - - - - <% if can?(:vote, comment) %> - <%= link_to vote_comment_path(comment, value: "no"), - method: "post", remote: true, title: t("votes.disagree") do %> - - <%= t("votes.disagree") %> - - <% end %> - <% else %> - <%= link_to new_user_session_path do %> - - <%= t("votes.disagree") %> - - <% end %> - <% end %> - <%= comment.total_dislikes %> - -
- - - <% end %> -
+<%= render Comments::VotesComponent.new(comment) %> diff --git a/app/views/debates/_votes.html.erb b/app/views/debates/_votes.html.erb index 9cd3437a4..dfdd0f629 100644 --- a/app/views/debates/_votes.html.erb +++ b/app/views/debates/_votes.html.erb @@ -1,63 +1 @@ -<% voted_classes = css_classes_for_vote(@debate_votes, debate) %> -
-
- <% if user_signed_in? %> - <%= link_to vote_debate_path(debate, value: "yes"), - class: "like #{voted_classes[:in_favor]}", title: t("votes.agree"), method: "post", remote: true do %> - - <%= t("votes.agree") %> - - <%= votes_percentage("likes", debate) %> - <% end %> - <% else %> - - <% end %> -
- - - -
- <% if user_signed_in? %> - <%= link_to vote_debate_path(debate, value: "no"), class: "unlike #{voted_classes[:against]}", title: t("votes.disagree"), method: "post", remote: true do %> - - <%= t("votes.disagree") %> - - <%= votes_percentage("dislikes", debate) %> - <% end %> - <% else %> -
- - <%= t("votes.disagree") %> - - <%= votes_percentage("dislikes", debate) %> -
- <% end %> -
- - - <%= t("debates.debate.votes", count: debate.votes_score) %> - - - <% if user_signed_in? && current_user.organization? %> - - <% elsif user_signed_in? && !debate.votable_by?(current_user) %> - - <% elsif !user_signed_in? %> -
- <%= render "shared/login_to_vote" %> -
- <% end %> -
+<%= render Debates::VotesComponent.new(debate, debate_votes: @debate_votes) %> diff --git a/app/views/legislation/proposals/_votes.html.erb b/app/views/legislation/proposals/_votes.html.erb index d78c24c4d..669ea4ca6 100644 --- a/app/views/legislation/proposals/_votes.html.erb +++ b/app/views/legislation/proposals/_votes.html.erb @@ -1,76 +1 @@ -<% voted_classes = css_classes_for_vote(@legislation_proposal_votes, proposal) %> -
- <% if @process.proposals_phase.open? %> -
- <% if user_signed_in? %> - <%= link_to vote_legislation_process_proposal_path(process_id: proposal.process, id: proposal, value: "yes"), - class: "like #{voted_classes[:in_favor]}", title: t("votes.agree"), method: "post", remote: true do %> - - <%= t("votes.agree") %> - - <%= votes_percentage("likes", proposal) %> - <% end %> - <% else %> - - <% end %> -
- - - -
- <% if user_signed_in? %> - <%= link_to vote_legislation_process_proposal_path(process_id: proposal.process, id: proposal, value: "no"), class: "unlike #{voted_classes[:against]}", title: t("votes.disagree"), method: "post", remote: true do %> - - <%= t("votes.disagree") %> - - <%= votes_percentage("dislikes", proposal) %> - <% end %> - <% else %> -
- - <%= t("votes.disagree") %> - - <%= votes_percentage("dislikes", proposal) %> -
- <% end %> -
- <% end %> - - - <%= t("proposals.proposal.votes", count: proposal.votes_score) %> - - - <% if user_signed_in? && current_user.organization? %> - - <% elsif user_signed_in? && !proposal.votable_by?(current_user) %> - - <% elsif !user_signed_in? %> -
- <%= render "shared/login_to_vote" %> -
- <% end %> - - <% if voted_for?(@legislation_proposal_votes, proposal) && setting["twitter_handle"] %> - - <% end %> -
+<%= render Legislation::Proposals::VotesComponent.new(proposal, legislation_proposal_votes: @legislation_proposal_votes) %> diff --git a/app/views/proposals/_votes.html.erb b/app/views/proposals/_votes.html.erb index c78dfa720..9f3f3b4a6 100644 --- a/app/views/proposals/_votes.html.erb +++ b/app/views/proposals/_votes.html.erb @@ -1,47 +1 @@ -
- <%= render "proposals/supports", proposal: proposal %> - -
- <% if voted_for?(@proposal_votes, proposal) %> -
- <%= t("proposals.proposal.already_supported") %> -
- <% elsif user_signed_in? && proposal.votable_by?(current_user) %> - <%= link_to vote_url, - class: "button button-support small expanded", - title: t("proposals.proposal.support_title"), method: "post", remote: true do %> - <%= t("proposals.proposal.support") %> - <% end %> - <% else %> -
- <%= t("proposals.proposal.support") %> -
- <% end %> -
- - <% if user_signed_in? && current_user.organization? %> - - <% elsif user_signed_in? && !proposal.votable_by?(current_user) %> -
- -
- <% elsif !user_signed_in? %> -
- <%= render "shared/login_to_vote" %> -
- <% end %> - - <% if voted_for?(@proposal_votes, proposal) && setting["twitter_handle"] %> - - <% end %> -
+<%= render Proposals::VotesComponent.new(proposal, vote_url: vote_url, proposal_votes: @proposal_votes) %> diff --git a/app/views/proposals/vote.js.erb b/app/views/proposals/vote.js.erb index 6dceb49cc..ca1ffc15b 100644 --- a/app/views/proposals/vote.js.erb +++ b/app/views/proposals/vote.js.erb @@ -1,4 +1,4 @@ -$("#<%= dom_id(@proposal) %>_votes").html("<%= j render("proposals/votes", proposal: @proposal) %>"); +$("#<%= dom_id(@proposal) %>_votes").html("<%= j render("proposals/votes", proposal: @proposal, vote_url: nil) %>"); App.Followable.update("<%= dom_id(@follow.followable) %>", "<%= j render("follows/follow_button", follow: @follow) %>", "<%= j render("layouts/flash") %>")