Just like we do in the Budget module, and in some places in the Poll and Legislation modules, we don't need to specify the class name when the name of the relation matches the name of a class in the same module.
33 lines
749 B
Ruby
33 lines
749 B
Ruby
class Poll
|
|
class BoothAssignment < ApplicationRecord
|
|
belongs_to :booth
|
|
belongs_to :poll
|
|
|
|
before_destroy :destroy_poll_shifts, only: :destroy
|
|
|
|
has_many :officer_assignments, dependent: :destroy
|
|
has_many :officers, through: :officer_assignments
|
|
has_many :voters
|
|
has_many :partial_results
|
|
has_many :recounts
|
|
|
|
def shifts?
|
|
shifts.empty? ? false : true
|
|
end
|
|
|
|
def unable_to_destroy?
|
|
(partial_results.count + recounts.count).positive?
|
|
end
|
|
|
|
private
|
|
|
|
def shifts
|
|
Poll::Shift.where(booth_id: booth_id, officer_id: officer_assignments.pluck(:officer_id), date: officer_assignments.pluck(:date))
|
|
end
|
|
|
|
def destroy_poll_shifts
|
|
shifts.destroy_all
|
|
end
|
|
end
|
|
end
|