-
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 #1654 from DFE-Digital/support-console-preview
Improve support console region and country previews
- Loading branch information
Showing
37 changed files
with
592 additions
and
436 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
146 changes: 75 additions & 71 deletions
146
app/controllers/support_interface/countries_controller.rb
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,83 +1,87 @@ | ||
# frozen_string_literal: true | ||
|
||
class SupportInterface::CountriesController < SupportInterface::BaseController | ||
skip_before_action :authorize_support, only: :confirm_edit | ||
before_action :load_country_and_edit_actions, only: %w[confirm_edit update] | ||
|
||
def index | ||
@countries = Country.includes(:regions).order(:code) | ||
end | ||
|
||
def edit | ||
@country = Country.includes(:regions).find(params[:id]) | ||
@all_regions = @country.regions.map(&:name).join("\n") | ||
end | ||
module SupportInterface | ||
class CountriesController < BaseController | ||
def index | ||
authorize [:support_interface, Country] | ||
@countries = Country.includes(:regions).order(:code) | ||
end | ||
|
||
def confirm_edit | ||
authorize :support, :edit? | ||
@country.assign_attributes(country_params) | ||
end | ||
def edit | ||
@form = | ||
CountryForm.new( | ||
country:, | ||
eligibility_enabled: country.eligibility_enabled, | ||
eligibility_skip_questions: country.eligibility_skip_questions, | ||
has_regions: country.regions.count > 1, | ||
qualifications_information: country.qualifications_information, | ||
region_names: country.regions.pluck(:name).join("\n"), | ||
requires_preliminary_check: country.requires_preliminary_check, | ||
teaching_authority_address: country.teaching_authority_address, | ||
teaching_authority_certificate: | ||
country.teaching_authority_certificate, | ||
teaching_authority_emails_string: | ||
country.teaching_authority_emails_string, | ||
teaching_authority_name: country.teaching_authority_name, | ||
teaching_authority_online_checker_url: | ||
country.teaching_authority_online_checker_url, | ||
teaching_authority_other: country.teaching_authority_other, | ||
teaching_authority_sanction_information: | ||
country.teaching_authority_sanction_information, | ||
teaching_authority_status_information: | ||
country.teaching_authority_status_information, | ||
teaching_authority_websites_string: | ||
country.teaching_authority_websites_string, | ||
) | ||
end | ||
|
||
def update | ||
if @country.update(country_params) | ||
@diff_actions.each do |action| | ||
case action[:action] | ||
when :create | ||
@country.regions.create!(name: action[:name]) | ||
when :delete | ||
region = @country.regions.find_by!(name: action[:name]) | ||
region.eligibility_checks.delete_all | ||
region.destroy! | ||
end | ||
def update | ||
@form = CountryForm.new(country_params.merge(country:)) | ||
if @form.invalid? | ||
render :edit, status: :unprocessable_entity | ||
elsif ActiveModel::Type::Boolean.new.cast(params[:preview]) | ||
session[:country] = country_params | ||
redirect_to [:preview, :support_interface, country] | ||
else | ||
@form.save! | ||
redirect_to %i[support_interface countries] | ||
end | ||
|
||
flash[ | ||
:success | ||
] = "Successfully updated #{CountryName.from_country(@country)}" | ||
|
||
redirect_to support_interface_countries_path | ||
else | ||
render :edit, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def load_country_and_edit_actions | ||
@country = Country.includes(:regions).find(params[:id]) | ||
@all_regions = (params.dig(:country, :all_regions) || "").chomp | ||
@diff_actions = calculate_diff_actions | ||
end | ||
|
||
def calculate_diff_actions | ||
current_region_names = @country.regions.map(&:name) | ||
new_region_names = @all_regions.split("\n").map(&:chomp) | ||
new_region_names = [""] if new_region_names.empty? | ||
|
||
regions_to_delete = current_region_names - new_region_names | ||
regions_to_create = new_region_names - current_region_names | ||
def preview | ||
@form = CountryForm.new(session[:country].merge(country:)) | ||
@form.assign_country_attributes | ||
end | ||
|
||
delete_actions = regions_to_delete.map { |name| { action: :delete, name: } } | ||
create_actions = regions_to_create.map { |name| { action: :create, name: } } | ||
private | ||
|
||
(delete_actions + create_actions).sort_by { |action| action[:name] } | ||
end | ||
def country | ||
@country ||= | ||
begin | ||
country = Country.includes(:regions).find(params[:id]) | ||
authorize [:support_interface, country] | ||
country | ||
end | ||
end | ||
|
||
def country_params | ||
params.require(:country).permit( | ||
:eligibility_enabled, | ||
:eligibility_skip_questions, | ||
:qualifications_information, | ||
:requires_preliminary_check, | ||
:teaching_authority_name, | ||
:teaching_authority_address, | ||
:teaching_authority_emails_string, | ||
:teaching_authority_websites_string, | ||
:teaching_authority_certificate, | ||
:teaching_authority_other, | ||
:teaching_authority_sanction_information, | ||
:teaching_authority_status_information, | ||
:teaching_authority_online_checker_url, | ||
) | ||
def country_params | ||
params.require(:support_interface_country_form).permit( | ||
:has_regions, | ||
:region_names, | ||
:eligibility_enabled, | ||
:eligibility_skip_questions, | ||
:qualifications_information, | ||
:requires_preliminary_check, | ||
:teaching_authority_name, | ||
:teaching_authority_address, | ||
:teaching_authority_emails_string, | ||
:teaching_authority_websites_string, | ||
:teaching_authority_certificate, | ||
:teaching_authority_other, | ||
:teaching_authority_sanction_information, | ||
:teaching_authority_status_information, | ||
:teaching_authority_online_checker_url, | ||
) | ||
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
5 changes: 5 additions & 0 deletions
5
app/controllers/support_interface/feature_flags_controller.rb
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,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class SupportInterface::FeatureFlagsController < SupportInterface::BaseController | ||
before_action { authorize %i[support_interface feature_flag] } | ||
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
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,90 @@ | ||
# frozen_string_literal: true | ||
|
||
class SupportInterface::CountryForm | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
attr_accessor :country | ||
validates :country, presence: true | ||
|
||
attribute :eligibility_enabled, :boolean | ||
attribute :eligibility_skip_questions, :boolean | ||
attribute :has_regions, :boolean | ||
attribute :qualifications_information, :string | ||
attribute :region_names, :string | ||
attribute :requires_preliminary_check, :boolean | ||
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_other, :string | ||
attribute :teaching_authority_sanction_information, :string | ||
attribute :teaching_authority_status_information, :string | ||
attribute :teaching_authority_websites_string, :string | ||
|
||
validates :eligibility_enabled, inclusion: { in: [true, false] } | ||
validates :eligibility_skip_questions, inclusion: { in: [true, false] } | ||
validates :has_regions, inclusion: { in: [true, false] } | ||
validates :region_names, presence: true, if: :has_regions | ||
validates :requires_preliminary_check, inclusion: { in: [true, false] } | ||
|
||
def save! | ||
ActiveRecord::Base.transaction do | ||
assign_country_attributes | ||
country.save! | ||
|
||
diff_actions.each do |action| | ||
case action[:action] | ||
when :create | ||
@country.regions.create!(name: action[:name]) | ||
when :delete | ||
region = @country.regions.find_by!(name: action[:name]) | ||
region.eligibility_checks.delete_all | ||
region.destroy! | ||
end | ||
end | ||
end | ||
|
||
true | ||
end | ||
|
||
def assign_country_attributes | ||
country.assign_attributes( | ||
eligibility_enabled:, | ||
eligibility_skip_questions:, | ||
qualifications_information:, | ||
requires_preliminary_check:, | ||
teaching_authority_address:, | ||
teaching_authority_certificate:, | ||
teaching_authority_emails_string:, | ||
teaching_authority_name:, | ||
teaching_authority_online_checker_url:, | ||
teaching_authority_other:, | ||
teaching_authority_sanction_information:, | ||
teaching_authority_status_information:, | ||
teaching_authority_websites_string:, | ||
) | ||
end | ||
|
||
def diff_actions | ||
@diff_actions ||= | ||
begin | ||
current_region_names = country.regions.map(&:name) | ||
new_region_names = | ||
has_regions ? region_names.split("\n").map(&:chomp) : [] | ||
|
||
new_region_names = [""] if new_region_names.empty? | ||
|
||
regions_to_delete = current_region_names - new_region_names | ||
regions_to_create = new_region_names - current_region_names | ||
|
||
delete_actions = | ||
regions_to_delete.map { |name| { action: :delete, name: } } | ||
create_actions = | ||
regions_to_create.map { |name| { action: :create, name: } } | ||
|
||
(delete_actions + create_actions).sort_by { |action| action[:name] } | ||
end | ||
end | ||
end |
Oops, something went wrong.