extracts officer_assignments from admin's poll/show

This commit is contained in:
Juanjo Bazán
2017-02-14 13:47:33 +01:00
parent 20e31133a8
commit 19ec7f93b3
13 changed files with 225 additions and 204 deletions

View File

@@ -1,11 +1,16 @@
class Admin::Poll::OfficerAssignmentsController < Admin::BaseController
before_action :redirect_if_blank_required_params, only: [:index]
before_action :load_poll
before_action :redirect_if_blank_required_params, only: [:by_officer]
before_action :load_booth_assignment, only: [:create]
def index
@poll = ::Poll.includes(:booths).find(officer_assignment_params[:poll])
@officer = ::Poll::Officer.includes(:user).find(officer_assignment_params[:officer])
@officers = @poll.officer_assignments.includes(officer: :user).select(:officer_id).distinct.map(&:officer)
end
def by_officer
@poll = ::Poll.includes(:booths).find(params[:poll_id])
@officer = ::Poll::Officer.includes(:user).find(officer_assignment_params[:officer_id])
@officer_assignments = ::Poll::OfficerAssignment.
joins(:booth_assignment).
includes(:recount, :final_recounts, booth_assignment: :booth).
@@ -13,6 +18,15 @@ class Admin::Poll::OfficerAssignmentsController < Admin::BaseController
order(:date)
end
def search_officers
load_search
@officers = User.joins(:poll_officer).search(@search).order(username: :asc)
respond_to do |format|
format.js
end
end
def create
@officer_assignment = ::Poll::OfficerAssignment.new(booth_assignment: @booth_assignment,
officer_id: create_params[:officer_id],
@@ -24,7 +38,7 @@ class Admin::Poll::OfficerAssignmentsController < Admin::BaseController
else
notice = t("admin.poll_officer_assignments.flash.error_create")
end
redirect_to admin_officer_assignments_path(officer: create_params[:officer_id], poll: create_params[:poll_id]), notice: notice
redirect_to by_officer_admin_poll_officer_assignments_path(poll_id: create_params[:poll_id], officer_id: create_params[:officer_id]), notice: notice
end
def destroy
@@ -35,13 +49,13 @@ class Admin::Poll::OfficerAssignmentsController < Admin::BaseController
else
notice = t("admin.poll_officer_assignments.flash.error_destroy")
end
redirect_to admin_officer_assignments_path(officer: @officer_assignment.officer_id, poll: @officer_assignment.poll_id), notice: notice
redirect_to by_officer_admin_poll_officer_assignments_path(poll_id: @officer_assignment.poll_id, officer_id: @officer_assignment.officer_id), notice: notice
end
private
def officer_assignment_params
params.permit(:officer, :poll)
params.permit(:officer_id)
end
def create_params
@@ -52,14 +66,22 @@ class Admin::Poll::OfficerAssignmentsController < Admin::BaseController
@booth_assignment = ::Poll::BoothAssignment.includes(:poll).find_by(poll_id: create_params[:poll_id], booth_id: create_params[:booth_id])
end
def redirect_if_blank_required_params
if officer_assignment_params[:officer].blank?
if officer_assignment_params[:poll].blank?
redirect_to admin_polls_path
else
redirect_to admin_poll_path(officer_assignment_params[:poll])
def load_poll
@poll = ::Poll.find(params[:poll_id])
end
def redirect_if_blank_required_params
if officer_assignment_params[:officer_id].blank?
redirect_to admin_poll_path(@poll)
end
end
def search_params
params.permit(:poll_id, :search)
end
def load_search
@search = search_params[:search]
end
end

View File

@@ -9,11 +9,9 @@ class Admin::Poll::PollsController < Admin::BaseController
def show
@poll = Poll.includes(:questions,
booth_assignments: [:booth,
:final_recounts,
:recounts],
officers: [:user]).
order('poll_questions.title', 'poll_booths.name', 'users.username').
booth_assignments: [:final_recounts,
:recounts]).
order('poll_questions.title').
find(params[:id])
end
@@ -70,14 +68,6 @@ class Admin::Poll::PollsController < Admin::BaseController
end
end
def search_officers
@officers = User.joins(:poll_officer).search(@search).order(username: :asc)
respond_to do |format|
format.js
end
end
private
def load_geozones
@geozones = Geozone.all.order(:name)

View File

@@ -1,6 +1,6 @@
<div class="row">
<div class="small-12 medium-6 column">
<%= form_tag(search_officers_admin_poll_path(@poll), method: :get, remote: true) do |f| %>
<%= form_tag(search_officers_admin_poll_officer_assignments_path(@poll), method: :get, remote: true) do |f| %>
<div class="input-group">
<%= text_field_tag :search,
@search,

View File

@@ -10,8 +10,8 @@
<table>
<thead>
<tr>
<th><%= t("admin.polls.show.table_name") %></th>
<th><%= t("admin.polls.show.table_email") %></th>
<th><%= t("admin.poll_officer_assignments.index.table_name") %></th>
<th><%= t("admin.poll_officer_assignments.index.table_email") %></th>
<th class="text-center"><%= t("admin.polls.show.table_assignment") %></th>
</tr>
</thead>
@@ -26,12 +26,12 @@
</td>
<td class="text-center">
<% if @poll.officer_ids.include?(user.poll_officer.id) %>
<%= link_to t("admin.polls.show.edit_officer_assignments"),
admin_officer_assignments_path(poll: @poll, officer: user.poll_officer),
<%= link_to t("admin.poll_officer_assignments.index.edit_officer_assignments"),
by_officer_admin_poll_officer_assignments_path(@poll, officer_id: user.poll_officer.id),
class: "button hollow alert" %>
<% else %>
<%= link_to t("admin.polls.show.add_officer_assignments"),
admin_officer_assignments_path(poll: @poll, officer: user.poll_officer),
<%= link_to t("admin.poll_officer_assignments.index.add_officer_assignments"),
by_officer_admin_poll_officer_assignments_path(@poll, officer_id: user.poll_officer.id),
class: "button hollow" %>
<% end %>
</td>

View File

@@ -0,0 +1,127 @@
<%= link_to admin_poll_officer_assignments_path(@poll) do %>
<span class="icon-angle-left"></span>
<%= @poll.name %>
<% end %>
<h2><%= @officer.name %> - <%= @officer.email %></h2>
<%= form_tag(admin_poll_officer_assignments_path(@poll), {id: "officer_assignment_form"}) do %>
<fieldset class="fieldset">
<legend><%= t("admin.poll_officer_assignments.by_officer.new_assignment") %></legend>
<div class="small-12 medium-4 column">
<label><%= t("admin.poll_officer_assignments.by_officer.date") %></label>
<%= select_tag :date,
poll_dates_select_options(@poll) + poll_final_recount_option(@poll),
{ prompt: t("admin.poll_officer_assignments.by_officer.select_date"),
label: false } %>
</div>
<div class="small-12 medium-4 column">
<label><%= t("admin.poll_officer_assignments.by_officer.booth") %></label>
<%= select_tag :booth_id,
poll_booths_select_options(@poll),
{ prompt: t("admin.poll_officer_assignments.by_officer.select_booth"),
label: false } %>
</div>
<div class="small-12 medium-4 column">
<%= hidden_field_tag :officer_id, @officer.id %>
<%= hidden_field_tag :poll_id, @poll.id %>
<%= submit_tag t("admin.poll_officer_assignments.by_officer.add_assignment"),
class: "button expanded hollow margin-top" %>
</div>
</fieldset>
<% end %>
<% if @officer_assignments.empty? %>
<div class="callout primary margin-top">
<%= t("admin.poll_officer_assignments.by_officer.no_assignments") %>
</div>
<% else %>
<h3><%= t("admin.poll_officer_assignments.by_officer.assignments") %></h3>
<table class="fixed">
<thead>
<tr>
<th><%= t("admin.poll_officer_assignments.by_officer.date") %></th>
<th><%= t("admin.poll_officer_assignments.by_officer.booth") %></th>
<th class="text-right"><%= t("admin.poll_officer_assignments.by_officer.assignment") %></th>
</tr>
</thead>
<tbody>
<% @officer_assignments.each do |officer_assignment| %>
<tr id="<%= dom_id officer_assignment %>">
<td><%= officer_assignment.final? ? t('polls.final_date') : l(officer_assignment.date.to_date) %></td>
<td><%= booth_name_with_location(officer_assignment.booth_assignment.booth) %></td>
<td class="text-right">
<%= link_to t("admin.poll_officer_assignments.by_officer.remove_assignment"),
admin_poll_officer_assignment_path(@poll, officer_assignment),
method: :delete,
class: "button hollow alert" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<% voting_days_officer_assignments = @officer_assignments.select{|oa| oa.final == false} %>
<% if voting_days_officer_assignments.any? %>
<h3><%= t("admin.poll_officer_assignments.by_officer.recounts") %></h3>
<table id="recount_list" class="fixed">
<thead>
<tr>
<th><%= t("admin.poll_officer_assignments.by_officer.date") %></th>
<th><%= t("admin.poll_officer_assignments.by_officer.booth") %></th>
<th class="text-right"><%= t("admin.poll_officer_assignments.by_officer.recount") %></th>
</tr>
</thead>
<tbody>
<% voting_days_officer_assignments.each do |officer_assignment| %>
<tr id="recount_<%= officer_assignment.date.to_date.strftime('%Y%m%d') %>">
<td><%= l(officer_assignment.date.to_date) %></td>
<td><%= booth_name_with_location(officer_assignment.booth_assignment.booth) %></td>
<td class="text-right">
<% if officer_assignment.recount.present? %>
<%= officer_assignment.recount.count %>
<% else %>
<span>-</span>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<% final_officer_assignments = @officer_assignments.select{|oa| oa.final == true} %>
<% if final_officer_assignments.any? %>
<h3><%= t("admin.poll_officer_assignments.by_officer.final_recounts") %></h3>
<table id="final_recount_list" class="fixed">
<thead>
<tr>
<th><%= t("admin.poll_officer_assignments.by_officer.date") %></th>
<th><%= t("admin.poll_officer_assignments.by_officer.booth") %></th>
<th class="text-right"><%= t("admin.poll_officer_assignments.by_officer.final_recount") %></th>
</tr>
</thead>
<tbody>
<% final_officer_assignments.each do |officer_assignment| %>
<tr id="final_recount_<%= officer_assignment.date.to_date.strftime('%Y%m%d') %>">
<td><%= l(officer_assignment.date.to_date) %></td>
<td><%= booth_name_with_location(officer_assignment.booth_assignment.booth) %></td>
<td class="text-right">
<% if officer_assignment.final_recounts.any? %>
<%= officer_assignment.final_recounts.to_a.sum(&:count) %>
<% else %>
<span>-</span>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>

View File

@@ -1,127 +1,40 @@
<%= link_to admin_poll_path(@poll, anchor: 'tab-officers') do %>
<span class="icon-angle-left"></span>
<%= @poll.name %>
<% end %>
<%= render "/admin/poll/polls/poll_header" %>
<div id="poll-resources">
<%= render "/admin/poll/polls/filter_subnav" %>
<%= render "search_officers" %>
<h2><%= @officer.name %> - <%= @officer.email %></h2>
<h3><%= t("admin.poll_officer_assignments.index.officers_title") %></h3>
<%= form_tag(admin_officer_assignments_path, {id: "officer_assignment_form"}) do %>
<fieldset class="fieldset">
<legend><%= t("admin.poll_officer_assignments.index.new_assignment") %></legend>
<div class="small-12 medium-4 column">
<label><%= t("admin.poll_officer_assignments.index.date") %></label>
<%= select_tag :date,
poll_dates_select_options(@poll) + poll_final_recount_option(@poll),
{ prompt: t("admin.poll_officer_assignments.index.select_date"),
label: false } %>
</div>
<div class="small-12 medium-4 column">
<label><%= t("admin.poll_officer_assignments.index.booth") %></label>
<%= select_tag :booth_id,
poll_booths_select_options(@poll),
{ prompt: t("admin.poll_officer_assignments.index.select_booth"),
label: false } %>
</div>
<div class="small-12 medium-4 column">
<%= hidden_field_tag :officer_id, @officer.id %>
<%= hidden_field_tag :poll_id, @poll.id %>
<%= submit_tag t("admin.poll_officer_assignments.index.add_assignment"),
class: "button expanded hollow margin-top" %>
</div>
</fieldset>
<% end %>
<% if @officer_assignments.empty? %>
<% if @officers.empty? %>
<div class="callout primary margin-top">
<%= t("admin.poll_officer_assignments.index.no_assignments") %>
<%= t("admin.poll_officer_assignments.index.no_officers") %>
</div>
<% else %>
<h3><%= t("admin.poll_officer_assignments.index.assignments") %></h3>
<table class="fixed">
<thead>
<tr>
<th><%= t("admin.poll_officer_assignments.index.date") %></th>
<th><%= t("admin.poll_officer_assignments.index.booth") %></th>
<th class="text-right"><%= t("admin.poll_officer_assignments.index.assignment") %></th>
</tr>
</thead>
<tbody>
<% @officer_assignments.each do |officer_assignment| %>
<tr id="<%= dom_id officer_assignment %>">
<td><%= officer_assignment.final? ? t('polls.final_date') : l(officer_assignment.date.to_date) %></td>
<td><%= booth_name_with_location(officer_assignment.booth_assignment.booth) %></td>
<td class="text-right">
<%= link_to t("admin.poll_officer_assignments.index.remove_assignment"),
admin_officer_assignment_path(officer_assignment),
method: :delete,
class: "button hollow alert" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<% voting_days_officer_assignments = @officer_assignments.select{|oa| oa.final == false} %>
<% if voting_days_officer_assignments.any? %>
<h3><%= t("admin.poll_officer_assignments.index.recounts") %></h3>
<table id="recount_list" class="fixed">
<thead>
<tr>
<th><%= t("admin.poll_officer_assignments.index.date") %></th>
<th><%= t("admin.poll_officer_assignments.index.booth") %></th>
<th class="text-right"><%= t("admin.poll_officer_assignments.index.recount") %></th>
</tr>
</thead>
<tbody>
<% voting_days_officer_assignments.each do |officer_assignment| %>
<tr id="recount_<%= officer_assignment.date.to_date.strftime('%Y%m%d') %>">
<td><%= l(officer_assignment.date.to_date) %></td>
<td><%= booth_name_with_location(officer_assignment.booth_assignment.booth) %></td>
<td class="text-right">
<% if officer_assignment.recount.present? %>
<%= officer_assignment.recount.count %>
<% else %>
<span>-</span>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<% final_officer_assignments = @officer_assignments.select{|oa| oa.final == true} %>
<% if final_officer_assignments.any? %>
<h3><%= t("admin.poll_officer_assignments.index.final_recounts") %></h3>
<table id="final_recount_list" class="fixed">
<table class="fixed margin">
<thead>
<tr>
<th><%= t("admin.poll_officer_assignments.index.date") %></th>
<th><%= t("admin.poll_officer_assignments.index.booth") %></th>
<th class="text-right"><%= t("admin.poll_officer_assignments.index.final_recount") %></th>
</tr>
<th><%= t("admin.poll_officer_assignments.index.table_name") %></th>
<th><%= t("admin.poll_officer_assignments.index.table_email") %></th>
<th class="text-right"><%= t("admin.actions.actions") %></th>
</thead>
<tbody>
<% final_officer_assignments.each do |officer_assignment| %>
<tr id="final_recount_<%= officer_assignment.date.to_date.strftime('%Y%m%d') %>">
<td><%= l(officer_assignment.date.to_date) %></td>
<td><%= booth_name_with_location(officer_assignment.booth_assignment.booth) %></td>
<% @officers.each do |officer| %>
<tr id="officer_<%= officer.id %>" class="officer">
<td>
<strong>
<%= link_to officer.name, by_officer_admin_poll_officer_assignments_path(@poll, officer_id: officer.id) %>
</strong>
</td>
<td>
<%= officer.email %>
</td>
<td class="text-right">
<% if officer_assignment.final_recounts.any? %>
<%= officer_assignment.final_recounts.to_a.sum(&:count) %>
<% else %>
<span>-</span>
<% end %>
<%= link_to t("admin.poll_officer_assignments.index.edit_officer_assignments"),
by_officer_admin_poll_officer_assignments_path(@poll, officer_id: officer.id),
class: "button hollow" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<% end %>
</div>

View File

@@ -12,7 +12,7 @@
<% end %>
</li>
<li>
<%= link_to "#tab-officers" do %>
<%= link_to admin_poll_officer_assignments_path(@poll) do %>
<%= t("admin.polls.show.officers_tab") %>
(<%= @poll.officer_assignments.select(:officer_id).distinct.count %>)
<% end %>

View File

@@ -1,34 +0,0 @@
<h3><%= t("admin.polls.show.officers_title") %></h3>
<% if @poll.officers.empty? %>
<div class="callout primary margin-top">
<%= t("admin.polls.show.no_officers") %>
</div>
<% else %>
<table class="fixed margin">
<thead>
<th><%= t("admin.polls.show.table_name") %></th>
<th><%= t("admin.polls.show.table_email") %></th>
<th class="text-right"><%= t("admin.actions.actions") %></th>
</thead>
<tbody>
<% @poll.officers.uniq.each do |officer| %>
<tr id="officer_<%= officer.id %>" class="officer">
<td>
<strong>
<%= link_to officer.name, admin_officer_assignments_path(officer: officer, poll: @poll) %>
</strong>
</td>
<td>
<%= officer.email %>
</td>
<td class="text-right">
<%= link_to t("admin.polls.show.edit_officer_assignments"),
admin_officer_assignments_path(officer: officer, poll: @poll),
class: "button hollow" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>

View File

@@ -8,11 +8,6 @@
<%= render "questions" %>
</div>
<div class="tabs-panel" id="tab-officers">
<%= render "search_officers" %>
<%= render 'officers' %>
</div>
<div class="tabs-panel" id="tab-recounts">
<%= render 'recounting' %>
</div>

View File

@@ -271,6 +271,13 @@ en:
error_destroy: "An error ocurred when removing officer assignment"
error_create: "An error ocurred when adding officer assignment"
index:
officers_title: "List of officers"
no_officers: "There are no officers assigned to this poll."
table_name: "Name"
table_email: "Email"
add_officer_assignments: "Add shifts as officer"
edit_officer_assignments: "Edit officing shifts"
by_officer:
new_assignment: "New shift"
date: "Date"
booth: "Booth"
@@ -331,21 +338,16 @@ en:
recounts_tab: Recounting
results_tab: Results
no_questions: "There are no questions assigned to this poll."
no_officers: "There are no officers assigned to this poll."
no_recounts: "There is nothing to be recounted"
no_results: "There are no results"
officers_title: "List of officers"
questions_title: "List of questions"
recounting_title: "Recounts"
results_title: "Results"
remove_question: "Remove question from poll"
add_question: "Include question"
add_officer_assignments: "Add shifts as officer"
edit_officer_assignments: "Edit officing shifts"
table_title: "Title"
table_assignment: "Assignment"
table_name: "Name"
table_email: "Email"
table_booth_name: "Booth"
table_final_recount: "Final recount (by officer)"
table_recounts: "Accumulated daily recounts (by officer)"

View File

@@ -271,6 +271,13 @@ es:
error_destroy: "Se ha producido un error al eliminar el turno"
error_create: "Se ha producido un error al intentar crear el turno"
index:
officers_title: "Listado de presidentes de mesa asignados"
no_officers: "No hay presidentes de mesa asignados a esta votación."
table_name: "Nombre"
table_email: "Email"
add_officer_assignments: "Añadir turnos como presidente de mesa"
edit_officer_assignments: "Editar turnos"
by_officer:
new_assignment: "Nuevo turno"
date: "Fecha"
booth: "Urna"
@@ -331,21 +338,16 @@ es:
recounts_tab: Recuentos
results_tab: Resultados
no_questions: "No hay preguntas asignadas a esta votación."
no_officers: "No hay presidentes de mesa asignados a esta votación."
no_recounts: "No hay nada de lo que hacer recuento"
no_results: "No hay resultados"
officers_title: "Listado de presidentes de mesa asignados"
questions_title: "Listado de preguntas asignadas"
recounting_title: "Recuentos"
results_title: "Resultados"
remove_question: "Desasignar pregunta"
add_question: "Incluir pregunta"
add_officer_assignments: "Añadir turnos como presidente de mesa"
edit_officer_assignments: "Editar turnos"
table_title: "Título"
table_assignment: "Asignación"
table_name: "Nombre"
table_email: "Email"
table_booth_name: "Urna"
table_final_recount: "Recuento final (presidente de mesa)"
table_recounts: "Recuentos diarios acumulados (presidente de mesa)"

View File

@@ -216,6 +216,11 @@ Rails.application.routes.draw do
resources :booth_assignments, only: [:index, :show, :create, :destroy] do
get :search_booths, on: :collection
end
resources :officer_assignments, only: [:index, :create, :destroy] do
get :search_officers, on: :collection
get :by_officer, on: :collection
end
end
resources :officers do
@@ -223,7 +228,6 @@ Rails.application.routes.draw do
end
resources :booths
resources :officer_assignments, only: [:index, :create, :destroy]
resources :questions
end