Skip to content

Commit

Permalink
Rename controllers (#699)
Browse files Browse the repository at this point in the history
This renames some controllers, to better handle the conflict between the controllers that handle creating things (eg corrective actions) and those that handle viewing the things. Conventionally these would be the same controller, but because the creation controllers use the `show` action as part of the creation flow (via WickedWizard), these conflict.

This PR follows [a suggestion](#687 (review)) from @slorek to rename the creation controllers as `RecordThingController` so that the view controllers can be `ThingController`, instead of `ViewThingController`. This will allow us to more easily merge the creation controllers back into the regular controllers, when we get around to refactoring away from using WickedWizard.

### Changes

#### Corrective actions

`ViewCorrectiveActionsController` ➡️  `CorrectiveActionsController`
`CorrectiveActionsController` ➡️  `RecordCorrectiveActionsController`

#### Correspondence

`ViewEmailsController` ➡️  `EmailsController`
`EmailsController` ➡️  `RecordEmailsController`
`ViewPhoneCallsController` ➡️  `PhoneCallsController`
`PhoneCallsController` ➡️  `RecordPhoneCallsController`
`ViewMeetingsController` ➡️  `MeetingsController`
`MeetingsController` ➡️  `RecordMeetingsController`
  • Loading branch information
frankieroberto authored Jun 9, 2020
1 parent 546373c commit 17df5ec
Show file tree
Hide file tree
Showing 26 changed files with 320 additions and 329 deletions.
Original file line number Diff line number Diff line change
@@ -1,90 +1,7 @@
class Investigations::CorrectiveActionsController < ApplicationController
include CorrectiveActionsConcern
include FileConcern
set_attachment_names :file
set_file_params_key :corrective_action

include Wicked::Wizard
steps :details, :confirmation

before_action :set_investigation
before_action :set_corrective_action, only: %i[show create update]
before_action :set_attachment, only: %i[show create update]
before_action :store_corrective_action, only: %i[update]

# GET /corrective_actions/1
def show
authorize @investigation, :update?
render_wizard
end

# GET /corrective_actions/new
def new
clear_session
redirect_to wizard_path(steps.first, request.query_parameters)
end

# POST /corrective_actions
# POST /corrective_actions.json
def create
authorize @investigation, :update?
respond_to do |format|
update_attachment
if corrective_action_saved?
format.html { redirect_to investigation_url(@investigation), flash: { success: "Corrective action was successfully recorded." } }
format.json { render :show, status: :created, location: @corrective_action }
else
format.html { render step }
format.json { render json: @corrective_action.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /corrective_actions/1
# PATCH/PUT /corrective_actions/1.json
def update
authorize @investigation, :update?
respond_to do |format|
update_attachment
if corrective_action_valid?
save_attachment
format.html { redirect_to next_wizard_path }
format.json { render :show, status: :ok, location: @corrective_action }
else
format.html { render step }
format.json { render json: @corrective_action.errors, status: :unprocessable_entity }
end
end
end

private

def clear_session
session[:corrective_action] = nil
initialize_file_attachments
end

def store_corrective_action
session[:corrective_action] = @corrective_action.attributes if @corrective_action.valid?(step)
end

def corrective_action_saved?
return false unless corrective_action_valid?

# In addition to attaching to the test, we also attach to the investigation, so the file is surfaced in the ui
attach_blobs_to_list(@file_blob, @investigation.documents)
@corrective_action.save
end

def save_attachment
if params[:corrective_action][:related_file] == "Yes"
@file_blob.save if @file_blob
elsif @file_blob
@file_blob.purge
end
end

def corrective_action_session_params
session[:corrective_action] || {}
@investigation = Investigation.find_by!(pretty_id: params[:investigation_pretty_id])
authorize @investigation, :view_non_protected_details?
@corrective_action = @investigation.corrective_actions.find(params[:id])
end
end
81 changes: 5 additions & 76 deletions psd-web/app/controllers/investigations/emails_controller.rb
Original file line number Diff line number Diff line change
@@ -1,78 +1,7 @@
class Investigations::EmailsController < Investigations::CorrespondenceController
set_file_params_key :correspondence_email
set_attachment_names :email_file, :email_attachment

private

def audit_class
AuditActivity::Correspondence::AddEmail
end

def model_class
Correspondence::Email
end

def common_file_metadata
{
title: correspondence_params["overview"],
has_consumer_info: correspondence_params["has_consumer_info"]
}
end

def email_file_metadata
get_attachment_metadata_params(:email_file)
.merge(common_file_metadata)
.merge(
description: "Original email as a file"
)
end

def email_attachment_metadata
get_attachment_metadata_params(:email_attachment)
.merge(common_file_metadata)
end

def request_params
return {} if params[correspondence_params_key].blank?

params.require(correspondence_params_key).permit(
:correspondent_name,
:email_address,
:email_direction,
:overview,
:details,
:email_subject,
:attachment_description,
:has_consumer_info
)
end

def set_attachments
@email_file_blob, @email_attachment_blob = load_file_attachments
end

def update_attachments
update_blob_metadata @email_file_blob, email_file_metadata
update_blob_metadata @email_attachment_blob, email_attachment_metadata
end

def correspondence_valid?
@correspondence.validate(step || steps.last)
@correspondence.validate_email_file_and_content(@email_file_blob) if step == :content
validate_blob_size(@email_file_blob, @correspondence.errors, "email file")
validate_blob_size(@email_attachment_blob, @correspondence.errors, "email attachment")
Rails.logger.error "#{__method__}: correspondence has errors: #{@correspondence.errors.full_messages}" if @correspondence.errors.any?
@correspondence.errors.empty?
end

def attach_files
attach_blob_to_attachment_slot(@email_file_blob, @correspondence.email_file)
attach_blob_to_attachment_slot(@email_attachment_blob, @correspondence.email_attachment)
attach_blobs_to_list(@email_file_blob, @email_attachment_blob, @investigation.documents)
end

def save_attachments
@email_file_blob.save if @email_file_blob
@email_attachment_blob.save if @email_attachment_blob
class Investigations::EmailsController < ApplicationController
def show
@investigation = Investigation.find_by!(pretty_id: params[:investigation_pretty_id])
authorize @investigation, :view_protected_details?
@email = @investigation.emails.find(params[:id]).decorate
end
end
69 changes: 5 additions & 64 deletions psd-web/app/controllers/investigations/meetings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,66 +1,7 @@
class Investigations::MeetingsController < Investigations::CorrespondenceController
set_attachment_names :transcript, :related_attachment
set_file_params_key :correspondence_meeting

private

def audit_class
AuditActivity::Correspondence::AddMeeting
end

def model_class
Correspondence::Meeting
end

def transcript_metadata
get_attachment_metadata_params(:transcript).merge(
title: correspondence_params["overview"],
description: "Meeting transcript"
)
end

def related_attachment_metadata
get_attachment_metadata_params(:related_attachment).merge(
title: correspondence_params["overview"]
)
end

def request_params
return {} if params[correspondence_params_key].blank?

params.require(correspondence_params_key).permit(
:correspondent_name,
:overview,
:details,
:has_consumer_info
)
end

def set_attachments
@transcript_blob, @related_attachment_blob = load_file_attachments
end

def update_attachments
update_blob_metadata @transcript_blob, transcript_metadata
update_blob_metadata @related_attachment_blob, related_attachment_metadata
end

def correspondence_valid?
@correspondence.validate(step || steps.last)
@correspondence.validate_transcript_and_content(@transcript_blob) if step == :content
validate_blob_size(@transcript_blob, @correspondence.errors, "transcript")
validate_blob_size(@related_attachment_blob, @correspondence.errors, "related attachment")
@correspondence.errors.empty?
end

def attach_files
attach_blob_to_attachment_slot(@transcript_blob, @correspondence.transcript)
attach_blob_to_attachment_slot(@related_attachment_blob, @correspondence.related_attachment)
attach_blobs_to_list(@transcript_blob, @related_attachment_blob, @investigation.documents)
end

def save_attachments
@transcript_blob.save if @transcript_blob
@related_attachment_blob.save if @related_attachment_blob
class Investigations::MeetingsController < ApplicationController
def show
@investigation = Investigation.find_by!(pretty_id: params[:investigation_pretty_id])
authorize @investigation, :view_protected_details?
@meeting = @investigation.meetings.find(params[:id]).decorate
end
end
61 changes: 5 additions & 56 deletions psd-web/app/controllers/investigations/phone_calls_controller.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,7 @@
class Investigations::PhoneCallsController < Investigations::CorrespondenceController
set_attachment_names :transcript
set_file_params_key :correspondence_phone_call

private

def audit_class
AuditActivity::Correspondence::AddPhoneCall
end

def model_class
Correspondence::PhoneCall
end

def file_metadata
get_attachment_metadata_params(:transcript).merge(
title: correspondence_params["overview"],
description: "Call transcript",
has_consumer_info: correspondence_params["has_consumer_info"]
)
end

def request_params
return {} if params[correspondence_params_key].blank?

params.require(correspondence_params_key).permit(
:correspondent_name,
:phone_number,
:overview,
:details,
:has_consumer_info
)
end

def set_attachments
@transcript_blob, * = load_file_attachments
end

def update_attachments
update_blob_metadata @transcript_blob, file_metadata
end

def correspondence_valid?
@correspondence.validate(step || steps.last)
@correspondence.validate_transcript_and_content(@transcript_blob) if step == :content
validate_blob_size(@transcript_blob, @correspondence.errors, "file")
@correspondence.errors.empty?
end

def attach_files
attach_blob_to_attachment_slot(@transcript_blob, @correspondence.transcript)
attach_blobs_to_list(@transcript_blob, @investigation.documents)
end

def save_attachments
@transcript_blob.save if @transcript_blob
class Investigations::PhoneCallsController < ApplicationController
def show
@investigation = Investigation.find_by!(pretty_id: params[:investigation_pretty_id])
authorize @investigation, :view_protected_details?
@phone_call = @investigation.phone_calls.find(params[:id]).decorate
end
end
Loading

0 comments on commit 17df5ec

Please sign in to comment.