Skip to content

Commit

Permalink
Refactor ApplicationFormViewObject
Browse files Browse the repository at this point in the history
I've renamed it and combined the decline reason methods together
so we can use them in the views and in the mailers.
  • Loading branch information
thomasleese committed Nov 9, 2023
1 parent 8fdd55d commit fd3ea14
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ApplicationFormsController < BaseController
define_history_check :edit

def new
existing_application_form = current_teacher.application_form
existing_application_form = application_form

@already_applied = existing_application_form.present?
@needs_region = false
Expand All @@ -26,7 +26,7 @@ def new
end

def create
@already_applied = current_teacher.application_form.present?
@already_applied = application_form.present?

@country_region_form =
CountryRegionForm.new(
Expand All @@ -44,15 +44,15 @@ def create
end

def show
@view_object = ApplicationFormShowViewObject.new(current_teacher:)

if @view_object.application_form.nil?
if application_form.nil?
redirect_to %i[new teacher_interface application_form]
end

@view_object = ApplicationFormViewObject.new(application_form:)
end

def edit
@view_object = ApplicationFormShowViewObject.new(current_teacher:)
@view_object = ApplicationFormViewObject.new(application_form:)

@sanction_confirmation_form =
SanctionConfirmationForm.new(
Expand All @@ -62,7 +62,7 @@ def edit
end

def update
@view_object = ApplicationFormShowViewObject.new(current_teacher:)
@view_object = ApplicationFormViewObject.new(application_form:)

if (
@sanction_confirmation_form =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,31 @@
# frozen_string_literal: true

class TeacherInterface::ApplicationFormShowViewObject
class TeacherInterface::ApplicationFormViewObject
include RegionHelper

def initialize(current_teacher:)
@current_teacher = current_teacher
def initialize(application_form:)
@application_form = application_form
end

def teacher
@current_teacher
end

def application_form
@application_form ||= teacher.application_form
end
attr_reader :application_form

def assessment
@assessment ||= application_form&.assessment
end
delegate :assessment, :region, :teacher, to: :application_form

def further_information_request
@further_information_request ||=
assessment&.further_information_requests&.first
end

def professional_standing_request
@professional_standing_request ||= assessment&.professional_standing_request
end

def started_at
application_form.created_at.strftime("%e %B %Y")
application_form.created_at.to_fs(:date).strip
end

def expires_at
(application_form.created_at + 6.months).strftime("%e %B %Y")
application_form.expires_at.to_fs(:date).strip
end

def task_list_sections
@task_list_sections ||= [
[
task_list_section(
:about_you,
%i[personal_information identification_document],
Expand All @@ -62,40 +50,40 @@ def task_list_sections
end

def completed_task_list_sections
@completed_task_list_sections ||=
task_list_sections.filter do |section|
section[:items].all? { |item| item[:status] == "completed" }
end
task_list_sections.filter do |section|
section[:items].all? { |item| item[:status] == "completed" }
end
end

def can_submit?
completed_task_list_sections.count == task_list_sections.count
end

def notes_from_assessors
return [] if assessment.nil? || further_information_request.present?

assessment.sections.filter_map do |section|
next nil if section.selected_failure_reasons.empty?

failure_reasons =
section.selected_failure_reasons.map do |failure_reason|
is_decline =
FailureReasons.decline?(failure_reason: failure_reason.key)

{
key: failure_reason.key,
is_decline:,
assessor_feedback: failure_reason.assessor_feedback,
}
end

failure_reasons =
failure_reasons.sort_by do |failure_reason|
[failure_reason[:is_decline] ? 0 : 1, failure_reason[:key]]
end

{ assessment_section_key: section.key, failure_reasons: }
def declined_reasons
if further_information_request&.expired?
{
"" => [
I18n.t(
"application_form.decline_reasons.further_information_request_expired",
),
],
}
elsif professional_standing_request&.expired?
{
"" => [
I18n.t(
"application_form.decline_reasons.professional_standing_request_expired",
certificate_name: region_certificate_name(region),
teaching_authority_name: region_teaching_authority_name(region),
),
],
}
elsif (note = assessment&.recommendation_assessor_note).present?
{ "" => [note] }
elsif assessment.present? && further_information_request.nil?
assessment_declined_reasons
else
{}
end
end

Expand All @@ -111,14 +99,6 @@ def declined_cannot_reapply?
end
end

def show_further_information_request_expired_content?
further_information_request&.expired? || false
end

def show_professional_standing_request_expired_content?
professional_standing_request&.expired? || false
end

def request_further_information?
further_information_request.present? &&
further_information_request.requested? &&
Expand All @@ -134,17 +114,16 @@ def request_professional_standing_certificate?
) || false
end

delegate :region, to: :application_form

private

delegate :needs_work_history,
:needs_written_statement,
:needs_registration_number,
:teaching_authority_provides_written_statement,
:requires_preliminary_check,
to: :application_form,
allow_nil: true
to: :application_form

delegate :professional_standing_request, to: :assessment, allow_nil: true

def task_list_section(key, item_keys)
{
Expand All @@ -156,14 +135,14 @@ def task_list_section(key, item_keys)
def task_list_items(keys)
keys.map do |key|
{
name: name_for_task_item(key),
link: link_for_task_item(key),
status: status_for_task_item(key),
name: task_list_item_name(key),
link: task_list_item_link(key),
status: task_list_item_status(key),
}
end
end

def name_for_task_item(key)
def task_list_item_name(key)
if key == :written_statement
if application_form.teaching_authority_provides_written_statement
I18n.t("application_form.tasks.items.written_statement.provide")
Expand All @@ -175,7 +154,7 @@ def name_for_task_item(key)
end
end

def link_for_task_item(key)
def task_list_item_link(key)
case key
when :identification_document
[
Expand Down Expand Up @@ -205,7 +184,43 @@ def link_for_task_item(key)
end
end

def status_for_task_item(key)
def task_list_item_status(key)
application_form.send("#{key}_status")
end

def assessment_declined_reasons
assessment
.sections
.each_with_object({}) do |section, hash|
next if section.selected_failure_reasons.empty?

sorted_reasons =
section.selected_failure_reasons.sort_by do |failure_reason|
is_decline =
FailureReasons.decline?(failure_reason: failure_reason.key)

[is_decline ? 0 : 1, failure_reason.key]
end

reasons =
sorted_reasons.map do |failure_reason|
[
I18n.t(
failure_reason.key,
scope: %i[
assessor_interface
assessment_sections
failure_reasons
as_statement
],
),
failure_reason.assessor_feedback,
].compact_blank.join("\n\n")
end

title = section.key.humanize.upcase_first

hash[title] = reasons
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,20 @@

<h3 class="govuk-heading-m">Why your application was declined</h3>

<% if view_object.show_further_information_request_expired_content? %>
<%= govuk_inset_text(text: t(".further_information_request_expired")) %>
<% end %>

<% if view_object.show_professional_standing_request_expired_content? %>
<%= govuk_inset_text(text: t(
".professional_standing_request_expired",
certificate_name: region_certificate_name(view_object.region),
teaching_authority_name: region_teaching_authority_name(view_object.region)
)) %>
<% end %>
<% if (declined_reasons = view_object.declined_reasons).present? %>
<% declined_reasons.each do |title, reasons| %>
<% if title.present? %>
<h4 class="govuk-heading-s"><%= title %></h4>
<% end %>

<% if (sections = view_object.notes_from_assessors).present? %>
<h4 class="govuk-heading-s">Notes from the assessor:</h4>

<% sections.each do |section| %>
<h5 class="govuk-heading-s"><%= t(".assessment_section.#{section[:assessment_section_key]}") %></h5>
<ul class="govuk-list">
<% section[:failure_reasons].each do |failure_reason| %>
<li>
<h6 class="govuk-body">
<%= t(failure_reason[:key], scope: %i[assessor_interface assessment_sections failure_reasons as_statement]) %>
</h6>

<% if (text = failure_reason[:assessor_feedback]).present? %>
<%= govuk_inset_text { simple_format text } %>
<% end %>
</li>
<% reasons.each do |reason| %>
<li><%= simple_format reason %></li>
<% end %>
</ul>
<% end %>
<% end %>

<% if (recommendation_assessor_note = view_object.assessment.recommendation_assessor_note).present? %>
<%= govuk_inset_text { simple_format recommendation_assessor_note } %>
<% end %>

<% unless view_object.declined_cannot_reapply? %>
<h3 class="govuk-heading-m">What you can do next</h3>

Expand Down
4 changes: 2 additions & 2 deletions config/initializers/date_formats.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

Date::DATE_FORMATS[:long_ordinal_uk] = "%-d %B %Y"
Date::DATE_FORMATS[:month_and_year] = "%B %Y"

Time::DATE_FORMATS[:date] = "%e %B %Y"
Time::DATE_FORMATS[:date_and_time] = "%e %B %Y at %l:%M %P"

Date::DATE_FORMATS[:month_and_year] = "%B %Y"
6 changes: 3 additions & 3 deletions config/locales/teacher_interface.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ en:
teacher_interface:
application_forms:
show:
declined:
further_information_request_expired: Your application has been declined as you did not respond to the assessor’s request for further information within the specified time.
professional_standing_request_expired: Your application has been declined as we did not receive your %{certificate_name} from %{teaching_authority_name} within 180 days.
draft:
check: Check your application
save: Save and sign out
Expand Down Expand Up @@ -68,6 +65,9 @@ en:
title: Delete work history

application_form:
decline_reasons:
further_information_request_expired: Your application has been declined as you did not respond to the assessor’s request for further information within the specified time.
professional_standing_request_expired: Your application has been declined as we did not receive your %{certificate_name} from %{teaching_authority_name} within 180 days.
tasks:
sections:
about_you: About you
Expand Down
Loading

0 comments on commit fd3ea14

Please sign in to comment.