@@ -45,23 +44,23 @@
<%= t("proposals.proposal.votes", count: proposal.votes_score) %>
- <% if current_user&.organization? %>
+ <% if !current_user %>
+
+ <%= render "shared/login_to_vote" %>
+
+ <% elsif organization? %>
<%= t("votes.organizations") %>
- <% elsif current_user && !proposal.votable_by?(current_user) %>
+ <% elsif !can_vote? %>
<%= sanitize(t("legislation.proposals.not_verified",
verify_account: link_to_verify_account)) %>
- <% elsif !current_user %>
-
- <%= render "shared/login_to_vote" %>
-
<% end %>
<% if current_user&.voted_as_when_voted_for(proposal) && setting["twitter_handle"] %>
diff --git a/app/components/legislation/proposals/votes_component.rb b/app/components/legislation/proposals/votes_component.rb
index f59d11d68..fced9f731 100644
--- a/app/components/legislation/proposals/votes_component.rb
+++ b/app/components/legislation/proposals/votes_component.rb
@@ -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
diff --git a/app/components/proposals/votes_component.html.erb b/app/components/proposals/votes_component.html.erb
index 5ca164d94..9a28371aa 100644
--- a/app/components/proposals/votes_component.html.erb
+++ b/app/components/proposals/votes_component.html.erb
@@ -2,11 +2,11 @@
<%= render "proposals/supports", proposal: proposal %>
- <% if current_user&.voted_for?(proposal) %>
+ <% if voted? %>
<%= t("proposals.proposal.already_supported") %>
- <% elsif current_user && proposal.votable_by?(current_user) %>
+ <% elsif can_vote? %>
<%= link_to vote_url,
class: "button button-support small expanded",
title: t("proposals.proposal.support_title"), method: "post", remote: true do %>
@@ -19,13 +19,17 @@
<% end %>
- <% if current_user&.organization? %>
+ <% if !current_user %>
+
+ <%= render "shared/login_to_vote" %>
+
+ <% elsif organization? %>
<%= t("votes.organizations") %>
- <% elsif current_user && !proposal.votable_by?(current_user) %>
+ <% elsif !can_vote? %>
- <% elsif !current_user %>
-
- <%= render "shared/login_to_vote" %>
-
<% end %>
- <% if current_user&.voted_for?(proposal) && setting["twitter_handle"] %>
+ <% if voted? && setting["twitter_handle"] %>
<%= render "proposals/social_share", proposal: proposal, share_title: false %>
diff --git a/app/components/proposals/votes_component.rb b/app/components/proposals/votes_component.rb
index 143b32ada..bae9cf50c 100644
--- a/app/components/proposals/votes_component.rb
+++ b/app/components/proposals/votes_component.rb
@@ -10,4 +10,18 @@ class Proposals::VotesComponent < ApplicationComponent
def vote_url
@vote_url || vote_proposal_path(proposal, value: "yes")
end
+
+ private
+
+ def voted?
+ current_user&.voted_for?(proposal)
+ end
+
+ def can_vote?
+ proposal.votable_by?(current_user)
+ end
+
+ def organization?
+ current_user&.organization?
+ end
end
diff --git a/spec/components/proposals/votes_component_spec.rb b/spec/components/proposals/votes_component_spec.rb
new file mode 100644
index 000000000..b4837318d
--- /dev/null
+++ b/spec/components/proposals/votes_component_spec.rb
@@ -0,0 +1,77 @@
+require "rails_helper"
+
+describe Proposals::VotesComponent do
+ let(:proposal) { create(:proposal, title: "Create a monthly transport ticket") }
+ let(:component) { Proposals::VotesComponent.new(proposal) }
+
+ describe "support proposal link" do
+ it "is shown as plain text to unverified users" do
+ sign_in(create(:user))
+
+ render_inline component
+
+ expect(page).to have_content "Support"
+ expect(page).not_to have_link "Support"
+ end
+
+ it "is shown to verified users" do
+ sign_in(create(:user, :level_two))
+
+ render_inline component
+
+ expect(page).to have_link count: 1
+ expect(page).to have_link "Support", title: "Support this proposal"
+ expect(page).not_to have_content "You have already supported this proposal. Share it!"
+ end
+
+ it "is replaced with a success message when the proposal is already supported" do
+ sign_in(create(:user, :level_two, votables: [proposal]))
+
+ render_inline component
+
+ expect(page).to have_content "You have already supported this proposal. Share it!"
+ expect(page).not_to have_link "Support"
+ end
+ end
+
+ describe "participation not allowed" do
+ it "asks anonymous users to sign in or sign up" do
+ render_inline component
+
+ expect(page).to have_link "sign in", visible: :hidden
+ expect(page).to have_link "sign up", visible: :hidden
+ 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 "says only verified users can vote to unverified users" do
+ sign_in(create(:user))
+
+ render_inline component
+
+ expect(page).to have_content "Only verified users can vote on proposals"
+ expect(page).to have_link "verify your account", visible: :hidden
+ 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 for verified users" do
+ sign_in(create(:user, :level_two))
+
+ render_inline component
+
+ expect(page).not_to have_css ".participation-not-allowed"
+ expect(page).not_to have_link "verify your account", visible: :all
+ expect(page).not_to have_link "sign in", visible: :all
+ expect(page).not_to have_link "sign up", visible: :all
+ end
+ end
+end
diff --git a/spec/system/votes_spec.rb b/spec/system/votes_spec.rb
index fd78c4718..3dcfcbbb3 100644
--- a/spec/system/votes_spec.rb
+++ b/spec/system/votes_spec.rb
@@ -181,28 +181,6 @@ describe "Votes" do
describe "Proposals" do
before { login_as(verified) }
- scenario "Index shows user votes on proposals" do
- proposal1 = create(:proposal, voters: [verified])
- proposal2 = create(:proposal)
- proposal3 = create(:proposal)
-
- visit proposals_path
-
- within("#proposals") do
- within("#proposal_#{proposal1.id}_votes") do
- expect(page).to have_content "You have already supported this proposal. Share it!"
- end
-
- within("#proposal_#{proposal2.id}_votes") do
- expect(page).not_to have_content "You have already supported this proposal. Share it!"
- end
-
- within("#proposal_#{proposal3.id}_votes") do
- expect(page).not_to have_content "You have already supported this proposal. Share it!"
- end
- end
- end
-
describe "Single proposal" do
let!(:proposal) { create(:proposal) }