Files
grecia/app/models/poll/shift.rb
Javi Martín 8b13daad95 Add and apply rules for multi-line hashes
For the HashAlignment rule, we're using the default `key` style (keys
are aligned and values aren't) instead of the `table` style (both keys
and values are aligned) because, even if we used both in the
application, we used the `key` style a lot more. Furthermore, the
`table` style looks strange in places where there are both very long and
very short keys and sometimes we weren't even consistent with the
`table` style, aligning some keys without aligning other keys.

Ideally we could align hashes to "either key or table", so developers
can decide whether keeping the symmetry of the code is worth it in a
case-per-case basis, but Rubocop doesn't allow this option.
2023-08-18 14:56:16 +02:00

52 lines
1.5 KiB
Ruby

class Poll
class Shift < ApplicationRecord
belongs_to :booth
belongs_to :officer
validates :booth_id, presence: true
validates :officer_id, presence: true
validates :date, presence: true, uniqueness: { scope: [:officer_id, :booth_id, :task] }
validates :task, presence: true
enum task: { vote_collection: 0, recount_scrutiny: 1 }
scope :current, -> { where(date: Date.current) }
before_create :persist_data
after_create :create_officer_assignments
before_destroy :destroy_officer_assignments
def title
"#{I18n.t("admin.poll_shifts.#{task}")} #{officer_name} #{I18n.l(date.to_date, format: :long)}"
end
def persist_data
self.officer_name = officer.name
self.officer_email = officer.email
end
def create_officer_assignments
booth.booth_assignments.order(:id).each do |booth_assignment|
attrs = {
officer_id: officer_id,
date: date,
booth_assignment_id: booth_assignment.id,
final: recount_scrutiny?
}
Poll::OfficerAssignment.create!(attrs)
end
end
def unable_to_destroy?
booth.booth_assignments.map(&:unable_to_destroy?).any?
end
def destroy_officer_assignments
Poll::OfficerAssignment.where(booth_assignment: booth.booth_assignments,
officer: officer,
date: date,
final: recount_scrutiny?).destroy_all
end
end
end