Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport] Display 'Validate document' menu item only when applicable #3343

Merged
merged 2 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions app/models/poll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,42 @@ def self.answerable_by(user)
.where('geozone_restricted = ? OR geozones_polls.geozone_id = ?', false, user.geozone_id)
end

def self.votable_by(user)
answerable_by(user).
not_voted_by(user)
end

def votable_by?(user)
!document_has_voted?(user.document_number, user.document_type)
answerable_by?(user) &&
not_voted_by?(user)
end

def document_has_voted?(document_number, document_type)
voters.where(document_number: document_number, document_type: document_type).exists?
def self.not_voted_by(user)
where("polls.id not in (?)", poll_ids_voted_by(user))
end

def voted_in_booth?(user)
Poll::Voter.where(poll: self, user: user, origin: "booth").exists?
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 voted_in_web?(user)
Poll::Voter.where(poll: self, user: user, origin: "web").exists?
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

def voted_in_booth?(user)
Poll::Voter.where(poll: self, user: user, origin: "booth").exists?
end

def voted_in_web?(user)
Poll::Voter.where(poll: self, user: user, origin: "web").exists?
end

def date_range
unless starts_at.present? && ends_at.present? && starts_at <= ends_at
errors.add(:starts_at, I18n.t('errors.messages.invalid_date_range'))
Expand Down
4 changes: 3 additions & 1 deletion app/views/officing/_menu.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<div class="admin-sidebar" data-equalizer-watch>
<ul id="officing_menu">
<% 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 %>
<span class="icon-user"></span>
<%= t("officing.menu.voters") %>
Expand Down
3 changes: 2 additions & 1 deletion app/views/officing/voters/create.js.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
$("#<%= 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");
44 changes: 43 additions & 1 deletion spec/features/polls/voter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,48 @@
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
64 changes: 64 additions & 0 deletions spec/models/poll/poll_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,70 @@
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
it "return false if the user has not voted for this poll" do
user = create(:user, :level_two)
Expand Down