From b96cfbc837d1982ebcddb541d2d388883a2a8b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baza=CC=81n?= Date: Mon, 30 Jan 2017 11:52:06 +0100 Subject: [PATCH] adds methods to poll_officer to return assigned polls --- app/controllers/officing/polls_controller.rb | 2 +- app/models/poll/officer.rb | 11 ++- spec/models/poll/officer_spec.rb | 85 +++++++++++++++++++- 3 files changed, 91 insertions(+), 7 deletions(-) diff --git a/app/controllers/officing/polls_controller.rb b/app/controllers/officing/polls_controller.rb index 4841baa39..46ca9da51 100644 --- a/app/controllers/officing/polls_controller.rb +++ b/app/controllers/officing/polls_controller.rb @@ -1,7 +1,7 @@ class Officing::PollsController < Officing::BaseController def index - @polls = current_user.poll_officer? ? current_user.poll_officer.assigned_polls : [] + @polls = current_user.poll_officer? ? current_user.poll_officer.voting_days_assigned_polls : [] @polls = @polls.select {|poll| poll.current?(Time.current) || poll.current?(1.day.ago)} end diff --git a/app/models/poll/officer.rb b/app/models/poll/officer.rb index 138a582fc..81a2afd5d 100644 --- a/app/models/poll/officer.rb +++ b/app/models/poll/officer.rb @@ -7,8 +7,15 @@ class Poll delegate :name, :email, to: :user - def assigned_polls - officer_assignments.includes(booth_assignment: :poll). + def voting_days_assigned_polls + officer_assignments.voting_days.includes(booth_assignment: :poll). + map(&:booth_assignment). + map(&:poll).uniq.compact. + sort {|x, y| y.ends_at <=> x.ends_at} + end + + def final_days_assigned_polls + officer_assignments.final.includes(booth_assignment: :poll). map(&:booth_assignment). map(&:poll).uniq.compact. sort {|x, y| y.ends_at <=> x.ends_at} diff --git a/spec/models/poll/officer_spec.rb b/spec/models/poll/officer_spec.rb index cea0e8bae..83bdbecc2 100644 --- a/spec/models/poll/officer_spec.rb +++ b/spec/models/poll/officer_spec.rb @@ -2,8 +2,8 @@ require 'rails_helper' describe :officer do - describe "#assigned_polls" do - it "should return all polls with this officer assigned" do + describe "#voting_days_assigned_polls" do + it "should return all polls with this officer assigned during voting days" do officer = create(:poll_officer) poll_1 = create(:poll) @@ -18,13 +18,31 @@ describe :officer do create(:poll_officer_assignment, booth_assignment: booth_assignment_1b, officer: officer, date: poll_1.ends_at) create(:poll_officer_assignment, booth_assignment: booth_assignment_2, officer: officer) - assigned_polls = officer.assigned_polls + assigned_polls = officer.voting_days_assigned_polls expect(assigned_polls.size).to eq 2 expect(assigned_polls.include?(poll_1)).to eq(true) expect(assigned_polls.include?(poll_2)).to eq(true) expect(assigned_polls.include?(poll_3)).to eq(false) end + it "should not return polls with this officer assigned for final recount/results" do + officer = create(:poll_officer) + + poll_1 = create(:poll) + poll_2 = create(:poll) + + booth_assignment_1 = create(:poll_booth_assignment, poll: poll_1) + booth_assignment_2 = create(:poll_booth_assignment, poll: poll_2) + + create(:poll_officer_assignment, booth_assignment: booth_assignment_1, officer: officer, date: poll_1.starts_at) + create(:poll_officer_assignment, booth_assignment: booth_assignment_2, officer: officer, final: true) + + assigned_polls = officer.voting_days_assigned_polls + expect(assigned_polls.size).to eq 1 + expect(assigned_polls.include?(poll_1)).to eq(true) + expect(assigned_polls.include?(poll_2)).to eq(false) + end + it "should return polls ordered by end date (desc)" do officer = create(:poll_officer) @@ -36,7 +54,7 @@ describe :officer do create(:poll_officer_assignment, officer: officer, booth_assignment: create(:poll_booth_assignment, poll: p)) end - assigned_polls = officer.assigned_polls + assigned_polls = officer.voting_days_assigned_polls expect(assigned_polls.first).to eq(poll_2) expect(assigned_polls.second).to eq(poll_1) @@ -44,4 +62,63 @@ describe :officer do end end + describe "#final_days_assigned_polls" do + it "should return all polls with this officer assigned for final recount/results" do + officer = create(:poll_officer) + + poll_1 = create(:poll) + poll_2 = create(:poll) + poll_3 = create(:poll) + + booth_assignment_1a = create(:poll_booth_assignment, poll: poll_1) + booth_assignment_1b = create(:poll_booth_assignment, poll: poll_1) + booth_assignment_2 = create(:poll_booth_assignment, poll: poll_2) + + create(:poll_officer_assignment, booth_assignment: booth_assignment_1a, officer: officer, date: poll_1.starts_at, final: true) + create(:poll_officer_assignment, booth_assignment: booth_assignment_1b, officer: officer, date: poll_1.ends_at, final: true) + create(:poll_officer_assignment, booth_assignment: booth_assignment_2, officer: officer, final: true) + + assigned_polls = officer.final_days_assigned_polls + expect(assigned_polls.size).to eq 2 + expect(assigned_polls.include?(poll_1)).to eq(true) + expect(assigned_polls.include?(poll_2)).to eq(true) + expect(assigned_polls.include?(poll_3)).to eq(false) + end + + it "should not return polls with this officer assigned for voting days" do + officer = create(:poll_officer) + + poll_1 = create(:poll) + poll_2 = create(:poll) + + booth_assignment_1 = create(:poll_booth_assignment, poll: poll_1) + booth_assignment_2 = create(:poll_booth_assignment, poll: poll_2) + + create(:poll_officer_assignment, booth_assignment: booth_assignment_1, officer: officer, date: poll_1.starts_at) + create(:poll_officer_assignment, booth_assignment: booth_assignment_2, officer: officer, final: true) + + assigned_polls = officer.final_days_assigned_polls + expect(assigned_polls.size).to eq 1 + expect(assigned_polls.include?(poll_1)).to eq(false) + expect(assigned_polls.include?(poll_2)).to eq(true) + end + + it "should return polls ordered by end date (desc)" do + officer = create(:poll_officer) + + poll_1 = create(:poll, ends_at: 1.day.ago) + poll_2 = create(:poll, ends_at: 10.days.from_now) + poll_3 = create(:poll, ends_at: 10.day.ago) + + [poll_1, poll_2, poll_3].each do |p| + create(:poll_officer_assignment, officer: officer, booth_assignment: create(:poll_booth_assignment, poll: p), final: true) + end + + assigned_polls = officer.final_days_assigned_polls + + expect(assigned_polls.first).to eq(poll_2) + expect(assigned_polls.second).to eq(poll_1) + expect(assigned_polls.last).to eq(poll_3) + end + end end \ No newline at end of file