Unify the logic to show the "not allowed" message

We were using the same logic six times regarding when we should show a
"participation not allowed" message. Since we're going to change the
current behavior, we're unifying the logic in one place so the changes
will be easier.
This commit is contained in:
Javi Martín
2022-02-19 12:10:46 +01:00
parent 168575d606
commit 9c4d406a77
18 changed files with 137 additions and 103 deletions

View File

@@ -40,17 +40,5 @@
</div> </div>
<% end %> <% end %>
<% if reason.present? && !voted? %> <%= render Shared::ParticipationNotAllowedComponent.new(investment, cannot_vote_text: cannot_vote_text) %>
<div tabindex="0">
<div class="participation-not-allowed" style="display:none">
<p>
<%= sanitize(t("budgets.ballots.reasons_for_not_balloting.#{reason}",
verify_account: link_to_verify_account, signin: link_to_signin,
signup: link_to_signup, my_heading: link_to_my_heading,
change_ballot: link_to_change_ballot,
heading_link: heading_link(assigned_heading, budget))) %>
</p>
</div>
</div>
<% end %>
</div> </div>

View File

@@ -1,7 +1,6 @@
class Budgets::Investments::BallotComponent < ApplicationComponent class Budgets::Investments::BallotComponent < ApplicationComponent
attr_reader :investment, :investment_ids, :ballot, :assigned_heading attr_reader :investment, :investment_ids, :ballot, :assigned_heading
delegate :current_user, :heading_link, :link_to_signin, :link_to_signup, delegate :current_user, :heading_link, :link_to_verify_account, to: :helpers
:link_to_verify_account, to: :helpers
def initialize(investment:, investment_ids:, ballot:, assigned_heading:) def initialize(investment:, investment_ids:, ballot:, assigned_heading:)
@investment = investment @investment = investment
@@ -42,4 +41,14 @@ class Budgets::Investments::BallotComponent < ApplicationComponent
link_to(t("budgets.ballots.reasons_for_not_balloting.change_ballot"), link_to(t("budgets.ballots.reasons_for_not_balloting.change_ballot"),
budget_ballot_path(budget)) budget_ballot_path(budget))
end end
def cannot_vote_text
if reason.present? && !voted?
t("budgets.ballots.reasons_for_not_balloting.#{reason}",
verify_account: link_to_verify_account,
my_heading: link_to_my_heading,
change_ballot: link_to_change_ballot,
heading_link: heading_link(assigned_heading, budget))
end
end
end end

View File

@@ -34,21 +34,7 @@
<% end %> <% end %>
</div> </div>
<% if reason.present? && !user_voted_for? %> <%= render Shared::ParticipationNotAllowedComponent.new(investment, cannot_vote_text: cannot_vote_text) %>
<div tabindex="0">
<div class="participation-not-allowed" style="display:none" aria-hidden="false">
<p>
<%= sanitize(t("votes.budget_investments.#{reason}",
count: investment.group.max_votable_headings,
verify_account: link_to_verify_account,
signin: link_to_signin,
signup: link_to_signup,
supported_headings: (current_user && current_user.headings_voted_within_group(investment.group).map(&:name).sort.to_sentence)
)) %>
</p>
</div>
</div>
<% end %>
<% if user_voted_for? && setting["twitter_handle"] %> <% if user_voted_for? && setting["twitter_handle"] %>
<div class="share-supported"> <div class="share-supported">

View File

@@ -1,7 +1,6 @@
class Budgets::Investments::VotesComponent < ApplicationComponent class Budgets::Investments::VotesComponent < ApplicationComponent
attr_reader :investment attr_reader :investment
delegate :namespace, :current_user, :image_absolute_url, delegate :namespace, :current_user, :image_absolute_url, :link_to_verify_account, to: :helpers
:link_to_verify_account, :link_to_signin, :link_to_signup, to: :helpers
def initialize(investment) def initialize(investment)
@investment = investment @investment = investment
@@ -59,4 +58,13 @@ class Budgets::Investments::VotesComponent < ApplicationComponent
def remove_support_aria_label def remove_support_aria_label
t("budgets.investments.votes.remove_support_label", investment: investment.title) t("budgets.investments.votes.remove_support_label", investment: investment.title)
end end
def cannot_vote_text
if reason.present? && !user_voted_for?
t("votes.budget_investments.#{reason}",
count: investment.group.max_votable_headings,
verify_account: link_to_verify_account,
supported_headings: (current_user && current_user.headings_voted_within_group(investment.group).map(&:name).sort.to_sentence))
end
end
end end

View File

@@ -5,21 +5,5 @@
<%= t("debates.debate.votes", count: debate.votes_score) %> <%= t("debates.debate.votes", count: debate.votes_score) %>
</span> </span>
<% if !current_user %> <%= render Shared::ParticipationNotAllowedComponent.new(debate, cannot_vote_text: cannot_vote_text) %>
<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 !can_vote? %>
<div class="participation-not-allowed" style="display:none" aria-hidden="false">
<p>
<%= sanitize(t("votes.anonymous", verify_account: link_to_verify_account)) %>
</p>
</div>
<% end %>
</div> </div>

View File

@@ -12,7 +12,7 @@ class Debates::VotesComponent < ApplicationComponent
debate.votable_by?(current_user) debate.votable_by?(current_user)
end end
def organization? def cannot_vote_text
current_user&.organization? t("votes.anonymous", verify_account: link_to_verify_account) unless can_vote?
end end
end end

View File

@@ -7,24 +7,7 @@
<%= t("proposals.proposal.votes", count: proposal.votes_score) %> <%= t("proposals.proposal.votes", count: proposal.votes_score) %>
</span> </span>
<% if !current_user %> <%= render Shared::ParticipationNotAllowedComponent.new(proposal, cannot_vote_text: cannot_vote_text) %>
<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 !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>
<% end %>
<% if current_user&.voted_as_when_voted_for(proposal) && setting["twitter_handle"] %> <% if current_user&.voted_as_when_voted_for(proposal) && setting["twitter_handle"] %>
<div class="share-supported"> <div class="share-supported">

View File

@@ -12,7 +12,7 @@ class Legislation::Proposals::VotesComponent < ApplicationComponent
proposal.votable_by?(current_user) proposal.votable_by?(current_user)
end end
def organization? def cannot_vote_text
current_user&.organization? t("legislation.proposals.not_verified", verify_account: link_to_verify_account) unless can_vote?
end end
end end

View File

@@ -19,25 +19,7 @@
<% end %> <% end %>
</div> </div>
<% if !current_user %> <%= render Shared::ParticipationNotAllowedComponent.new(proposal, cannot_vote_text: cannot_vote_text) %>
<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 !can_vote? %>
<div tabindex="0">
<div class="participation-not-allowed" style="display:none" aria-hidden="false">
<p>
<%= sanitize(t("votes.verified_only", verify_account: link_to_verify_account)) %>
</p>
</div>
</div>
<% end %>
<% if voted? && setting["twitter_handle"] %> <% if voted? && setting["twitter_handle"] %>
<div class="share-supported"> <div class="share-supported">

View File

@@ -25,7 +25,7 @@ class Proposals::VotesComponent < ApplicationComponent
t("proposals.proposal.support_label", proposal: proposal.title) t("proposals.proposal.support_label", proposal: proposal.title)
end end
def organization? def cannot_vote_text
current_user&.organization? t("votes.verified_only", verify_account: link_to_verify_account) unless can_vote?
end end
end end

View File

@@ -0,0 +1,5 @@
<div tabindex="0">
<div class="participation-not-allowed" style="display:none" aria-hidden="false">
<%= body %>
</div>
</div>

View File

@@ -0,0 +1,30 @@
class Shared::ParticipationNotAllowedComponent < ApplicationComponent
attr_reader :votable, :cannot_vote_text
delegate :current_user, :link_to_signin, :link_to_signup, to: :helpers
def initialize(votable, cannot_vote_text:)
@votable = votable
@cannot_vote_text = cannot_vote_text
end
def render?
body.present?
end
private
def body
@body ||=
if !current_user
sanitize(t("users.login_to_continue", signin: link_to_signin, signup: link_to_signup))
elsif organization?
tag.p t("votes.organizations")
elsif cannot_vote_text.present?
tag.p sanitize(cannot_vote_text)
end
end
def organization?
current_user&.organization?
end
end

View File

@@ -1,3 +0,0 @@
<div class="participation-not-allowed" style="display:none" aria-hidden="false">
<%= sanitize(t("users.login_to_continue", signin: link_to_signin, signup: link_to_signup)) %>
</div>

View File

@@ -31,9 +31,7 @@ en:
voted_info_2: "But you can change your vote at any time until this phase is closed." voted_info_2: "But you can change your vote at any time until this phase is closed."
zero: You have not voted any investment project. zero: You have not voted any investment project.
reasons_for_not_balloting: reasons_for_not_balloting:
not_logged_in: You must %{signin} or %{signup} to continue.
not_verified: Only verified users can vote on investments; %{verify_account}. not_verified: Only verified users can vote on investments; %{verify_account}.
organization: Organizations are not permitted to vote
not_selected: Unselected investment projects can not be supported not_selected: Unselected investment projects can not be supported
not_enough_money: "You have already assigned the available budget.<br><small>Remember you can %{change_ballot} at any time</small>" not_enough_money: "You have already assigned the available budget.<br><small>Remember you can %{change_ballot} at any time</small>"
no_ballots_allowed: Selecting phase is closed no_ballots_allowed: Selecting phase is closed

View File

@@ -855,9 +855,7 @@ en:
supports: Supports supports: Supports
verified_only: Only verified users can vote on proposals; %{verify_account}. verified_only: Only verified users can vote on proposals; %{verify_account}.
budget_investments: budget_investments:
not_logged_in: You must %{signin} or %{signup} to continue.
not_verified: Only verified users can vote on investment projects; %{verify_account}. not_verified: Only verified users can vote on investment projects; %{verify_account}.
organization: Organizations are not permitted to vote
unfeasible: Unfeasible investment projects can not be supported unfeasible: Unfeasible investment projects can not be supported
not_voting_allowed: Voting phase is closed not_voting_allowed: Voting phase is closed
different_heading_assigned: different_heading_assigned:

View File

@@ -31,9 +31,7 @@ es:
voted_info_2: "Pero puedes cambiarlos en cualquier momento hasta el cierre de esta fase." voted_info_2: "Pero puedes cambiarlos en cualquier momento hasta el cierre de esta fase."
zero: Todavía no has votado ningún proyecto de gasto. zero: Todavía no has votado ningún proyecto de gasto.
reasons_for_not_balloting: reasons_for_not_balloting:
not_logged_in: Necesitas %{signin} o %{signup} para continuar.
not_verified: Los proyectos de gasto sólo pueden ser apoyados por usuarios verificados, %{verify_account}. not_verified: Los proyectos de gasto sólo pueden ser apoyados por usuarios verificados, %{verify_account}.
organization: Las organizaciones no pueden votar.
not_selected: No se pueden votar proyectos inviables. not_selected: No se pueden votar proyectos inviables.
not_enough_money: "Ya has asignado el presupuesto disponible.<br><small>Recuerda que puedes %{change_ballot} en cualquier momento</small>" not_enough_money: "Ya has asignado el presupuesto disponible.<br><small>Recuerda que puedes %{change_ballot} en cualquier momento</small>"
no_ballots_allowed: El periodo de votación está cerrado. no_ballots_allowed: El periodo de votación está cerrado.

View File

@@ -855,9 +855,7 @@ es:
supports: Apoyos supports: Apoyos
verified_only: Las propuestas sólo pueden ser votadas por usuarios verificados, %{verify_account}. verified_only: Las propuestas sólo pueden ser votadas por usuarios verificados, %{verify_account}.
budget_investments: budget_investments:
not_logged_in: Necesitas %{signin} o %{signup} para continuar.
not_verified: Los proyectos de gasto sólo pueden ser apoyadas por usuarios verificados, %{verify_account}. not_verified: Los proyectos de gasto sólo pueden ser apoyadas por usuarios verificados, %{verify_account}.
organization: Las organizaciones no pueden votar.
unfeasible: No se pueden votar propuestas inviables. unfeasible: No se pueden votar propuestas inviables.
not_voting_allowed: El periodo de votación está cerrado. not_voting_allowed: El periodo de votación está cerrado.
different_heading_assigned: different_heading_assigned:

View File

@@ -0,0 +1,70 @@
require "rails_helper"
describe Shared::ParticipationNotAllowedComponent do
let(:votable) { create(:proposal) }
context "Without cannot vote text" do
let(:component) { Shared::ParticipationNotAllowedComponent.new(votable, cannot_vote_text: nil) }
it "asks anonymous users to sign in or sign up" do
render_inline component
expect(page).to have_content "You must sign in or sign up to continue"
expect(page).to have_link "sign in"
expect(page).to have_link "sign up"
end
it "says voting is not allowed to organizations" do
sign_in(create(:organization, user: create(:user, :level_two)).user)
render_inline component
expect(page).to have_content "Organizations are not permitted to vote"
expect(page).not_to have_link "sign in", visible: :all
expect(page).not_to have_link "sign up", visible: :all
end
it "is not rendered to regular users" do
sign_in(create(:user))
render_inline component
expect(page).not_to be_rendered
end
end
context "With cannot vote text" do
let(:component) { Shared::ParticipationNotAllowedComponent.new(votable, cannot_vote_text: "Too old") }
it "ignores the text and asks anonymous users to sign in or sign up" do
render_inline component
expect(page).to have_content "You must sign in or sign up to continue"
expect(page).to have_link "sign in"
expect(page).to have_link "sign up"
expect(page).not_to have_content "Too old"
end
it "ignores the text and says voting is not allowed to organizations" do
sign_in(create(:organization, user: create(:user, :level_two)).user)
render_inline component
expect(page).to have_content "Organizations are not permitted to vote"
expect(page).not_to have_link "sign in", visible: :all
expect(page).not_to have_link "sign up", visible: :all
expect(page).not_to have_content "Too old"
end
it "renders the cannot vote text to regular users" do
sign_in(create(:user))
render_inline component
expect(page).to have_content "Too old"
expect(page).not_to have_content "Organizations are not permitted to vote"
expect(page).not_to have_link "sign in", visible: :all
expect(page).not_to have_link "sign up", visible: :all
end
end
end