Skip to content

Add occurred at to incidents#292

Open
avacmeyer wants to merge 5 commits intomainfrom
add_occurred_at_to_incidents
Open

Add occurred at to incidents#292
avacmeyer wants to merge 5 commits intomainfrom
add_occurred_at_to_incidents

Conversation

@avacmeyer
Copy link
Contributor

I moved the field occurred_at from incident_report to incidents. The driver now has has to input a date and time before being redirected to filling out their incident_report. This is part one of two PRs to close #288.
Screen Shot 2021-01-01 at 2 10 27 PM

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
Contributor

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.

= report.bus
%p
%strong= human_name :incident_report, :occurred_at
= report.occurred_at_readable
Copy link
Contributor

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.

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
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Moving occurred_at from incident_report to incident

2 participants