diff --git a/app/controllers/admin/poll/officer_assignments_controller.rb b/app/controllers/admin/poll/officer_assignments_controller.rb
new file mode 100644
index 000000000..e34e1a142
--- /dev/null
+++ b/app/controllers/admin/poll/officer_assignments_controller.rb
@@ -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
\ No newline at end of file
diff --git a/app/views/admin/poll/officer_assignments/index.html.erb b/app/views/admin/poll/officer_assignments/index.html.erb
new file mode 100644
index 000000000..55e879858
--- /dev/null
+++ b/app/views/admin/poll/officer_assignments/index.html.erb
@@ -0,0 +1,64 @@
+<%= link_to @poll.name, admin_poll_path(@poll, anchor: 'tab-officers') %>
+
<%= @officer.name %> - <%= @officer.email %>
+
+<%= form_tag(admin_officer_assignments_path, {id: "officer_assignment_form"}) do %>
+
+ <%= t("admin.poll_officer_assignments.index.new_assignment") %>
+
+ <%= t("admin.poll_officer_assignments.index.date") %>
+ <%= select_tag :date,
+ poll_dates_select_options(@poll),
+ { prompt: t("admin.poll_officer_assignments.index.select_date"),
+ label: false } %>
+
+
+
+ <%= t("admin.poll_officer_assignments.index.booth") %>
+ <%= select_tag :booth_id,
+ poll_booths_select_options(@poll),
+ { prompt: t("admin.poll_officer_assignments.index.select_booth"),
+ label: false } %>
+
+
+
+ <%= 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" %>
+
+
+<% end %>
+
+
+<% if @officer_assignments.empty? %>
+
+ <%= t("admin.poll_officer_assignments.index.no_assignments") %>
+
+<% else %>
+ <%= t("admin.poll_officer_assignments.index.assignments") %>
+
+
+
+ <%= t("admin.poll_officer_assignments.index.date") %>
+ <%= t("admin.poll_officer_assignments.index.booth") %>
+
+
+
+ <% @officer_assignments.each do |officer_assignment| %>
+
+ <%= l officer_assignment.date.to_date %>
+ <%= booth_name_with_location(officer_assignment.booth_assignment.booth) %>
+
+ <%= link_to t("admin.poll_officer_assignments.index.remove_assignment"),
+ admin_officer_assignment_path(officer_assignment),
+ method: :delete,
+ class: "button hollow alert" %>
+
+
+ <% end %>
+
+
+<% end %>
+
+
+
diff --git a/app/views/admin/poll/polls/_officers.html.erb b/app/views/admin/poll/polls/_officers.html.erb
index 106bff526..e95d90d24 100644
--- a/app/views/admin/poll/polls/_officers.html.erb
+++ b/app/views/admin/poll/polls/_officers.html.erb
@@ -15,7 +15,7 @@
- <%= officer.name %>
+ <%= link_to officer.name, admin_officer_assignments_path(officer: officer, poll: @poll) %>
@@ -25,66 +25,4 @@
<% end %>
-<% end %>
-
-
-Clemente padilla Otero
-user2@consul.dev
-
-
-
- Nuevo turno
-
- Fecha
-
- Seleccionar día
-
-
-
-
- Urna
-
- Seleccionar urna
-
-
-
-
-
-
-
-
-Añadir nuevo turno
-
-Turnos asignados
-
-
-
- Fecha
- Urna
-
-
-
-
- 13/02/2016
- Urna Moncloa
-
- <%= link_to "Eliminar turno", "#", class: "button hollow alert" %>
-
-
-
- 14/02/2016
- Urna Moncloa
-
- <%= link_to "Eliminar turno", "#", class: "button hollow alert" %>
-
-
-
- 15/02/2016
- Urna Chamartín
-
- <%= link_to "Eliminar turno", "#", class: "button hollow alert" %>
-
-
-
-
-
+<% end %>
\ No newline at end of file
diff --git a/app/views/admin/poll/polls/_search_officers_results.html.erb b/app/views/admin/poll/polls/_search_officers_results.html.erb
index 71fed6589..6225ad9c5 100644
--- a/app/views/admin/poll/polls/_search_officers_results.html.erb
+++ b/app/views/admin/poll/polls/_search_officers_results.html.erb
@@ -14,7 +14,15 @@
<%= user.email %>
-
+ <% 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 %>
<% end %>
diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml
index a3dddb17f..1210affa7 100755
--- a/config/locales/admin.en.yml
+++ b/config/locales/admin.en.yml
@@ -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"
diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml
index 03ceb208e..1f2ea2257 100644
--- a/config/locales/admin.es.yml
+++ b/config/locales/admin.es.yml
@@ -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"
diff --git a/config/routes.rb b/config/routes.rb
index 63b0e9f6e..96e2db196 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -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
diff --git a/spec/features/admin/poll/officer_assignments_spec.rb b/spec/features/admin/poll/officer_assignments_spec.rb
new file mode 100644
index 000000000..3cbf9fb13
--- /dev/null
+++ b/spec/features/admin/poll/officer_assignments_spec.rb
@@ -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
\ No newline at end of file