Skip to content

Commit

Permalink
feat: Use interactive table on bus list passengers
Browse files Browse the repository at this point in the history
  • Loading branch information
sman591 committed May 29, 2019
1 parent 7364223 commit 90f3b7b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 54 deletions.
13 changes: 9 additions & 4 deletions app/assets/javascripts/manage/lib/datatables.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ $.fn.autoDatatable = function() {

var config = convertDataAttrsToConfig(this);
if (config.order) {
var parts = config.order.split(' ');
var index = parseInt(parts[0], 10);
var direction = parts.length > 1 ? parts[1] : 'asc';
config.order = [index, direction];
var sequence = config.order.split(',').map(function(piece) {
return piece.trim();
});
config.order = sequence.map(function(piece) {
var parts = piece.split(' ');
var index = parseInt(parts[0], 10);
var direction = parts.length > 1 ? parts[1] : 'asc';
return [index, direction];
});
} else {
config.order = [1, 'asc'];
}
Expand Down
27 changes: 25 additions & 2 deletions app/datatables/questionnaire_datatable.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class QuestionnaireDatatable < ApplicationDatatable
def_delegators :@view, :link_to, :manage_questionnaire_path, :manage_school_path, :current_user, :acc_status_class, :display_datetime, :bold
def_delegators :@view, :link_to, :manage_questionnaire_path, :manage_school_path, :toggle_bus_captain_manage_bus_list_path, :current_user, :acc_status_class, :display_datetime, :bold

def view_columns
@view_columns ||= {
Expand All @@ -14,6 +14,7 @@ def view_columns
acc_status: { source: "Questionnaire.acc_status", searchable: true },
checked_in: { source: "Questionnaire.checked_in_at", searchable: false },
boarded_bus: { source: "Questionnaire.boarded_bus_at", searchable: false },
bus_captain: { source: "Questionnaire.is_bus_captain", searchable: false },
school: { source: "School.name" },
created_at: { source: "Questionnaire.created_at", searchable: false },
dietary_restrictions: { source: "Questionnaire.dietary_restrictions", searchable: true },
Expand All @@ -32,6 +33,17 @@ def note(record)
output.html_safe
end

def bus_captain(record)
return "No" unless record.bus_list_id?
return record.is_bus_captain? ? '<span class="badge badge-success">Yes</span>' : "No" unless current_user.admin?

if record.is_bus_captain?
link_to("Remove", toggle_bus_captain_manage_bus_list_path(record.bus_list, questionnaire_id: record.id, bus_captain: "0"), method: "post", class: "text-danger")
else
link_to("Promote", toggle_bus_captain_manage_bus_list_path(record.bus_list, questionnaire_id: record.id, bus_captain: "1"), method: "post", data: { confirm: "Are you sure you want to make #{record.full_name} a bus captain? They will receive a confirmation email." })
end
end

def data
records.map do |record|
{
Expand All @@ -48,6 +60,7 @@ def data
acc_status: "<span class=\"text-#{acc_status_class(record.acc_status)}\">#{record.acc_status.titleize}</span>".html_safe,
checked_in: record.checked_in? ? '<span class="badge badge-success">Yes</span>'.html_safe : "No",
boarded_bus: record.boarded_bus? ? '<span class="badge badge-success">Yes</span>'.html_safe : "No",
bus_captain: bus_captain(record),
school: link_to(record.school.name, manage_school_path(record.school)),
created_at: record.created_at.present? ? display_datetime(record.created_at) : "",
dietary_restrictions: record.dietary_restrictions,
Expand All @@ -57,6 +70,16 @@ def data
end

def get_raw_records
Questionnaire.includes(:user, :school).references(:user, :school)
records = Questionnaire.includes(:user, :school).references(:user, :school)
if @params[:school_id].present?
records = records.where(school_id: @params[:school_id])
end
if @params[:bus_list_id].present?
records = records.where(bus_list_id: @params[:bus_list_id])
end
if @params[:acc_status].present?
records = records.where(acc_status: @params[:acc_status])
end
records
end
end
28 changes: 17 additions & 11 deletions app/views/manage/application/_questionnaire_datatable.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
columns ||= %w(link note first_name last_name email acc_status checked_in school created_at)
visible = -> (column, columns) { columns.include?(column) ? 'true' : 'false' }

order ||= "15 desc"
bulk_actions = true unless defined?(bulk_actions)

# Use default page length if not defined
page_length ||= nil

%div
%table.datatable.table.table-striped.table-hover{ data: { "auto-datatable": true, source: data_source, "table-order": "14 desc", 'page-length': page_length } }
%table.datatable.table.table-striped.table-hover{ data: { "auto-datatable": true, source: data_source, "table-order": order, 'page-length': page_length } }
%thead
%tr
%th{'data-table': { orderable: 'false', data: 'bulk' }}
%th{'data-table': { orderable: 'false', data: 'bulk', visible: bulk_actions ? 'true' : 'false' }}
- if current_user.admin?
%input{ type: "checkbox", name: "select_allc", value: "1", data: { bulk_row_select: "" } }
%th{'data-table': { orderable: 'false', data: 'link', visible: visible.call('link', columns) }}
Expand All @@ -30,6 +33,7 @@
%th{'data-table': { orderable: 'true', data: 'acc_status', visible: visible.call('acc_status', columns) }} Status
%th{'data-table': { orderable: 'true', data: 'checked_in', visible: visible.call('checked_in', columns) }} Checked in
%th{'data-table': { orderable: 'true', data: 'boarded_bus', visible: visible.call('boarded_bus', columns) }} Boarded bus
%th{'data-table': { orderable: 'true', data: 'bus_captain', visible: visible.call('bus_captain', columns) }} Bus captain
%th{'data-table': { orderable: 'true', data: 'school', visible: visible.call('school', columns) }} School
%th{'data-table': { orderable: 'true', data: 'created_at', visible: visible.call('created_at', columns) }} Applied on
%th{'data-table': { orderable: 'true', data: 'dietary_restrictions', visible: visible.call('dietary_restrictions', columns) }} Dietary restrictions
Expand Down Expand Up @@ -60,6 +64,7 @@
%option{value: key}= value
%th
%th
%th
%th
%input.form-control.form-control-sm{autocomplete: 'off', placeholder: 'School'}
%th
Expand All @@ -69,12 +74,13 @@
%input.form-control.form-control-sm{autocomplete: 'off', placeholder: 'Special needs'}
.row
.col-sm-7.col-lg-6
%br
.card
.card-body
%h5.card-title Bulk Action
= bs_vertical_simple_form Questionnaire.new, url: bulk_apply_manage_questionnaires_path, html: { data: { bulk_row_edit: true } } do |f|
= f.input :acc_status, as: :select, collection: Questionnaire::POSSIBLE_ACC_STATUS.invert, include_blank: false, label: "Acceptance Status:", input_html: { data: { bulk_row_edit: true } }, hint: "Updating this status may trigger an automatic email to each applicant - see below for details."
= f.button :submit, value: "Update Status", data: { bulk_row_edit: true }, class: 'btn-primary'
- if bulk_actions
.row
.col-sm-7.col-lg-6
%br
.card
.card-body
%h5.card-title Bulk Action
= bs_vertical_simple_form Questionnaire.new, url: bulk_apply_manage_questionnaires_path, html: { data: { bulk_row_edit: true } } do |f|
= f.input :acc_status, as: :select, collection: Questionnaire::POSSIBLE_ACC_STATUS.invert, include_blank: false, label: "Acceptance Status:", input_html: { data: { bulk_row_edit: true } }, hint: "Updating this status may trigger an automatic email to each applicant - see below for details."
= f.button :submit, value: "Update Status", data: { bulk_row_edit: true }, class: 'btn-primary'
38 changes: 1 addition & 37 deletions app/views/manage/bus_lists/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -47,43 +47,7 @@
Passengers
%small.text-muted (#{@bus_list.passengers.count} total, #{@bus_list.boarded_passengers.count} boarded, #{@bus_list.checked_in_passengers.count} checked in)

%table.table
%thead
%tr
%th
%th First Name
%th Last Name
%th Email
%th Phone Number
%th School
%th Boarded bus?
%th Checked in?
%th Bus Captain
%tbody
- was_bus_captain = false
- @bus_list.passengers.reorder('is_bus_captain DESC, last_name ASC').each do |p|
- # Visual separator between captains and passengers
- if was_bus_captain && !p.is_bus_captain
%tr
%td{colspan: 9}
- was_bus_captain = p.is_bus_captain
%tr
%td= link_to '<i class="fa fa-search"></i>'.html_safe, manage_questionnaire_path(p)
%td= p.first_name
%td= p.last_name
%td= p.email
%td= phone_link_to p.phone
%td= link_to p.school.name, manage_school_path(p.school)
%td= p.boarded_bus? ? '<span class="text-success">Yes</span>'.html_safe : 'No'
%td= p.checked_in? ? '<span class="text-success">Yes</span>'.html_safe : 'No'
- if current_user.admin?
%td
- if p.is_bus_captain?
= link_to "Remove", toggle_bus_captain_manage_bus_list_path(@bus_list, questionnaire_id: p.id, bus_captain: '0'), method: 'post', class: 'text-danger'
- else
= link_to "Promote", toggle_bus_captain_manage_bus_list_path(@bus_list, questionnaire_id: p.id, bus_captain: '1'), method: 'post', data: { confirm: "Are you sure you want to make #{p.full_name} a bus captain? They will receive a confirmation email." }
- else
%td= p.is_bus_captain? ? "Yes" : "No"
= render "questionnaire_datatable", scope_params: { bus_list_id: @bus_list.id, acc_status: 'rsvp_confirmed' }, columns: %w(link first_name last_name email phone checked_in boarded_bus bus_captain school), order: '13 desc, 15 desc', bulk_actions: false

- if @bus_list.needs_bus_captain
%h4.mt-4.pb-2 Possible Bus Captains
Expand Down

0 comments on commit 90f3b7b

Please sign in to comment.