-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2255 from DFE-Digital/refactor-support-region-form
Refactor support region and country forms
- Loading branch information
Showing
11 changed files
with
260 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
class SupportInterface::RegionsController < SupportInterface::BaseController | ||
before_action :load_region | ||
|
||
def edit | ||
end | ||
module SupportInterface | ||
class RegionsController < BaseController | ||
def edit | ||
@form = RegionForm.for_existing_region(region) | ||
end | ||
|
||
def update | ||
@region.assign_attributes(region_params) | ||
if @region.invalid? | ||
render :edit, status: :unprocessable_entity | ||
elsif ActiveModel::Type::Boolean.new.cast(params[:preview]) | ||
session[:region] = region_params | ||
redirect_to [:preview, :support_interface, @region] | ||
else | ||
@region.save! | ||
redirect_to %i[support_interface countries] | ||
def update | ||
@form = RegionForm.new(region_params.merge(region:)) | ||
if @form.invalid? | ||
render :edit, status: :unprocessable_entity | ||
elsif ActiveModel::Type::Boolean.new.cast(params[:preview]) | ||
session[:region] = region_params | ||
redirect_to [:preview, :support_interface, region] | ||
else | ||
@form.save! | ||
redirect_to %i[support_interface countries] | ||
end | ||
end | ||
end | ||
|
||
def preview | ||
@region.assign_attributes(session[:region]) | ||
end | ||
def preview | ||
@form = RegionForm.new(session[:region].merge(region:)) | ||
@form.assign_region_attributes | ||
end | ||
|
||
private | ||
private | ||
|
||
def load_region | ||
@region = Region.find(params[:id]) | ||
authorize [:support_interface, @region] | ||
end | ||
def region | ||
@region ||= authorize [:support_interface, Region.find(params[:id])] | ||
end | ||
|
||
def region_params | ||
params.require(:region).permit( | ||
:all_sections_necessary, | ||
:other_information, | ||
:requires_preliminary_check, | ||
:sanction_check, | ||
:sanction_information, | ||
:status_check, | ||
:status_information, | ||
:teaching_authority_address, | ||
:teaching_authority_certificate, | ||
:teaching_authority_emails_string, | ||
:teaching_authority_name, | ||
:teaching_authority_online_checker_url, | ||
:teaching_authority_provides_written_statement, | ||
:teaching_authority_requires_submission_email, | ||
:teaching_authority_websites_string, | ||
:teaching_qualification_information, | ||
:work_history_section_to_omit, | ||
:written_statement_optional, | ||
) | ||
def region_params | ||
params.require(:support_interface_region_form).permit( | ||
:all_sections_necessary, | ||
:other_information, | ||
:requires_preliminary_check, | ||
:sanction_check, | ||
:sanction_information, | ||
:status_check, | ||
:status_information, | ||
:teaching_authority_address, | ||
:teaching_authority_certificate, | ||
:teaching_authority_emails_string, | ||
:teaching_authority_name, | ||
:teaching_authority_online_checker_url, | ||
:teaching_authority_provides_written_statement, | ||
:teaching_authority_requires_submission_email, | ||
:teaching_authority_websites_string, | ||
:teaching_qualification_information, | ||
:work_history_section_to_omit, | ||
:written_statement_optional, | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# frozen_string_literal: true | ||
|
||
class SupportInterface::RegionForm | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
include ActiveModel::Dirty | ||
|
||
attr_accessor :region | ||
validates :region, presence: true | ||
|
||
attribute :all_sections_necessary, :boolean | ||
attribute :other_information, :string | ||
attribute :requires_preliminary_check, :boolean | ||
attribute :sanction_check, :string | ||
attribute :sanction_information, :string | ||
attribute :status_check, :string | ||
attribute :status_information, :string | ||
attribute :teaching_authority_address, :string | ||
attribute :teaching_authority_certificate, :string | ||
attribute :teaching_authority_emails_string, :string | ||
attribute :teaching_authority_name, :string | ||
attribute :teaching_authority_online_checker_url, :string | ||
attribute :teaching_authority_provides_written_statement, :boolean | ||
attribute :teaching_authority_requires_submission_email, :boolean | ||
attribute :teaching_authority_websites_string, :string | ||
attribute :teaching_qualification_information, :string | ||
attribute :work_history_section_to_omit, :string | ||
attribute :written_statement_optional, :boolean | ||
|
||
validates :all_sections_necessary, inclusion: { in: [true, false] } | ||
validates :requires_preliminary_check, inclusion: { in: [true, false] } | ||
validates :sanction_check, inclusion: { in: %w[online written none] } | ||
validates :status_check, inclusion: { in: %w[online written none] } | ||
validates :teaching_authority_name, | ||
format: { | ||
without: /\Athe.*\z/i, | ||
message: "Teaching authority name shouldn't start with ‘the’.", | ||
} | ||
validates :teaching_authority_online_checker_url, url: { allow_blank: true } | ||
validates :teaching_authority_provides_written_statement, | ||
inclusion: { | ||
in: [true, false], | ||
} | ||
validates :work_history_section_to_omit, | ||
inclusion: { | ||
in: %w[whole_section contact_details], | ||
}, | ||
unless: :all_sections_necessary | ||
validates :written_statement_optional, inclusion: { in: [true, false] } | ||
|
||
def save! | ||
assign_region_attributes | ||
|
||
ActiveRecord::Base.transaction { region.save! } | ||
end | ||
|
||
def assign_region_attributes | ||
region.assign_attributes( | ||
application_form_skip_work_history:, | ||
other_information:, | ||
reduced_evidence_accepted:, | ||
requires_preliminary_check:, | ||
sanction_check:, | ||
sanction_information:, | ||
status_check:, | ||
status_information:, | ||
teaching_authority_address:, | ||
teaching_authority_certificate:, | ||
teaching_authority_emails:, | ||
teaching_authority_name:, | ||
teaching_authority_online_checker_url:, | ||
teaching_authority_provides_written_statement:, | ||
teaching_authority_requires_submission_email:, | ||
teaching_authority_websites:, | ||
teaching_qualification_information:, | ||
written_statement_optional:, | ||
) | ||
end | ||
|
||
def teaching_authority_emails | ||
teaching_authority_emails_string.split("\n").map(&:chomp).compact_blank | ||
end | ||
|
||
def teaching_authority_websites | ||
teaching_authority_websites_string.split("\n").map(&:chomp).compact_blank | ||
end | ||
|
||
def application_form_skip_work_history | ||
!all_sections_necessary && work_history_section_to_omit == "whole_section" | ||
end | ||
|
||
def reduced_evidence_accepted | ||
!all_sections_necessary && work_history_section_to_omit == "contact_details" | ||
end | ||
|
||
def self.for_existing_region(region) | ||
all_sections_necessary = | ||
!region.application_form_skip_work_history && | ||
!region.reduced_evidence_accepted | ||
|
||
work_history_section_to_omit = | ||
if region.application_form_skip_work_history | ||
"whole_section" | ||
elsif region.reduced_evidence_accepted | ||
"contact_details" | ||
end | ||
|
||
new( | ||
region:, | ||
all_sections_necessary:, | ||
other_information: region.other_information, | ||
requires_preliminary_check: region.requires_preliminary_check, | ||
sanction_check: region.sanction_check, | ||
sanction_information: region.sanction_information, | ||
status_check: region.status_check, | ||
status_information: region.status_information, | ||
teaching_authority_address: region.teaching_authority_address, | ||
teaching_authority_emails_string: | ||
region.teaching_authority_emails.join("\n"), | ||
teaching_authority_name: region.teaching_authority_name, | ||
teaching_authority_online_checker_url: | ||
region.teaching_authority_online_checker_url, | ||
teaching_authority_provides_written_statement: | ||
region.teaching_authority_provides_written_statement, | ||
teaching_authority_requires_submission_email: | ||
region.teaching_authority_requires_submission_email, | ||
teaching_authority_websites_string: | ||
region.teaching_authority_websites.join("\n"), | ||
teaching_qualification_information: | ||
region.teaching_qualification_information, | ||
work_history_section_to_omit:, | ||
written_statement_optional: region.written_statement_optional, | ||
) | ||
end | ||
end |
Oops, something went wrong.