Merge pull request #1840 from consul/poll-shifts-search-officers

Poll shifts search officers
This commit is contained in:
BertoCQ
2017-09-11 00:07:26 +02:00
committed by GitHub
15 changed files with 173 additions and 73 deletions

View File

@@ -2,20 +2,21 @@ class Admin::Poll::ShiftsController < Admin::BaseController
before_action :load_booth
before_action :load_polls
before_action :load_officer
def new
load_officers
load_shifts
@shift = ::Poll::Shift.new
end
def create
@shift = ::Poll::Shift.new(shift_params)
@officer = @shift.officer
if @shift.save
notice = t("admin.poll_shifts.flash.create")
redirect_to new_admin_booth_shift_path(@shift.booth), notice: notice
else
load_officers
load_shifts
render :new
end
@@ -28,6 +29,10 @@ class Admin::Poll::ShiftsController < Admin::BaseController
redirect_to new_admin_booth_shift_path(@booth), notice: notice
end
def search_officers
@officers = User.search(params[:search]).order(username: :asc)
end
private
def load_booth
@@ -38,14 +43,16 @@ class Admin::Poll::ShiftsController < Admin::BaseController
@polls = ::Poll.current_or_incoming
end
def load_officers
@officers = ::Poll::Officer.all
end
def load_shifts
@shifts = @booth.shifts
end
def load_officer
if params[:officer_id].present?
@officer = ::Poll::Officer.find(params[:officer_id])
end
end
def shift_params
params.require(:shift).permit(:booth_id, :officer_id, :date)
end

View File

@@ -15,8 +15,7 @@
<thead>
<th><%= t("admin.booths.index.name") %></th>
<th><%= t("admin.booths.index.location") %></th>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th class="text-right"><%= t("admin.actions.actions") %></th>
</thead>
<tbody>
<% @booths.each do |booth| %>

View File

@@ -15,7 +15,6 @@
<thead>
<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>
<% @officers.each do |officer| %>
@@ -28,11 +27,6 @@
<td>
<%= officer.email %>
</td>
<td class="text-right">
<%= 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>
@@ -40,4 +34,4 @@
<%= paginate @officers %>
<% end %>
</div>
</div>

View File

@@ -0,0 +1,30 @@
<%= form_for @shift, as: :shift, url: admin_booth_shifts_path do |f| %>
<%= render "shared/errors", resource: @shift %>
<fieldset class="fieldset">
<legend>
<%= t("admin.poll_shifts.new.new_shift") %>
</legend>
<div class="small-12 medium-4 column highlight padding">
<strong><%= t("admin.poll_shifts.new.officer") %></strong>
<br><%= @officer.name %>
<%= f.hidden_field :officer_id, value: @officer.id %>
</div>
<div class="small-12 medium-4 column">
<label><%= t("admin.poll_shifts.new.date") %></label>
<%= f.select :date,
shift_dates_select_options(@polls),
prompt: t("admin.poll_shifts.new.select_date"),
label: false %>
</div>
<%= f.hidden_field :booth_id, value: @booth.id %>
<div class="small-12 medium-4 column">
<%= f.submit t("admin.poll_shifts.new.add_shift"),
class: "button expanded margin-top" %>
</div>
</fieldset>
<% end %>

View File

@@ -0,0 +1,19 @@
<div class="row">
<div class="small-12 medium-6 column">
<%= form_tag search_officers_admin_booth_shifts_path,
method: :get, remote: true do |f| %>
<div class="input-group">
<%= text_field_tag :search,
@search,
placeholder: t("admin.poll_shifts.new.search_officer_placeholder"),
id: "search-officers" %>
<div class="input-group-button">
<%= submit_tag t("admin.poll_shifts.new.search_officer_button"),
class: "button" %>
</div>
</div>
<% end %>
</div>
</div>
<div id="search-officers-results"></div>

View File

@@ -0,0 +1,36 @@
<% if @officers.blank? %>
<div class="callout alert margin-bottom">
<%= t('admin.shared.no_search_results') %>
</div>
<% else %>
<h3><%= t('admin.shared.search_results') %></h3>
<table>
<thead>
<tr>
<th><%= t("admin.poll_shifts.new.table_name") %></th>
<th><%= t("admin.poll_shifts.new.table_email") %></th>
<th class="text-right">
<%= t("admin.poll_shifts.new.table_shift") %>
</th>
</tr>
</thead>
<tbody>
<% @officers.each do |user| %>
<tr>
<td>
<%= user.name %>
</td>
<td>
<%= user.email %>
</td>
<td class="text-right">
<%= link_to t("admin.poll_shifts.new.edit_shifts"),
new_admin_booth_shift_path(officer_id: user.poll_officer.id),
class: "button hollow" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% end %>

View File

@@ -1,10 +1,10 @@
<h3><%= t("admin.poll_shifts.new.assignments") %></h3>
<h3><%= t("admin.poll_shifts.new.shifts") %></h3>
<table class="fixed">
<thead>
<tr>
<th><%= t("admin.poll_shifts.new.date") %></th>
<th><%= t("admin.poll_shifts.new.officer") %></th>
<th class="text-right"><%= t("admin.poll_shifts.new.assignment") %></th>
<th class="text-right"><%= t("admin.poll_shifts.new.shift") %></th>
</tr>
</thead>
<tbody>
@@ -13,7 +13,7 @@
<td><%= l(shift.date.to_date, format: :long) %></td>
<td><%= shift.officer.name %></td>
<td class="text-right">
<%= link_to t("admin.poll_shifts.new.remove_assignment"),
<%= link_to t("admin.poll_shifts.new.remove_shift"),
admin_booth_shift_path(@booth, shift),
method: :delete,
class: "button hollow alert" %>

View File

@@ -2,45 +2,21 @@
<h2><%= @booth.name %></h2>
<%= form_for @shift, as: :shift, url: admin_booth_shifts_path do |f| %>
<%= render "shared/errors", resource: @shift %>
<fieldset class="fieldset">
<legend>
<%= t("admin.poll_shifts.new.new_assignment") %>
</legend>
<div class="small-12 medium-4 column">
<label><%= t("admin.poll_shifts.new.date") %></label>
<%= f.select :date,
shift_dates_select_options(@polls),
prompt: t("admin.poll_shifts.new.select_date"),
label: false %>
</div>
<div class="small-12 medium-4 column">
<label><%= t("admin.poll_shifts.new.officer") %></label>
<%= f.select :officer_id,
officer_select_options(@officers),
prompt: t("admin.poll_shifts.new.select_officer"),
label: false %>
</div>
<%= f.hidden_field :booth_id, value: @booth.id %>
<div class="small-12 medium-4 column">
<%= f.submit t("admin.poll_shifts.new.add_assignment"),
class: "button expanded hollow margin-top" %>
</div>
</fieldset>
<% if @officer.blank? %>
<p>
<%= t("admin.poll_shifts.new.search_officer_text") %>
</p>
<%= render "search_officers" %>
<% else %>
<%= render "form" %>
<% end %>
<div id="shifts">
<% if @shifts.empty? %>
<div class="callout primary margin-top">
<%= t("admin.poll_shifts.new.no_assignments") %>
<div class="callout primary">
<%= t("admin.poll_shifts.new.no_shifts") %>
</div>
<% else %>
<%= render "shifts" %>
<% end %>
</div>
</div>

View File

@@ -0,0 +1 @@
$("#search-officers-results").html("<%= j render 'search_officers_results' %>");

View File

@@ -498,16 +498,22 @@ en:
final_recount: "Final recount (by officer)"
poll_shifts:
new:
new_assignment: "New shift"
add_shift: "Add shift"
shift: "Assignment"
shifts: "Shifts in this booth"
date: "Date"
edit_shifts: Edit shifts
new_shift: "New shift"
no_shifts: "This booth has no shifts"
officer: "Officer"
assignment: "Assignment"
remove_shift: "Remove"
search_officer_button: Search
search_officer_placeholder: Search officer
search_officer_text: Search for an officer to assign a new shift
select_date: "Select day"
select_officer: "Select officer"
add_assignment: "Add shift"
remove_assignment: "Remove"
assignments: "Shifts in this booth"
no_assignments: "This booth has no shifts"
table_shift: "Shift"
table_email: "Email"
table_name: "Name"
flash:
create: "Shift added"
destroy: "Shift removed"

View File

@@ -175,6 +175,7 @@ en:
proposal_notification: "Notification"
spending_proposal: Spending proposal
budget/investment: Investment
poll/shift: Shift
user: Account
verification/sms: phone
signature_sheet: Signature sheet

View File

@@ -498,18 +498,24 @@ es:
final_recount: "Recuento final (presidente de mesa)"
poll_shifts:
new:
new_assignment: "Nuevo turno"
add_shift: "Añadir turno"
shift: "Asignación"
shifts: "Turnos en esta urna"
date: "Fecha"
edit_shifts: Asignar turno
new_shift: "Nuevo turno"
no_shifts: "Esta urna no tiene turnos asignados"
officer: "Presidente de mesa"
assignment: "Asignación"
remove_shift: "Eliminar turno"
search_officer_button: Buscar
search_officer_placeholder: Buscar presidentes de mesa
search_officer_text: Busca al presidente de mesa para asignar un turno
select_date: "Seleccionar día"
select_officer: "Seleccionar presidente de mesa"
add_assignment: "Añadir turno"
remove_assignment: "Eliminar turno"
assignments: "Turnos en esta urna"
no_assignments: "Esta urna no tiene turnos asignados"
table_shift: "Turno"
table_email: "Email"
table_name: "Nombre"
flash:
create: "Añadido turno de presidente de mesa"
create: "Añadido turno de presidente de mesa"
destroy: "Eliminado turno de presidente de mesa"
poll_booth_assignments:
flash:

View File

@@ -175,6 +175,7 @@ es:
proposal_notification: "la notificación"
spending_proposal: la propuesta de gasto
budget/investment: la propuesta de inversión
poll/shift: el turno
user: la cuenta
verification/sms: el teléfono
signature_sheet: la hoja de firmas

View File

@@ -288,8 +288,10 @@ Rails.application.routes.draw do
resources :booths do
get :available, on: :collection
resources :shifts
resources :shifts do
get :search_officers, on: :collection
end
end
resources :questions

View File

@@ -30,7 +30,7 @@ feature 'Admin shifts' do
expect(page).to have_content officer.name
end
scenario "Create" do
scenario "Create", :js do
poll = create(:poll)
booth = create(:poll_booth)
officer = create(:poll_officer)
@@ -41,10 +41,13 @@ feature 'Admin shifts' do
click_link "Manage shifts"
end
select I18n.l(poll.starts_at.to_date, format: :long), from: 'shift_date'
select officer.name, from: 'shift_officer_id'
click_button "Add shift"
fill_in "search", with: officer.email
click_button "Search"
click_link "Edit shifts"
select I18n.l(poll.starts_at.to_date, format: :long), from: 'shift_date'
click_button "Add shift"
expect(page).to have_content "Shift added"
within("#shifts") do
@@ -54,6 +57,25 @@ feature 'Admin shifts' do
end
end
scenario "Erros on create", :js do
poll = create(:poll)
booth = create(:poll_booth)
officer = create(:poll_officer)
visit admin_booths_path
within("#booth_#{booth.id}") do
click_link "Manage shifts"
end
fill_in "search", with: officer.email
click_button "Search"
click_link "Edit shifts"
click_button "Add shift"
expect(page).to have_content "can't be blank"
end
scenario "Destroy" do
poll = create(:poll)
booth = create(:poll_booth)