Merge pull request #3343 from consul/backport-budget_poll_menu

Display 'Validate document' menu item only when applicable
This commit is contained in:
Javier Martín
2019-03-06 19:12:35 +01:00
committed by GitHub
5 changed files with 136 additions and 11 deletions

View File

@@ -65,12 +65,32 @@ class Poll < ActiveRecord::Base
.where('geozone_restricted = ? OR geozones_polls.geozone_id = ?', false, user.geozone_id) .where('geozone_restricted = ? OR geozones_polls.geozone_id = ?', false, user.geozone_id)
end end
def votable_by?(user) def self.votable_by(user)
!document_has_voted?(user.document_number, user.document_type) answerable_by(user).
not_voted_by(user)
end end
def document_has_voted?(document_number, document_type) def votable_by?(user)
voters.where(document_number: document_number, document_type: document_type).exists? answerable_by?(user) &&
not_voted_by?(user)
end
def self.not_voted_by(user)
where("polls.id not in (?)", poll_ids_voted_by(user))
end
def self.poll_ids_voted_by(user)
return -1 if Poll::Voter.where(user: user).empty?
Poll::Voter.where(user: user).pluck(:poll_id)
end
def not_voted_by?(user)
Poll::Voter.where(poll: self, user: user).empty?
end
def voted_by?(user)
Poll::Voter.where(poll: self, user: user).exists?
end end
def voted_in_booth?(user) def voted_in_booth?(user)
@@ -81,10 +101,6 @@ class Poll < ActiveRecord::Base
Poll::Voter.where(poll: self, user: user, origin: "web").exists? Poll::Voter.where(poll: self, user: user, origin: "web").exists?
end end
def voted_by?(user)
Poll::Voter.where(poll: self, user: user).exists?
end
def date_range def date_range
unless starts_at.present? && ends_at.present? && starts_at <= ends_at unless starts_at.present? && ends_at.present? && starts_at <= ends_at
errors.add(:starts_at, I18n.t('errors.messages.invalid_date_range')) errors.add(:starts_at, I18n.t('errors.messages.invalid_date_range'))

View File

@@ -1,7 +1,9 @@
<div class="admin-sidebar" data-equalizer-watch> <div class="admin-sidebar" data-equalizer-watch>
<ul id="officing_menu"> <ul id="officing_menu">
<% if vote_collection_shift? %> <% if vote_collection_shift? %>
<li <%= "class=is-active" if controller_name == "voters" %>> <li class="<%= "js-vote-collection" %>
<%= " is-active" if controller_name == "voters" %>
<%= " is-hidden" if controller_name == "voters" && Poll.votable_by(@user).any? %>">
<%= link_to new_officing_residence_path do %> <%= link_to new_officing_residence_path do %>
<span class="icon-user"></span> <span class="icon-user"></span>
<%= t("officing.menu.voters") %> <%= t("officing.menu.voters") %>

View File

@@ -1,2 +1,3 @@
$("#<%= dom_id(@poll) %> #actions").html('<%= j render("voted") %>'); $("#<%= dom_id(@poll) %> #actions").html('<%= j render("voted") %>');
$("#<%= dom_id(@poll) %> #can_vote_callout").hide(); $("#<%= dom_id(@poll) %> #can_vote_callout").hide();
$(".js-vote-collection").removeClass("is-hidden");

View File

@@ -164,6 +164,48 @@ feature "Voter" do
expect(Poll::Voter.count).to eq(1) expect(Poll::Voter.count).to eq(1)
end end
context "Side menu" do
scenario "'Validate document' menu item with votable polls", :js do
login_through_form_as_officer(officer.user)
visit new_officing_residence_path
officing_verify_residence
expect(page).to have_content poll.name
within("#side_menu") do
expect(page).not_to have_content("Validate document")
end
within("#poll_#{poll.id}") do
click_button("Confirm vote")
expect(page).to have_content "Vote introduced!"
end
within("#side_menu") do
expect(page).to have_content("Validate document")
end
end
scenario "'Validate document' menu item without votable polls", :js do
create(:poll_voter, poll: poll, user: create(:user, :in_census))
login_through_form_as_officer(officer.user)
visit new_officing_residence_path
officing_verify_residence
expect(page).to have_content poll.name
within("#poll_#{poll.id}") do
expect(page).to have_content "Has already participated in this poll"
end
within("#side_menu") do
expect(page).to have_content("Validate document")
end
end end
end end
end
end

View File

@@ -147,6 +147,70 @@ describe Poll do
end end
end end
describe "votable_by" do
it "returns polls that have not been voted by a user" do
user = create(:user, :level_two)
poll1 = create(:poll)
poll2 = create(:poll)
poll3 = create(:poll)
create(:poll_voter, user: user, poll: poll1)
expect(Poll.votable_by(user)).to include(poll2)
expect(Poll.votable_by(user)).to include(poll3)
expect(Poll.votable_by(user)).not_to include(poll1)
end
it "returns polls that are answerable by a user" do
user = create(:user, :level_two, geozone: nil)
poll1 = create(:poll)
poll2 = create(:poll)
allow(Poll).to receive(:answerable_by).and_return(Poll.where(id: poll1))
expect(Poll.votable_by(user)).to include(poll1)
expect(Poll.votable_by(user)).not_to include(poll2)
end
it "returns polls even if there are no voters yet" do
user = create(:user, :level_two)
poll = create(:poll)
expect(Poll.votable_by(user)).to include(poll)
end
end
describe "#votable_by" do
it "returns false if the user has already voted the poll" do
user = create(:user, :level_two)
poll = create(:poll)
create(:poll_voter, user: user, poll: poll)
expect(poll.votable_by?(user)).to eq(false)
end
it "returns false if the poll is not answerable by the user" do
user = create(:user, :level_two)
poll = create(:poll)
allow_any_instance_of(Poll).to receive(:answerable_by?).and_return(false)
expect(poll.votable_by?(user)).to eq(false)
end
it "return true if a poll is answerable and has not been voted by the user" do
user = create(:user, :level_two)
poll = create(:poll)
allow_any_instance_of(Poll).to receive(:answerable_by?).and_return(true)
expect(poll.votable_by?(user)).to eq(true)
end
end
describe "#voted_by?" do describe "#voted_by?" do
it "return false if the user has not voted for this poll" do it "return false if the user has not voted for this poll" do
user = create(:user, :level_two) user = create(:user, :level_two)