Unify code in debates/legislation vote links

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.
This commit is contained in:
Javi Martín
2022-01-13 17:51:13 +01:00
parent 9a625fe59f
commit 86ad2df46d
12 changed files with 254 additions and 214 deletions

View File

@@ -1,43 +1,6 @@
<div class="votes">
<% if proposal.process.proposals_phase.open? %>
<div class="in-favor inline-block">
<% if current_user %>
<%= 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 %>
<span class="icon-like">
<span class="show-for-sr"><%= t("votes.agree") %></span>
</span>
<span class="percentage"><%= votes_percentage("likes", proposal) %></span>
<% end %>
<% else %>
<div class="like">
<span class="icon-like">
<span class="show-for-sr"><%= t("votes.agree") %></span>
</span>
<span class="percentage"><%= votes_percentage("likes", proposal) %></span>
</div>
<% end %>
</div>
<span class="divider"></span>
<div class="against inline-block">
<% if current_user %>
<%= 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 %>
<span class="icon-unlike">
<span class="show-for-sr"><%= t("votes.disagree") %></span>
</span>
<span class="percentage"><%= votes_percentage("dislikes", proposal) %></span>
<% end %>
<% else %>
<div class="unlike">
<span class="icon-unlike">
<span class="show-for-sr"><%= t("votes.disagree") %></span>
</span>
<span class="percentage"><%= votes_percentage("dislikes", proposal) %></span>
</div>
<% end %>
</div>
<%= render Shared::InFavorAgainstComponent.new(proposal) %>
<% end %>
<span class="total-votes">

View File

@@ -1,6 +1,6 @@
class Legislation::Proposals::VotesComponent < ApplicationComponent
attr_reader :proposal
delegate :css_classes_for_vote, :current_user, :link_to_verify_account, :votes_percentage, to: :helpers
delegate :current_user, :link_to_verify_account, to: :helpers
def initialize(proposal)
@proposal = proposal
@@ -8,10 +8,6 @@ class Legislation::Proposals::VotesComponent < ApplicationComponent
private
def voted_classes
@voted_classes ||= css_classes_for_vote(proposal)
end
def can_vote?
proposal.votable_by?(current_user)
end