Extract methods in votes components

This way we can make the view code a bit easier to read.

We're also changing the order of the conditions a little bit so we only
check for the presence of a current user once.

To make sure we aren't breaking anything with these changes, we're
adding some tests. We're also replacing one system test checking content
with a component test, since component tests are much faster.
This commit is contained in:
Javi Martín
2021-09-29 04:06:51 +02:00
parent 70f717e564
commit 4e3b3f2028
10 changed files with 163 additions and 56 deletions

View File

@@ -1,4 +1,3 @@
<% voted_classes = css_classes_for_vote(proposal) %>
<div class="votes">
<% if proposal.process.proposals_phase.open? %>
<div class="in-favor inline-block">
@@ -45,23 +44,23 @@
<%= t("proposals.proposal.votes", count: proposal.votes_score) %>
</span>
<% if current_user&.organization? %>
<% if !current_user %>
<div tabindex="0">
<%= render "shared/login_to_vote" %>
</div>
<% elsif organization? %>
<div class="participation-not-allowed" style="display:none" aria-hidden="false">
<p>
<%= t("votes.organizations") %>
</p>
</div>
<% elsif current_user && !proposal.votable_by?(current_user) %>
<% elsif !can_vote? %>
<div class="participation-not-allowed" style="display:none" aria-hidden="false">
<p>
<%= sanitize(t("legislation.proposals.not_verified",
verify_account: link_to_verify_account)) %>
</p>
</div>
<% elsif !current_user %>
<div tabindex="0">
<%= render "shared/login_to_vote" %>
</div>
<% end %>
<% if current_user&.voted_as_when_voted_for(proposal) && setting["twitter_handle"] %>

View File

@@ -5,4 +5,18 @@ class Legislation::Proposals::VotesComponent < ApplicationComponent
def initialize(proposal)
@proposal = proposal
end
private
def voted_classes
@voted_classes ||= css_classes_for_vote(proposal)
end
def can_vote?
proposal.votable_by?(current_user)
end
def organization?
current_user&.organization?
end
end