diff --git a/app/models/poll.rb b/app/models/poll.rb index f08f9bdcd..27125d8a1 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -90,8 +90,4 @@ class Poll < ActiveRecord::Base end end - def shifts_in_booth(booth_id) - officer_assignments.where(booth_assignment_id: booth_assignments.where(booth_id: booth_id).pluck(:id)).pluck(:officer_id).uniq - end - end diff --git a/app/models/poll/booth_assignment.rb b/app/models/poll/booth_assignment.rb index a1d0c0d39..81759ee0f 100644 --- a/app/models/poll/booth_assignment.rb +++ b/app/models/poll/booth_assignment.rb @@ -3,23 +3,26 @@ class Poll belongs_to :booth belongs_to :poll + before_destroy :destroy_poll_shifts, only: :destroy + has_many :officer_assignments, class_name: "Poll::OfficerAssignment", dependent: :destroy has_many :officers, through: :officer_assignments has_many :voters has_many :partial_results has_many :recounts - before_destroy :destroy_poll_shifts - - def has_shifts? - + def shifts? + shifts.empty? ? false : true 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 -# officers = poll.officers_in_booth(booth.id) -# Poll::Shift.where(officer_id: officers, booth_id: booth.id) + shifts.destroy_all end end end diff --git a/app/views/admin/poll/booth_assignments/_booth_assignment.html.erb b/app/views/admin/poll/booth_assignments/_booth_assignment.html.erb index b1ab3885e..b3b711f9f 100644 --- a/app/views/admin/poll/booth_assignments/_booth_assignment.html.erb +++ b/app/views/admin/poll/booth_assignments/_booth_assignment.html.erb @@ -15,7 +15,7 @@ remote: true, title: t("admin.booth_assignments.manage.actions.unassign"), class: "button hollow alert #{@poll.expired? ? 'disabled' : ''}", - data: (booth_assignment.has_shifts? ? {confirm: "Are you sure?"} : nil) %> + data: (booth_assignment.shifts? ? {confirm: "#{t("admin.poll_booth_assignments.alert.shifts")}"} : nil) %> <% else %> diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index a2062f8ce..c36ba0b94 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -544,6 +544,8 @@ en: assign: Assign booth unassign: Unassign booth poll_booth_assignments: + alert: + shifts: "There are shifts associated to this booth. If you remove the booth assignment, the shifts will be also deleted. Continue?" flash: destroy: "Booth not assigned anymore" create: "Booth assigned" diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 1c7d0fa74..cc3683bf4 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -544,6 +544,8 @@ es: assign: Assign booth unassign: Unassign booth poll_booth_assignments: + alert: + shifts: "Hay turnos asignados para esta urna. Si la desasignas, esos turnos se eliminarán. ¿Deseas continuar?" flash: destroy: "Urna desasignada" create: "Urna asignada"