adds assigned_polls method to poll_officer

This commit is contained in:
Juanjo Bazán
2017-01-02 20:28:16 +01:00
parent 55fea6c037
commit b3fd7ffafa
2 changed files with 54 additions and 0 deletions

View File

@@ -6,5 +6,12 @@ class Poll
validates :user_id, presence: true, uniqueness: true
delegate :name, :email, to: :user
def assigned_polls
officer_assignments.includes(booth_assignment: :poll).
map(&:booth_assignment).
map(&:poll).uniq.compact.
sort {|x, y| y.ends_at <=> x.ends_at}
end
end
end

View File

@@ -0,0 +1,47 @@
require 'rails_helper'
describe :officer do
describe "#assigned_polls" do
it "should return all polls with this officer assigned" 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)
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
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 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))
end
assigned_polls = officer.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