From 4527236284c7a847779f6646bab0b1d955980684 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Thu, 7 Sep 2017 22:38:42 +0200 Subject: [PATCH 1/4] adds searching for officers when creating shifts --- .../admin/poll/shifts_controller.rb | 19 ++++++--- .../poll/officer_assignments/index.html.erb | 6 --- app/views/admin/poll/shifts/_form.html.erb | 29 ++++++++++++++ .../poll/shifts/_search_officers.html.erb | 19 +++++++++ .../shifts/_search_officers_results.html.erb | 36 +++++++++++++++++ app/views/admin/poll/shifts/_shifts.html.erb | 6 +-- app/views/admin/poll/shifts/new.html.erb | 40 ++++--------------- .../admin/poll/shifts/search_officers.js.erb | 1 + config/routes.rb | 4 +- 9 files changed, 111 insertions(+), 49 deletions(-) create mode 100644 app/views/admin/poll/shifts/_form.html.erb create mode 100644 app/views/admin/poll/shifts/_search_officers.html.erb create mode 100644 app/views/admin/poll/shifts/_search_officers_results.html.erb create mode 100644 app/views/admin/poll/shifts/search_officers.js.erb diff --git a/app/controllers/admin/poll/shifts_controller.rb b/app/controllers/admin/poll/shifts_controller.rb index 8a808a7a9..168284474 100644 --- a/app/controllers/admin/poll/shifts_controller.rb +++ b/app/controllers/admin/poll/shifts_controller.rb @@ -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 diff --git a/app/views/admin/poll/officer_assignments/index.html.erb b/app/views/admin/poll/officer_assignments/index.html.erb index f6e75c602..6ffe3f04c 100644 --- a/app/views/admin/poll/officer_assignments/index.html.erb +++ b/app/views/admin/poll/officer_assignments/index.html.erb @@ -15,7 +15,6 @@ <%= t("admin.poll_officer_assignments.index.table_name") %> <%= t("admin.poll_officer_assignments.index.table_email") %> - <%= t("admin.actions.actions") %> <% @officers.each do |officer| %> @@ -28,11 +27,6 @@ <%= officer.email %> - - <%= 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" %> - <% end %> diff --git a/app/views/admin/poll/shifts/_form.html.erb b/app/views/admin/poll/shifts/_form.html.erb new file mode 100644 index 000000000..4311d00fe --- /dev/null +++ b/app/views/admin/poll/shifts/_form.html.erb @@ -0,0 +1,29 @@ +<%= form_for @shift, as: :shift, url: admin_booth_shifts_path do |f| %> + <%= render "shared/errors", resource: @shift %> + +
+ + <%= t("admin.poll_shifts.new.new_shift") %> + + +
+ Officer: <%= @officer.name %> + <%= f.hidden_field :officer_id, value: @officer.id %> +
+ +
+ + <%= f.select :date, + shift_dates_select_options(@polls), + prompt: t("admin.poll_shifts.new.select_date"), + label: false %> +
+ + <%= f.hidden_field :booth_id, value: @booth.id %> + +
+ <%= f.submit t("admin.poll_shifts.new.add_shift"), + class: "button expanded hollow margin-top" %> +
+
+<% end %> \ No newline at end of file diff --git a/app/views/admin/poll/shifts/_search_officers.html.erb b/app/views/admin/poll/shifts/_search_officers.html.erb new file mode 100644 index 000000000..793ac0311 --- /dev/null +++ b/app/views/admin/poll/shifts/_search_officers.html.erb @@ -0,0 +1,19 @@ +
+
+ <%= form_tag search_officers_admin_booth_shifts_path, + method: :get, remote: true do |f| %> +
+ <%= text_field_tag :search, + @search, + placeholder: t("admin.poll_shifts.new.search_officer_placeholder"), + id: "search-officers" %> +
+ <%= submit_tag t("admin.poll_shifts.new.search_officer_button"), + class: "button" %> +
+
+ <% end %> +
+
+ +
diff --git a/app/views/admin/poll/shifts/_search_officers_results.html.erb b/app/views/admin/poll/shifts/_search_officers_results.html.erb new file mode 100644 index 000000000..edb96eaf4 --- /dev/null +++ b/app/views/admin/poll/shifts/_search_officers_results.html.erb @@ -0,0 +1,36 @@ +<% if @officers.blank? %> +
+ <%= t('admin.shared.no_search_results') %> +
+<% else %> +

<%= t('admin.shared.search_results') %>

+ + + + + + + + + + + <% @officers.each do |user| %> + + + + + + <% end %> + +
<%= t("admin.poll_shifts.new.table_name") %><%= t("admin.poll_shifts.new.table_email") %> + <%= t("admin.poll_shifts.new.table_shift") %> +
+ <%= user.name %> + + <%= user.email %> + + <%= link_to t("admin.poll_shifts.new.edit_shifts"), + new_admin_booth_shift_path(officer_id: user.poll_officer.id), + class: "button hollow alert" %> +
+<% end %> \ No newline at end of file diff --git a/app/views/admin/poll/shifts/_shifts.html.erb b/app/views/admin/poll/shifts/_shifts.html.erb index 800c6944b..4c10f9ab5 100644 --- a/app/views/admin/poll/shifts/_shifts.html.erb +++ b/app/views/admin/poll/shifts/_shifts.html.erb @@ -1,10 +1,10 @@ -

<%= t("admin.poll_shifts.new.assignments") %>

+

<%= t("admin.poll_shifts.new.shifts") %>

- + @@ -13,7 +13,7 @@ - - + <% @booths.each do |booth| %> diff --git a/app/views/admin/poll/officer_assignments/index.html.erb b/app/views/admin/poll/officer_assignments/index.html.erb index 6ffe3f04c..f27f6b2ae 100644 --- a/app/views/admin/poll/officer_assignments/index.html.erb +++ b/app/views/admin/poll/officer_assignments/index.html.erb @@ -34,4 +34,4 @@ <%= paginate @officers %> <% end %> - \ No newline at end of file + diff --git a/app/views/admin/poll/shifts/_form.html.erb b/app/views/admin/poll/shifts/_form.html.erb index 4311d00fe..f0461855f 100644 --- a/app/views/admin/poll/shifts/_form.html.erb +++ b/app/views/admin/poll/shifts/_form.html.erb @@ -5,12 +5,13 @@ <%= t("admin.poll_shifts.new.new_shift") %> - -
- Officer: <%= @officer.name %> + +
+ <%= t("admin.poll_shifts.new.officer") %> +
<%= @officer.name %> <%= f.hidden_field :officer_id, value: @officer.id %>
- +
<%= f.select :date, @@ -23,7 +24,7 @@
<%= f.submit t("admin.poll_shifts.new.add_shift"), - class: "button expanded hollow margin-top" %> + class: "button expanded margin-top" %>
-<% end %> \ No newline at end of file +<% end %> diff --git a/app/views/admin/poll/shifts/_search_officers.html.erb b/app/views/admin/poll/shifts/_search_officers.html.erb index 793ac0311..e63d09d41 100644 --- a/app/views/admin/poll/shifts/_search_officers.html.erb +++ b/app/views/admin/poll/shifts/_search_officers.html.erb @@ -1,14 +1,14 @@
- <%= form_tag search_officers_admin_booth_shifts_path, + <%= form_tag search_officers_admin_booth_shifts_path, method: :get, remote: true do |f| %>
<%= text_field_tag :search, @search, - placeholder: t("admin.poll_shifts.new.search_officer_placeholder"), + placeholder: t("admin.poll_shifts.new.search_officer_placeholder"), id: "search-officers" %>
- <%= submit_tag t("admin.poll_shifts.new.search_officer_button"), + <%= submit_tag t("admin.poll_shifts.new.search_officer_button"), class: "button" %>
diff --git a/app/views/admin/poll/shifts/_search_officers_results.html.erb b/app/views/admin/poll/shifts/_search_officers_results.html.erb index edb96eaf4..70525ba6d 100644 --- a/app/views/admin/poll/shifts/_search_officers_results.html.erb +++ b/app/views/admin/poll/shifts/_search_officers_results.html.erb @@ -10,7 +10,7 @@
- @@ -24,13 +24,13 @@ - <% end %>
<%= t("admin.poll_shifts.new.date") %> <%= t("admin.poll_shifts.new.officer") %><%= t("admin.poll_shifts.new.assignment") %><%= t("admin.poll_shifts.new.shift") %>
<%= l(shift.date.to_date, format: :long) %> <%= shift.officer.name %> - <%= 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" %> diff --git a/app/views/admin/poll/shifts/new.html.erb b/app/views/admin/poll/shifts/new.html.erb index c997dc35f..7a01aeaf6 100644 --- a/app/views/admin/poll/shifts/new.html.erb +++ b/app/views/admin/poll/shifts/new.html.erb @@ -2,43 +2,17 @@

<%= @booth.name %>

-<%= form_for @shift, as: :shift, url: admin_booth_shifts_path do |f| %> - <%= render "shared/errors", resource: @shift %> - -
- - <%= t("admin.poll_shifts.new.new_assignment") %> - - -
- - <%= f.select :date, - shift_dates_select_options(@polls), - prompt: t("admin.poll_shifts.new.select_date"), - label: false %> -
- -
- - <%= f.select :officer_id, - officer_select_options(@officers), - prompt: t("admin.poll_shifts.new.select_officer"), - label: false %> -
- - <%= f.hidden_field :booth_id, value: @booth.id %> - -
- <%= f.submit t("admin.poll_shifts.new.add_assignment"), - class: "button expanded hollow margin-top" %> -
-
-<% end %> +<% if @officer.blank? %> + <%= t("admin.poll_shifts.new.search_officer_text") %> + <%= render "search_officers" %> +<% else %> + <%= render "form" %> +<% end %>
<% if @shifts.empty? %>
- <%= t("admin.poll_shifts.new.no_assignments") %> + <%= t("admin.poll_shifts.new.no_shifts") %>
<% else %> <%= render "shifts" %> diff --git a/app/views/admin/poll/shifts/search_officers.js.erb b/app/views/admin/poll/shifts/search_officers.js.erb new file mode 100644 index 000000000..ba621d8f7 --- /dev/null +++ b/app/views/admin/poll/shifts/search_officers.js.erb @@ -0,0 +1 @@ +$("#search-officers-results").html("<%= j render 'search_officers_results' %>"); \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index eaf6b98f4..67f816e32 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -287,7 +287,9 @@ Rails.application.routes.draw do end resources :booths do - resources :shifts + resources :shifts do + get :search_officers, on: :collection + end end resources :questions From d3890d1432a7c0966e83f64b37f681bfd1d111f1 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Thu, 7 Sep 2017 22:38:54 +0200 Subject: [PATCH 2/4] updates translations --- config/locales/en/admin.yml | 20 +++++++++++++------- config/locales/en/general.yml | 1 + config/locales/es/admin.yml | 20 +++++++++++++------- config/locales/es/general.yml | 1 + 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index c91719104..3c82199a5 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -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: Please 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" diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index 93075d3dc..321887388 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -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 diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 58fd31aa1..f657ae54d 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -498,16 +498,22 @@ 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: Por favor 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" destroy: "Eliminado turno de presidente de mesa" diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index 18597d347..c4fd431b9 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -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 From b1abb9dbb3c588b2c6146c01e990ec0a8b2a796b Mon Sep 17 00:00:00 2001 From: rgarcia Date: Thu, 7 Sep 2017 22:39:03 +0200 Subject: [PATCH 3/4] adds specs --- spec/features/admin/poll/shifts_spec.rb | 30 +++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/spec/features/admin/poll/shifts_spec.rb b/spec/features/admin/poll/shifts_spec.rb index fd9b9f4ca..b55e40c7c 100644 --- a/spec/features/admin/poll/shifts_spec.rb +++ b/spec/features/admin/poll/shifts_spec.rb @@ -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) From 3b4e80a34dc0ad9b641d909ffdca893d813858ea Mon Sep 17 00:00:00 2001 From: decabeza Date: Fri, 8 Sep 2017 16:50:53 +0200 Subject: [PATCH 4/4] improves layout and tables for polls shifts --- app/views/admin/poll/booths/index.html.erb | 3 +-- .../admin/poll/officer_assignments/index.html.erb | 2 +- app/views/admin/poll/shifts/_form.html.erb | 13 +++++++------ .../admin/poll/shifts/_search_officers.html.erb | 6 +++--- .../poll/shifts/_search_officers_results.html.erb | 8 ++++---- app/views/admin/poll/shifts/new.html.erb | 10 ++++++---- config/locales/en/admin.yml | 2 +- config/locales/es/admin.yml | 4 ++-- 8 files changed, 25 insertions(+), 23 deletions(-) diff --git a/app/views/admin/poll/booths/index.html.erb b/app/views/admin/poll/booths/index.html.erb index 0dbb62cdf..05044dd0e 100644 --- a/app/views/admin/poll/booths/index.html.erb +++ b/app/views/admin/poll/booths/index.html.erb @@ -15,8 +15,7 @@
<%= t("admin.booths.index.name") %> <%= t("admin.booths.index.location") %>  <%= t("admin.actions.actions") %>
<%= t("admin.poll_shifts.new.table_name") %> <%= t("admin.poll_shifts.new.table_email") %> + <%= t("admin.poll_shifts.new.table_shift") %>
<%= user.email %> + <%= link_to t("admin.poll_shifts.new.edit_shifts"), new_admin_booth_shift_path(officer_id: user.poll_officer.id), - class: "button hollow alert" %> + class: "button hollow" %>
-<% end %> \ No newline at end of file +<% end %> diff --git a/app/views/admin/poll/shifts/new.html.erb b/app/views/admin/poll/shifts/new.html.erb index 7a01aeaf6..b975c40a9 100644 --- a/app/views/admin/poll/shifts/new.html.erb +++ b/app/views/admin/poll/shifts/new.html.erb @@ -3,18 +3,20 @@

<%= @booth.name %>

<% if @officer.blank? %> - <%= t("admin.poll_shifts.new.search_officer_text") %> +

+ <%= t("admin.poll_shifts.new.search_officer_text") %> +

<%= render "search_officers" %> <% else %> <%= render "form" %> -<% end %> +<% end %>
<% if @shifts.empty? %> -
+
<%= t("admin.poll_shifts.new.no_shifts") %>
<% else %> <%= render "shifts" %> <% end %> -
\ No newline at end of file +
diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index 3c82199a5..3e5cb9d31 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -509,7 +509,7 @@ en: remove_shift: "Remove" search_officer_button: Search search_officer_placeholder: Search officer - search_officer_text: Please search for an officer to assign a new shift + search_officer_text: Search for an officer to assign a new shift select_date: "Select day" table_shift: "Shift" table_email: "Email" diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index f657ae54d..1b98adaf2 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -509,13 +509,13 @@ es: remove_shift: "Eliminar turno" search_officer_button: Buscar search_officer_placeholder: Buscar presidentes de mesa - search_officer_text: Por favor busca al presidente de mesa para asignar un turno + search_officer_text: Busca al presidente de mesa para asignar un turno select_date: "Seleccionar día" 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: