Merge pull request #1777 from consul/poll-shifts

Poll shifts
This commit is contained in:
Raimond Garcia
2017-09-05 15:14:46 +02:00
committed by GitHub
22 changed files with 424 additions and 184 deletions

View File

@@ -32,35 +32,6 @@ class Admin::Poll::OfficerAssignmentsController < Admin::BaseController
end
end
def create
@officer_assignment = ::Poll::OfficerAssignment.new(booth_assignment: @booth_assignment,
officer_id: create_params[:officer_id],
date: create_params[:date])
@officer_assignment.final = true if @officer_assignment.date > @booth_assignment.poll.ends_at.to_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_params = { poll_id: create_params[:poll_id], officer_id: create_params[:officer_id] }
redirect_to by_officer_admin_poll_officer_assignments_path(redirect_params), 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_params = { poll_id: @officer_assignment.poll_id, officer_id: @officer_assignment.officer_id }
redirect_to by_officer_admin_poll_officer_assignments_path(redirect_params), notice: notice
end
private
def officer_assignment_params

View File

@@ -0,0 +1,53 @@
class Admin::Poll::ShiftsController < Admin::BaseController
before_action :load_booth
before_action :load_polls
def new
load_officers
load_shifts
@shift = ::Poll::Shift.new
end
def create
@shift = ::Poll::Shift.new(shift_params)
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
end
def destroy
@shift = Poll::Shift.find(params[:id])
@shift.destroy
notice = t("admin.poll_shifts.flash.destroy")
redirect_to new_admin_booth_shift_path(@booth), notice: notice
end
private
def load_booth
@booth = ::Poll::Booth.find(params[:booth_id])
end
def load_polls
@polls = ::Poll.current_or_incoming
end
def load_officers
@officers = ::Poll::Officer.all
end
def load_shifts
@shifts = @booth.shifts
end
def shift_params
params.require(:shift).permit(:booth_id, :officer_id, :date)
end
end

View File

@@ -0,0 +1,23 @@
module ShiftsHelper
def shift_dates_select_options(polls)
options = []
(start_date(polls)..end_date(polls)).each do |date|
options << [l(date, format: :long), l(date)]
end
options_for_select(options, params[:date])
end
def start_date(polls)
polls.map(&:starts_at).min.to_date
end
def end_date(polls)
polls.map(&:ends_at).max.to_date
end
def officer_select_options(officers)
officers.collect { |officer| [officer.name, officer.id] }
end
end

View File

@@ -35,6 +35,10 @@ class Poll < ActiveRecord::Base
ends_at < timestamp
end
def self.current_or_incoming
current + incoming
end
def answerable_by?(user)
user.present? &&
user.level_two_or_three_verified? &&

View File

@@ -2,6 +2,7 @@ class Poll
class Booth < ActiveRecord::Base
has_many :booth_assignments, class_name: "Poll::BoothAssignment"
has_many :polls, through: :booth_assignments
has_many :shifts
validates :name, presence: true, uniqueness: true

22
app/models/poll/shift.rb Normal file
View File

@@ -0,0 +1,22 @@
class Poll
class Shift < ActiveRecord::Base
belongs_to :booth
belongs_to :officer
validates :booth_id, presence: true
validates :officer_id, presence: true
validates :date, presence: true
validates :date, uniqueness: { scope: [:officer_id, :booth_id] }
after_create :create_officer_assignments
def create_officer_assignments
booth.booth_assignments.each do |booth_assignment|
attrs = { officer_id: officer_id,
date: date,
booth_assignment_id: booth_assignment.id }
Poll::OfficerAssignment.create!(attrs)
end
end
end
end

View File

@@ -73,9 +73,13 @@
<%= link_to t('admin.menu.poll_officers'), admin_officers_path %>
</li>
<li <%= "class=active" if controller_name == "booths" %>>
<li <%# "class=active" if controller_name == "booths" %>>
<%= link_to t('admin.menu.poll_booths'), admin_booths_path %>
</li>
<li <%= "class=active" if controller_name == "booths" %>>
<%= link_to t('admin.menu.poll_shifts'), admin_booths_path %>
</li>
</ul>
</li>
<% end %>

View File

@@ -6,6 +6,9 @@
<%= booth.location %>
</td>
<td class="text-right">
<%= link_to t("admin.booths.booth.shifts"),
new_admin_booth_shift_path(booth),
class: "button hollow" %>
<%= link_to t("admin.actions.edit"),
edit_admin_booth_path(booth),
class: "button hollow" %>

View File

@@ -16,6 +16,7 @@
<th><%= t("admin.booths.index.name") %></th>
<th><%= t("admin.booths.index.location") %></th>
<th>&nbsp;</th>
<th>&nbsp;</th>
</thead>
<tbody>
<% @booths.each do |booth| %>

View File

@@ -5,35 +5,6 @@
<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") %>
@@ -45,7 +16,6 @@
<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>
@@ -53,12 +23,6 @@
<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>
@@ -93,6 +57,3 @@
</tbody>
</table>
<% end %>

View File

@@ -0,0 +1,24 @@
<h3><%= t("admin.poll_shifts.new.assignments") %></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>
</tr>
</thead>
<tbody>
<% @shifts.each do |shift| %>
<tr id="shift_<%= shift.id %>" class="shift">
<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"),
admin_booth_shift_path(@booth, shift),
method: :delete,
class: "button hollow alert" %>
</td>
</tr>
<% end %>
</tbody>
</table>

View File

@@ -0,0 +1,46 @@
<%= back_link_to admin_booths_path %>
<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>
<% end %>
<div id="shifts">
<% if @shifts.empty? %>
<div class="callout primary margin-top">
<%= t("admin.poll_shifts.new.no_assignments") %>
</div>
<% else %>
<%= render "shifts" %>
<% end %>
</div>