adds poll shift management to admin
This commit is contained in:
64
app/controllers/admin/poll/officer_assignments_controller.rb
Normal file
64
app/controllers/admin/poll/officer_assignments_controller.rb
Normal file
@@ -0,0 +1,64 @@
|
||||
class Admin::Poll::OfficerAssignmentsController < Admin::BaseController
|
||||
|
||||
before_action :redirect_if_blank_required_params, only: [:index]
|
||||
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])
|
||||
@officer_assignments = ::Poll::OfficerAssignment.
|
||||
joins(:booth_assignment).
|
||||
includes(booth_assignment: :booth).
|
||||
where("officer_id = ? AND poll_booth_assignments.poll_id = ?", @officer.id, @poll.id).
|
||||
order(:date)
|
||||
end
|
||||
|
||||
def create
|
||||
@officer_assignment = ::Poll::OfficerAssignment.new(booth_assignment: @booth_assignment,
|
||||
officer_id: create_params[:officer_id],
|
||||
date: create_params[:date])
|
||||
|
||||
if @officer_assignment.save
|
||||
notice = t("admin.poll_officer_assignments.flash.create")
|
||||
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
|
||||
end
|
||||
|
||||
def destroy
|
||||
@officer_assignment = ::Poll::OfficerAssignment.includes(:booth_assignment).find(params[:id])
|
||||
|
||||
if @officer_assignment.destroy
|
||||
notice = t("admin.poll_officer_assignments.flash.destroy")
|
||||
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
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def officer_assignment_params
|
||||
params.permit(:officer, :poll)
|
||||
end
|
||||
|
||||
def create_params
|
||||
params.permit(:poll_id, :booth_id, :date, :officer_id)
|
||||
end
|
||||
|
||||
def load_booth_assignment
|
||||
@booth_assignment = ::Poll::BoothAssignment.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])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
64
app/views/admin/poll/officer_assignments/index.html.erb
Normal file
64
app/views/admin/poll/officer_assignments/index.html.erb
Normal file
@@ -0,0 +1,64 @@
|
||||
<%= link_to @poll.name, admin_poll_path(@poll, anchor: 'tab-officers') %>
|
||||
<h2><%= @officer.name %> - <%= @officer.email %></h2>
|
||||
|
||||
<%= 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),
|
||||
{ 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? %>
|
||||
<div class="callout primary margin-top">
|
||||
<%= t("admin.poll_officer_assignments.index.no_assignments") %>
|
||||
</div>
|
||||
<% else %>
|
||||
<h3><%= t("admin.poll_officer_assignments.index.assignments") %></h3>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th><%= t("admin.poll_officer_assignments.index.date") %></th>
|
||||
<th colspan="2"><%= t("admin.poll_officer_assignments.index.booth") %></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @officer_assignments.each do |officer_assignment| %>
|
||||
<tr id="<%= dom_id officer_assignment %>">
|
||||
<td><%= 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 %>
|
||||
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<tr id="officer_<%= officer.id %>" class="officer">
|
||||
<td>
|
||||
<strong>
|
||||
<%= officer.name %>
|
||||
<%= link_to officer.name, admin_officer_assignments_path(officer: officer, poll: @poll) %>
|
||||
</strong>
|
||||
</td>
|
||||
<td>
|
||||
@@ -25,66 +25,4 @@
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
<% end %>
|
||||
|
||||
<!-- Assign booths to officer -->
|
||||
<h2>Clemente padilla Otero</h2>
|
||||
<p>user2@consul.dev</p>
|
||||
|
||||
|
||||
<fieldset class="fieldset">
|
||||
<legend>Nuevo turno</legend>
|
||||
<div class="small-12 medium-4 column">
|
||||
<label>Fecha</label>
|
||||
<select>
|
||||
<option>Seleccionar día</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="small-12 medium-4 column">
|
||||
<label>Urna</label>
|
||||
<select>
|
||||
<option>Seleccionar urna</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="small-12 medium-4 column">
|
||||
<input type="submit" value="Añadir turno" class="button expanded hollow margin-top">
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<a class="float-right">Añadir nuevo turno</a>
|
||||
|
||||
<h3>Turnos asignados</h3>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Fecha</th>
|
||||
<th colspan="2">Urna</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>13/02/2016</td>
|
||||
<td>Urna Moncloa</td>
|
||||
<td class="text-right">
|
||||
<%= link_to "Eliminar turno", "#", class: "button hollow alert" %>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>14/02/2016</td>
|
||||
<td>Urna Moncloa</td>
|
||||
<td class="text-right">
|
||||
<%= link_to "Eliminar turno", "#", class: "button hollow alert" %>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>15/02/2016</td>
|
||||
<td>Urna Chamartín</td>
|
||||
<td class="text-right">
|
||||
<%= link_to "Eliminar turno", "#", class: "button hollow alert" %>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<!-- /. Assign booths to officer -->
|
||||
<% end %>
|
||||
@@ -14,7 +14,15 @@
|
||||
<%= user.email %>
|
||||
</td>
|
||||
<td class="text-right">
|
||||
|
||||
<% 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),
|
||||
class: "button hollow alert" %>
|
||||
<% else %>
|
||||
<%= link_to t("admin.polls.show.add_officer_assignments"),
|
||||
admin_officer_assignments_path(poll: @poll, officer: user.poll_officer),
|
||||
class: "button hollow" %>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -161,6 +161,22 @@ en:
|
||||
email_placeholder: Search user by email
|
||||
search: Search
|
||||
user_not_found: User not found
|
||||
poll_officer_assignments:
|
||||
flash:
|
||||
destroy: "Officing shift removed"
|
||||
create: "Officing shift added"
|
||||
error_destroy: "An error ocurred when removing officer assignment"
|
||||
error_create: "An error ocurred when adding officer assignment"
|
||||
index:
|
||||
new_assignment: "New shift"
|
||||
date: "Date"
|
||||
booth: "Booth"
|
||||
select_date: "Select day"
|
||||
select_booth: "Select booth"
|
||||
add_assignment: "Add shift"
|
||||
remove_assignment: "Remove"
|
||||
assignments: "Officing shifts in this poll"
|
||||
no_assignments: "This user has no officing shifts in this poll"
|
||||
polls:
|
||||
index:
|
||||
title: "List of polls"
|
||||
@@ -192,6 +208,8 @@ en:
|
||||
remove_question: "Remove question from poll"
|
||||
add_booth: "Assign booth"
|
||||
add_question: "Include question"
|
||||
add_officer_assignments: "Add shifts as officer"
|
||||
edit_officer_assignments: "Edit officing shifts"
|
||||
name: "Name"
|
||||
location: "Location"
|
||||
email: "Email"
|
||||
|
||||
@@ -161,6 +161,22 @@ es:
|
||||
email_placeholder: Buscar usuario por email
|
||||
search: Buscar
|
||||
user_not_found: Usuario no encontrado
|
||||
poll_officer_assignments:
|
||||
flash:
|
||||
destroy: "Eliminado turno de presidente de mesa"
|
||||
create: "Añadido turno de presidente de mesa"
|
||||
error_destroy: "Se ha producido un error al eliminar el turno"
|
||||
error_create: "Se ha producido un error al intentar crear el turno"
|
||||
index:
|
||||
new_assignment: "Nuevo turno"
|
||||
date: "Fecha"
|
||||
booth: "Urna"
|
||||
select_date: "Seleccionar día"
|
||||
select_booth: "Seleccionar urna"
|
||||
add_assignment: "Añadir turno"
|
||||
remove_assignment: "Eliminar turno"
|
||||
assignments: "Turnos como presidente de mesa en esta votación"
|
||||
no_assignments: "No tiene turnos como presidente de mesa en esta votación"
|
||||
polls:
|
||||
index:
|
||||
title: "Listado de votaciones"
|
||||
@@ -192,6 +208,8 @@ es:
|
||||
remove_question: "Desasignar pregunta"
|
||||
add_booth: "Asignar urna"
|
||||
add_question: "Incluir pregunta"
|
||||
add_officer_assignments: "Añadir turnos como presidente de mesa"
|
||||
edit_officer_assignments: "Editar turnos"
|
||||
name: "Nombre"
|
||||
location: "Ubicación"
|
||||
email: "Email"
|
||||
|
||||
@@ -197,7 +197,7 @@ Rails.application.routes.draw do
|
||||
|
||||
resources :booths
|
||||
resources :booth_assignments, only: [:create, :destroy]
|
||||
resources :officer_assignments, only: [:new, :create, :destroy]
|
||||
resources :officer_assignments, only: [:index, :create, :destroy]
|
||||
resources :questions
|
||||
end
|
||||
|
||||
|
||||
68
spec/features/admin/poll/officer_assignments_spec.rb
Normal file
68
spec/features/admin/poll/officer_assignments_spec.rb
Normal file
@@ -0,0 +1,68 @@
|
||||
require 'rails_helper'
|
||||
|
||||
feature 'Admin officer assignments in poll' do
|
||||
|
||||
background do
|
||||
admin = create(:administrator)
|
||||
login_as(admin.user)
|
||||
end
|
||||
|
||||
scenario 'Assign officer to poll', :js do
|
||||
booth_assignment = create(:poll_booth_assignment)
|
||||
officer = create(:poll_officer)
|
||||
|
||||
visit admin_poll_path(booth_assignment.poll)
|
||||
within('#poll-resources') do
|
||||
click_link 'Officers (0)'
|
||||
end
|
||||
|
||||
expect(page).to have_content 'There are no officers assigned to this poll'
|
||||
|
||||
fill_in 'search-officers', with: officer.name
|
||||
click_button 'Search'
|
||||
|
||||
within('#search-officers-results') do
|
||||
click_link 'Add shifts as officer'
|
||||
end
|
||||
|
||||
expect(page).to have_content 'This user has no officing shifts in this poll'
|
||||
expect(page).to have_content officer.name
|
||||
expect(page).to have_content booth_assignment.poll.name
|
||||
|
||||
within('#officer_assignment_form') do
|
||||
select I18n.l(booth_assignment.poll.ends_at.to_date), from: 'date'
|
||||
select "#{booth_assignment.booth.name} (#{booth_assignment.booth.location})", from: 'booth_id'
|
||||
click_button 'Add shift'
|
||||
end
|
||||
|
||||
expect(page).to have_content 'Officing shift added'
|
||||
expect(page).to_not have_content 'This user has no officing shifts in this poll'
|
||||
|
||||
visit admin_poll_path(booth_assignment.poll)
|
||||
within('#poll-resources') do
|
||||
click_link 'Officers (1)'
|
||||
end
|
||||
|
||||
expect(page).to_not have_content 'There are no officers in this poll'
|
||||
expect(page).to have_content officer.name
|
||||
expect(page).to have_content officer.email
|
||||
end
|
||||
|
||||
scenario 'remove booth from poll' do
|
||||
officer_assignment = create(:poll_officer_assignment)
|
||||
poll = officer_assignment.booth_assignment.poll
|
||||
booth = officer_assignment.booth_assignment.booth
|
||||
officer = officer_assignment.officer
|
||||
|
||||
visit admin_officer_assignments_path(poll: poll, officer: officer)
|
||||
|
||||
expect(page).to_not have_content 'This user has no officing shifts in this poll'
|
||||
within("#poll_officer_assignment_#{officer_assignment.id}") do
|
||||
expect(page).to have_content booth.name
|
||||
click_link 'Remove'
|
||||
end
|
||||
|
||||
expect(page).to have_content 'Officing shift removed'
|
||||
expect(page).to have_content 'This user has no officing shifts in this poll'
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user