From afcf251cf10a8c8316adbc6378f5263a6989adaa Mon Sep 17 00:00:00 2001 From: Iain McNulty Date: Mon, 9 Dec 2024 14:51:39 +0000 Subject: [PATCH] Add CopyCoursesForm for managing form errors --- .../providers/copy_courses_controller.rb | 26 +++++++----- app/forms/support/copy_courses_form.rb | 16 +++++++ .../providers/copy_courses/new.html.erb | 42 ++++++++++--------- config/locales/en.yml | 4 ++ 4 files changed, 58 insertions(+), 30 deletions(-) create mode 100644 app/forms/support/copy_courses_form.rb diff --git a/app/controllers/support/providers/copy_courses_controller.rb b/app/controllers/support/providers/copy_courses_controller.rb index 35a2308117..f7def92f6c 100644 --- a/app/controllers/support/providers/copy_courses_controller.rb +++ b/app/controllers/support/providers/copy_courses_controller.rb @@ -4,25 +4,31 @@ module Support module Providers class CopyCoursesController < SupportController before_action :recruitment_cycle + def new - @provider = Provider.find(params[:provider_id]) + @target_provider = Provider.find(params[:provider_id]) + @copy_courses_form = CopyCoursesForm.new(@target_provider) end def create - @provider = Provider.find(params[:provider_id]) - @from_provider = recruitment_cycle.providers.find_by(provider_code: params[:course][:autocompleted_provider_code]) + @target_provider = Provider.find(params[:provider_id]) + @copy_courses_form = CopyCoursesForm.new(@target_provider, recruitment_cycle.providers.find_by(provider_code: params[:course][:autocompleted_provider_code])) - sites_copy_to_course = params[:schools] ? Sites::CopyToCourseService : ->(*) {} + if @copy_courses_form.valid? + sites_copy_to_course = params[:schools] ? Sites::CopyToCourseService : ->(*) {} - copier = ::Courses::CopyToProviderService.new(sites_copy_to_course:, enrichments_copy_to_course: Enrichments::CopyToCourseService.new, force: true) + copier = ::Courses::CopyToProviderService.new(sites_copy_to_course:, enrichments_copy_to_course: Enrichments::CopyToCourseService.new, force: true) - Provider.transaction do - @from_provider.courses.map do |course| - copier.execute(course:, new_provider: @provider) + Provider.transaction do + @copy_courses_form.provider.courses.map do |course| + copier.execute(course:, new_provider: @copy_courses_form.target_provider) + end end - end - redirect_to support_recruitment_cycle_provider_path(recruitment_cycle.year, @provider.id) + redirect_to support_recruitment_cycle_provider_path(recruitment_cycle.year, @copy_courses_form.target_provider.id) + else + render :new + end end end end diff --git a/app/forms/support/copy_courses_form.rb b/app/forms/support/copy_courses_form.rb new file mode 100644 index 0000000000..b415052187 --- /dev/null +++ b/app/forms/support/copy_courses_form.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module Support + class CopyCoursesForm + include ActiveModel::Model + + attr_reader :target_provider, :provider + + def initialize(target_provider, provider = nil) + @target_provider = target_provider + @provider = provider + end + + validates :target_provider, :provider, presence: true + end +end diff --git a/app/views/support/providers/copy_courses/new.html.erb b/app/views/support/providers/copy_courses/new.html.erb index 348b6128b9..7128498ddd 100644 --- a/app/views/support/providers/copy_courses/new.html.erb +++ b/app/views/support/providers/copy_courses/new.html.erb @@ -1,26 +1,28 @@ -

<%= @provider.provider_name %>

-

Copy courses from:

+<%= render PageTitle.new(title: "support.providers.copy_courses.new", has_errors: @copy_courses_form.errors.present?) %> +
- <%= form_with url: support_recruitment_cycle_provider_copy_courses_path(@recruitment_cycle.year, @provider) do |form| %> - <% error = flash[:error] && flash[:error]["message"] == "Name or provider code" %> -
- <%= form.label :query, { class: "govuk-label", for: "provider" } do %> -
Search for an organisation to copy courses from:
- Enter the name or provider code - <% if error %> - - Please enter the name or provider code - - <% end %> - <% end %> - <%= form.text_field :provider, - id: "provider", - value: params[:query], - class: "govuk-input", - data: { qa: "provider-search" } %> -
+ <%= form_with model: @copy_courses_form, url: support_recruitment_cycle_provider_copy_courses_path(@recruitment_cycle.year, @target_provider) do |form| %> +
+
+ <%= form.govuk_error_summary %> +
+
+

<%= @target_provider.provider_name %>

+

Copy courses from:

+
+
+
+ <%= form.govuk_text_field :provider, + id: "provider", + value: params[:query], + class: "govuk-input", + data: { qa: "provider-search" }, + label: -> { tag.div("Search for an organisation to copy courses from:") + tag.div("Enter the name or provider code", class: %w[govuk-hint govuk-!-display-inline]) } %> +
+
+
diff --git a/config/locales/en.yml b/config/locales/en.yml index 1e470d0741..d4dc0ee4f6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -260,6 +260,8 @@ en: show: "View a provider" edit: "Edit a provider" edit_contact: "Edit provider contact details" + copy_courses: + new: Copy courses courses: index: "Courses" edit: "Edit course details" @@ -1028,6 +1030,8 @@ en: blank: "Enter a first name" last_name: blank: "Enter a last name" + support/copy_courses_form: + blank: Provider can't be blank support/user_form: attributes: email: