Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add occurred at to incidents #292

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions app/controllers/incidents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ def create
if @incident.save
@incident.notify_supervisor_of_new_report if @assigning_supervisor
@incident.remove_supervisor if @removing_supervisor
redirect_to incidents_path, notice: 'Incident was successfully created.'
if current_user.driver?
redirect_to edit_incident_path(@incident)
else
redirect_to incidents_path, notice: 'Incident was successfully created.'
end
else render :new, status: :unprocessable_entity
end
end
Expand Down Expand Up @@ -102,11 +106,7 @@ def index
end

def new
if current_user.driver?
@incident = Incident.create driver_incident_report_attributes: { user_id: current_user.id }
redirect_to edit_incident_path(@incident)
else @incident = Incident.new
end
@incident = Incident.new
end

def search
Expand Down Expand Up @@ -166,6 +166,7 @@ def update
def incident_params
data = params.require(:incident).permit!
sup_report_attrs = data[:supervisor_incident_report_attributes]
@incident.driver_incident_report = IncidentReport.create( user_id: current_user.id ) if current_user.driver?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel uncomfortable creating an object in the params. If not for any reason other than the question: What if it doesn't work? There's no error handling here, that's done (as is the "Rails Way") in the create action.
There are a couple standard or at least more future-proof ways of handling the problem of "create a driver incident report also if the current user is a driver".
Since Incident accepts_nested_attributes_for IncidentReport, you could just handle this in the same way the supervisor incident report is, with incident_driver_incident_report_attributes. See the way sup_report_attrs is created and used.
What you want to end up with is data that can be used to create an object, not some data and also an already-created object.

if sup_report_attrs.present?
@assigning_supervisor = sup_report_attrs[:user_id].present? &&
@incident.supervisor_report.nil?
Expand Down
27 changes: 18 additions & 9 deletions app/models/incident.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Incident < ApplicationRecord
# belongs_to :reason_code, optional: { unless: :completed? }
belongs_to :reason_code, optional: true
belongs_to :supplementary_reason_code, optional: true
validates :occurred_at, presence: true
validates :reason_code, :supplementary_reason_code, :root_cause_analysis, :latitude, :longitude,
presence: true, if: :completed?

Expand All @@ -31,17 +32,13 @@ class Incident < ApplicationRecord
validate :supervisor_in_correct_group

accepts_nested_attributes_for :driver_incident_report
validates :occurred_at, presence: true, on: :create, if: :created_by_supervisor
delegate :occurred_at_readable, to: :driver_incident_report
delegate :occurred_at, to: :driver_incident_report
accepts_nested_attributes_for :supervisor_incident_report
accepts_nested_attributes_for :supervisor_report

has_many :staff_reviews, dependent: :destroy

scope :between, (lambda do |start_date, end_date|
joins(:driver_incident_report)
.where incident_reports: { occurred_at: start_date..end_date }
where occurred_at: start_date..end_date
end)

scope :in_divisions, (lambda do |divisions|
Expand All @@ -50,7 +47,7 @@ class Incident < ApplicationRecord
end)

scope :occurred_order, (lambda do
joins(:driver_incident_report).order 'incident_reports.occurred_at'
order :occurred_at
end)

scope :for_driver, ->(user) {
Expand All @@ -72,6 +69,18 @@ class Incident < ApplicationRecord

after_create :send_notifications

def occurred_at_readable
[occurred_date, occurred_time].join ' - '
end

def occurred_date
occurred_at.try :strftime, '%A, %B %e'
end

def occurred_time
occurred_at.try :strftime, '%l:%M %P'
end

def claim_for(user)
self.supervisor_incident_report = create_supervisor_incident_report user: user
self.supervisor_report = create_supervisor_report
Expand All @@ -81,10 +90,10 @@ def claim_for(user)
def csv_row
row = []
report = driver_incident_report
row << report.occurred_at.strftime('%m/%d/%Y') # Date
row << occurred_at.strftime('%m/%d/%Y') # Date
row << report.bus # Bus
row << "#{driver.badge_number} | #{driver.proper_name.upcase}" # Badge # and Operator
row << report.occurred_at.strftime('%H:%M:%S') # Time
row << occurred_at.strftime('%H:%M:%S') # Time
row << report.full_location # Location
row << report.run # Route
row << reason_code.try(:identifier) || "" # Classification 1
Expand All @@ -111,7 +120,7 @@ def export_claims_xml(current_user)
builder = Nokogiri::XML::Builder.new do |doc|
doc.claims_incident_data do
doc.drivers_report do
doc.incident_date report.occurred_at.iso8601
doc.incident_date occurred_at.iso8601
doc.street report.location
doc.city report.town
doc.state report.state
Expand Down
14 changes: 1 addition & 13 deletions app/models/incident_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class IncidentReport < ApplicationRecord

accepts_nested_attributes_for :injured_passengers

validates :occurred_at, :location, :direction, :town, :bus, :description,
validates :location, :direction, :town, :bus, :description,
presence: true, if: :changed?, unless: :new_record?
validates :run, length: { maximum: 5 }
validates :location, length: { maximum: 50 }
Expand Down Expand Up @@ -95,18 +95,6 @@ def needs_reason_not_up_to_curb?
motion_of_bus == 'Stopped' && !bus_up_to_curb?
end

def occurred_at_readable
[occurred_date, occurred_time].join ' - '
end

def occurred_date
occurred_at.try :strftime, '%A, %B %e'
end

def occurred_time
occurred_at.try :strftime, '%l:%M %P'
end

# rubocop:disable Metrics/LineLength
def other_vehicle_driver_full_address
first_line = other_vehicle_driver_address
Expand Down
6 changes: 0 additions & 6 deletions app/views/incident_reports/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
= form.select :user_id,
options_from_collection_for_select(users, :id, :proper_name, report.user_id),
{ include_blank: true }, id: :incident_report_user_id
.field
= form.label :occurred_at
- a11y_datetime_labels('incident_report_occurred_at').each do |label|
= label
= form.datetime_select :occurred_at, id: :incident_report_occurred_at,
prompt: true, ampm: true
.field
= form.label :run
= form.text_field :run, id: :incident_report_run
Expand Down
3 changes: 0 additions & 3 deletions app/views/incident_reports/_show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
%p
%strong= human_name :incident, :bus
= report.bus
%p
%strong= human_name :incident_report, :occurred_at
= report.occurred_at_readable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would people still expect to see this here? Do we want to still display occurred at for the incident the report is associated with or..? This is a business logic question, I really don't know.

%p
%strong= human_name :incident_report, :passengers_onboard
= report.passengers_onboard
Expand Down
8 changes: 4 additions & 4 deletions app/views/incidents/_incident.xml.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
%ai_code= incident.reason_code.identifier
- if incident.root_cause_analysis.present?
%ai_commentary= incident.root_cause_analysis
- if report.occurred_at.present?
%ai_date_occured= report.occurred_at.strftime '%Y-%m-%d'
%ai_time_occured= report.occurred_at.strftime '2000-01-01T%H:%M:00.000'
%ai_occured_year= report.occurred_at.year
- if incident.occurred_at.present?
%ai_date_occured= incident.occurred_at.strftime '%Y-%m-%d'
%ai_time_occured= incident.occurred_at.strftime '2000-01-01T%H:%M:00.000'
%ai_occured_year= incident.occurred_at.year
- if report.direction.present? && report.direction != 'NA'
%ai_direction= report.direction
- if report.town.present?
Expand Down
41 changes: 21 additions & 20 deletions app/views/incidents/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@
- @incident.errors.full_messages.each do |message|
%li= message
= form_with model: @incident, local: true do |form|
= form.fields_for :driver_incident_report, IncidentReport.new do |driver_form|
.field
= driver_form.label :user_id, 'Driver'
= driver_form.select :user_id,
options_from_collection_for_select(@drivers, :id, :proper_name, current_user.id),
{ include_blank: true },
id: :incident_driver_incident_report_attributes_user_id
.field
= driver_form.label :occurred_at
- a11y_datetime_labels('incident_driver_incident_report_attributes_occurred_at').each do |label|
= label
= driver_form.datetime_select :occurred_at, id: :incident_driver_incident_report_attributes_occurred_at,
prompt: true, ampm: true
= form.fields_for :supervisor_incident_report, IncidentReport.new do |supervisor_form|
.field
= supervisor_form.label :user_id, 'Supervisor'
= supervisor_form.select :user_id,
options_from_collection_for_select(@supervisors, :id, :proper_name, current_user.id),
{ include_blank: true },
id: :incident_supervisor_incident_report_attributes_user_id
.field
= form.label :occurred_at
- a11y_datetime_labels('incident_occurred_at').each do |label|
= label
= form.datetime_select :occurred_at, id: :incident_occurred_at,
prompt: true, ampm: true
- unless current_user.driver?
= form.fields_for :driver_incident_report, IncidentReport.new do |driver_form|
.field
= driver_form.label :user_id, 'Driver'
= driver_form.select :user_id,
options_from_collection_for_select(@drivers, :id, :proper_name, current_user.id),
{ include_blank: true },
id: :incident_driver_incident_report_attributes_user_id
= form.fields_for :supervisor_incident_report, IncidentReport.new do |supervisor_form|
.field
= supervisor_form.label :user_id, 'Supervisor'
= supervisor_form.select :user_id,
options_from_collection_for_select(@supervisors, :id, :proper_name, current_user.id),
{ include_blank: true },
id: :incident_supervisor_incident_report_attributes_user_id
.actions= form.submit 'Create Incident Report'
= link_to 'Back', incidents_path
3 changes: 3 additions & 0 deletions app/views/incidents/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
%p
%strong= human_name :incident, :completed
= yes_no_image @incident.completed?
%p
%strong= human_name :incident, :occurred_at
= @incident.occurred_at_readable

- if @incident.completed?
%p
Expand Down
12 changes: 6 additions & 6 deletions app/views/incidents/show.pdf.prawn
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ prawn_document do |pdf|
end

pdf.field_row height: 30, units: 6 do |row|
row.text_field field: 'Date of Incident', value: report.occurred_at.try(:strftime, '%m/%d/%Y')
row.text_field field: 'Time of Incident', value: report.occurred_time
row.text_field field: 'Date of Incident', value: @incident.occurred_at.try(:strftime, '%m/%d/%Y')
row.text_field field: 'Time of Incident', value: @incident.occurred_time
row.text_field field: '# passengers on bus', value: report.passengers_onboard
row.text_field field: '# courtesy cards distributed', value: report.courtesy_cards_distributed
row.text_field field: '# courtesy cards attached', value: report.courtesy_cards_collected
Expand Down Expand Up @@ -217,7 +217,7 @@ prawn_document do |pdf|
row.text_field width: 4, field: 'Operator', value: report.user.proper_name
row.text_field field: 'Badge No.', value: report.user.badge_number
row.text_field field: 'Bus #', value: report.bus
row.text_field width: 2, field: 'Date of Incident', value: report.occurred_at.try(:strftime, '%m/%d/%Y')
row.text_field width: 2, field: 'Date of Incident', value: @incident.occurred_at.try(:strftime, '%m/%d/%Y')
end

pdf.field_row height: pdf.cursor - 30, units: 1 do |row|
Expand Down Expand Up @@ -279,8 +279,8 @@ prawn_document do |pdf|
end

pdf.field_row height: 30, units: 7 do |row|
row.text_field field: 'Date of Incident', value: report.occurred_at.try(:strftime, '%m/%d/%Y')
row.text_field field: 'Time of Incident', value: report.occurred_time
row.text_field field: 'Date of Incident', value: @incident.occurred_at.try(:strftime, '%m/%d/%Y')
row.text_field field: 'Time of Incident', value: @incident.occurred_time
row.text_field field: '# passengers on bus', value: report.passengers_onboard
row.text_field field: '# courtesy cards distributed', value: report.courtesy_cards_distributed
row.text_field field: '# courtesy cards attached', value: report.courtesy_cards_collected
Expand Down Expand Up @@ -480,7 +480,7 @@ prawn_document do |pdf|
value: @incident.driver.proper_name,
options: { valign: :center }
row.text_field field: 'Date and time of incident', width: 2,
value: report.occurred_at.try(:strftime, '%B %e, %Y %-l:%M %P'),
value: @incident.occurred_at.try(:strftime, '%B %e, %Y %-l:%M %P'),
options: { valign: :center }
row.text_field field: 'Location of incident', width: 1,
value: report.location,
Expand Down
1 change: 0 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ en:
block: 'Block #'
bus: 'Bus #'
zip: 'ZIP'
occurred_at: 'Date and time of incident'
speed: 'Speed at time of incident'
motor_vehicle_collision?: 'Did the incident involve a collision?'
passenger_incident?: 'Did the incident involve a passenger?'
Expand Down
2 changes: 1 addition & 1 deletion coverage/.last_run.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"result": {
"covered_percent": 87.86
"covered_percent": 87.58
}
}
5 changes: 5 additions & 0 deletions db/migrate/20201230173222_add_occurred_at_to_incidents.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddOccurredAtToIncidents < ActiveRecord::Migration[5.2]
def change
add_column :incidents, :occurred_at, :datetime
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_07_15_141905) do
ActiveRecord::Schema.define(version: 2020_12_30_173222) do

create_table "divisions", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "name", null: false
Expand Down Expand Up @@ -143,6 +143,7 @@
t.integer "claims_id"
t.boolean "exported_to_claims"
t.integer "supplementary_reason_code_id"
t.datetime "occurred_at"
end

create_table "injured_passengers", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
Expand Down
11 changes: 11 additions & 0 deletions lib/tasks/occurred_at.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace :incidents do
desc 'move occurred_at to incidents'
task move_occurred_at: :environment do
Incident.all.each do |incident|
if incident.driver_incident_report?
occurred_at = incident.driver_incident_report.occurred_at
incident.update(occurred_at: occurred_at)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a task that updates every object in the database like this, there's a (slight) concern for me that for some reason past incidents won't be valid for other reasons. This has happened before. So updating it won't work in that case. Do we want to skip validations here?

end
end
end
end
1 change: 0 additions & 1 deletion spec/factories/incident_reports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
road_conditions { IncidentReport::ROAD_OPTIONS.sample }
light_conditions { IncidentReport::LIGHT_OPTIONS.sample }
description { FFaker::Lorem.paragraphs(5).join "\n" }
occurred_at { DateTime.now.beginning_of_minute }

trait :driver_report do
association :user, factory: %i[user driver]
Expand Down
1 change: 1 addition & 0 deletions spec/factories/incidents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

FactoryBot.define do
factory :incident do
occurred_at { DateTime.now.beginning_of_minute }
association :driver_incident_report,
factory: %i[incident_report driver_report]

Expand Down
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
end

def fill_in_base_incident_fields
fill_in_date_and_time
fill_in 'Bus #', with: '1803'
fill_in 'Location', with: 'Mill and Locust'
fill_in 'ZIP', with: '01108'
Expand Down
20 changes: 15 additions & 5 deletions spec/system/drivers/creating_incidents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@

describe 'creating incidents as a driver' do
before(:each) { when_current_user_is :driver }
it 'brings drivers directly to the form', js: true do
let :new_incident do
visit new_incident_url
fill_in_date_and_time
click_on 'Create Incident Report'
end
it 'prompts to input the date before redirecting to driver report', js: true do
visit incidents_url
find('button', text: 'New Incident').click
expect(page).to have_text 'New Incident'
click_on 'Create Incident Report'
expect(page).to have_text "Date and time of incident can't be blank"
fill_in_date_and_time
click_on 'Create Incident Report'
expect(page).to have_text 'Editing Driver Account of Incident'
driver_report = Incident.last.driver_incident_report
expect(page.current_url).to end_with edit_incident_report_path(driver_report)
end


it 'requires base fields', js: true do
visit new_incident_url
new_incident
save_and_preview

expect(page).to have_text 'This incident report has 6 missing values and so cannot be marked as completed.'
expect(page).to have_text "Date and time of incident can't be blank"
expect(page).to have_text 'This incident report has 5 missing values and so cannot be marked as completed.'
expect(page).to have_text "Location can't be blank"
expect(page).to have_text "Town can't be blank"
expect(page).to have_text "Bus # can't be blank"
Expand All @@ -26,7 +36,7 @@
end

it 'only requires bus, location, and town', js: true do
visit new_incident_url
new_incident
fill_in_base_incident_fields
save_and_preview

Expand Down
Loading