From e864f7078dd7ef7356bbc848b0c2f0a99209a824 Mon Sep 17 00:00:00 2001 From: Lori Bailey <44073106+elceebee@users.noreply.github.com> Date: Tue, 21 May 2024 13:05:13 +0100 Subject: [PATCH] Move Course Length to its own page --- .../publish/courses/fees_controller.rb | 2 +- .../publish/courses/length_controller.rb | 61 ++++++++ .../publish/courses/salary_controller.rb | 2 +- app/forms/publish/course_fee_form.rb | 3 - app/forms/publish/course_length_form.rb | 40 +++++ app/forms/publish/course_salary_form.rb | 7 +- .../publish/funding_type_form_methods.rb | 22 --- .../courses/_course_length_field.html.erb | 27 ++-- .../courses/_description_content.html.erb | 6 +- .../_other_course_length_field.html.erb | 6 +- app/views/publish/courses/fees/edit.html.erb | 6 +- .../publish/courses/length/edit.html.erb | 39 +++++ .../publish/courses/salary/edit.html.erb | 8 +- config/locales/en.yml | 28 +++- config/routes/publish.rb | 2 + ...rb => editing_course_funding_type_spec.rb} | 72 +++------ .../courses/editing_course_length_spec.rb | 148 ++++++++++++++++++ .../publish/viewing_a_course_preview_spec.rb | 3 +- spec/forms/publish/course_fee_form_spec.rb | 31 +--- spec/forms/publish/course_length_form_spec.rb | 46 ++++++ spec/forms/publish/course_salary_form_spec.rb | 32 +--- .../page_objects/publish/course_fee_edit.rb | 7 - 22 files changed, 413 insertions(+), 185 deletions(-) create mode 100644 app/controllers/publish/courses/length_controller.rb create mode 100644 app/forms/publish/course_length_form.rb create mode 100644 app/views/publish/courses/length/edit.html.erb rename spec/features/publish/courses/{editing_course_length_and_funding_type_spec.rb => editing_course_funding_type_spec.rb} (67%) create mode 100644 spec/features/publish/courses/editing_course_length_spec.rb create mode 100644 spec/forms/publish/course_length_form_spec.rb diff --git a/app/controllers/publish/courses/fees_controller.rb b/app/controllers/publish/courses/fees_controller.rb index 20bfee71b6..96db79e5ab 100644 --- a/app/controllers/publish/courses/fees_controller.rb +++ b/app/controllers/publish/courses/fees_controller.rb @@ -26,7 +26,7 @@ def update if goto_preview? redirect_to preview_publish_provider_recruitment_cycle_course_path(provider.provider_code, recruitment_cycle.year, course.course_code) else - course_updated_message('Course length and fees') + course_updated_message I18n.t('publish.providers.course_fees.edit.course_fees') redirect_to publish_provider_recruitment_cycle_course_path( provider.provider_code, diff --git a/app/controllers/publish/courses/length_controller.rb b/app/controllers/publish/courses/length_controller.rb new file mode 100644 index 0000000000..cd7dab0544 --- /dev/null +++ b/app/controllers/publish/courses/length_controller.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +module Publish + module Courses + class LengthController < PublishController + before_action :redirect_if_not_editable + + def edit + authorize(provider) + + @course_length_form = CourseLengthForm.new(course_enrichment) + + @course_length_form.valid? if show_errors_on_publish? + end + + def update + authorize(provider) + + @course_length_form = CourseLengthForm.new(course_enrichment, params: length_params) + + if @course_length_form.save! + course_updated_message I18n.t('publish.providers.course_length.edit.course_length') + + redirect_to publish_provider_recruitment_cycle_course_path( + provider.provider_code, + recruitment_cycle.year, + course.course_code + ) + + else + render :edit + end + end + + private + + def length_params + params.require(:publish_course_length_form) + .permit(*CourseLengthForm::FIELDS) + end + + def course + @course ||= CourseDecorator.new(provider.courses.find_by!(course_code: params[:code])) + end + + def course_enrichment + @course_enrichment ||= course.enrichments.find_or_initialize_draft + end + + def redirect_if_not_editable + return unless course.cannot_change_course_length? + + redirect_to publish_provider_recruitment_cycle_course_path( + provider.provider_code, + recruitment_cycle.year, + course.course_code + ) + end + end + end +end diff --git a/app/controllers/publish/courses/salary_controller.rb b/app/controllers/publish/courses/salary_controller.rb index a8b69422a9..91f577f295 100644 --- a/app/controllers/publish/courses/salary_controller.rb +++ b/app/controllers/publish/courses/salary_controller.rb @@ -16,7 +16,7 @@ def update @course_salary_form = CourseSalaryForm.new(course_enrichment, params: formatted_params) if @course_salary_form.save! - course_updated_message('Course length and salary') + course_updated_message I18n.t('publish.providers.course_salary.edit.course_salary') redirect_to publish_provider_recruitment_cycle_course_path( provider.provider_code, diff --git a/app/forms/publish/course_fee_form.rb b/app/forms/publish/course_fee_form.rb index d47210acf3..ba4a2afffa 100644 --- a/app/forms/publish/course_fee_form.rb +++ b/app/forms/publish/course_fee_form.rb @@ -9,8 +9,6 @@ class CourseFeeForm < BaseModelForm include FundingTypeFormMethods FIELDS = %i[ - course_length - course_length_other_length fee_uk_eu fee_international fee_details @@ -19,7 +17,6 @@ class CourseFeeForm < BaseModelForm attr_accessor(*FIELDS) - validates :course_length, presence: true validates :fee_uk_eu, presence: true validates :fee_international, presence: true, if: -> { course.can_sponsor_student_visa? } diff --git a/app/forms/publish/course_length_form.rb b/app/forms/publish/course_length_form.rb new file mode 100644 index 0000000000..ca5d6c5d3f --- /dev/null +++ b/app/forms/publish/course_length_form.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module Publish + class CourseLengthForm < BaseModelForm + alias course_enrichment model + + FIELDS = %i[course_length course_length_other_length].freeze + + attr_accessor(*FIELDS) + + validates :course_length, presence: true + + private + + def compute_fields + course_enrichment + .attributes + .symbolize_keys + .slice(*FIELDS) + .merge(formatted_params) + .symbolize_keys + end + + def formatted_params + if custom_length_provided? + new_attributes.merge(course_length: new_attributes[:course_length_other_length]) + else + new_attributes + end + end + + def custom_length_provided? + new_attributes[:course_length] == 'Other' && new_attributes[:course_length_other_length].present? + end + + def fields_to_ignore_before_save + [:course_length_other_length] + end + end +end diff --git a/app/forms/publish/course_salary_form.rb b/app/forms/publish/course_salary_form.rb index 11410ca127..83ef8055ec 100644 --- a/app/forms/publish/course_salary_form.rb +++ b/app/forms/publish/course_salary_form.rb @@ -6,15 +6,10 @@ class CourseSalaryForm < BaseModelForm include FundingTypeFormMethods - FIELDS = %i[ - course_length - course_length_other_length - salary_details - ].freeze + FIELDS = %i[salary_details].freeze attr_accessor(*FIELDS) - validates :course_length, presence: true validates :salary_details, presence: true validates :salary_details, words_count: { maximum: 250, message: :too_long } diff --git a/app/forms/publish/funding_type_form_methods.rb b/app/forms/publish/funding_type_form_methods.rb index 2db19c5026..753cde8208 100644 --- a/app/forms/publish/funding_type_form_methods.rb +++ b/app/forms/publish/funding_type_form_methods.rb @@ -2,10 +2,6 @@ module Publish module FundingTypeFormMethods - def other_course_length? - course_length_is_other?(course_length) - end - private def compute_fields @@ -14,29 +10,11 @@ def compute_fields .symbolize_keys .slice(*declared_fields) .merge(new_attributes) - .merge(**hydrate_other_course_length) .symbolize_keys end - def hydrate_other_course_length - return {} unless course_length_is_other?(course_enrichment[:course_length]) - - { - course_length: 'Other', - course_length_other_length: course_enrichment[:course_length] - } - end - - def fields_to_ignore_before_save - [:course_length_other_length] - end - def course course_enrichment.course end - - def course_length_is_other?(value) - value.presence && %w[OneYear TwoYears].exclude?(value) - end end end diff --git a/app/views/publish/courses/_course_length_field.html.erb b/app/views/publish/courses/_course_length_field.html.erb index 27e81154fd..c452581e30 100644 --- a/app/views/publish/courses/_course_length_field.html.erb +++ b/app/views/publish/courses/_course_length_field.html.erb @@ -1,19 +1,24 @@ -<%= f.govuk_radio_buttons_fieldset(:course_length, - legend: { text: "Course length", size: "m" }, +<%= f.govuk_radio_buttons_fieldset( + :course_length, + legend: nil, form_group: { id: form_object.errors.key?(:course_length) ? "course_length-error" : "course-length" - }) do %> - <%= f.govuk_radio_button(:course_length, "OneYear", - checked: @copied_fields_values&.value?("OneYear") || @course.course_length == "OneYear", + } +) do %> + +

+ <%= @course.name_and_code %> + <%= t("publish.providers.course_length.edit.course_length") %> +

+
+ <%= f.govuk_radio_button(:course_length, t("publish.providers.course_length.edit.one_year.value"), data: { qa: "course_course_length_oneyear" }, - label: { text: "1 year" }, + label: { text: t("publish.providers.course_length.edit.one_year.label") }, link_errors: true) %> - <%= f.govuk_radio_button(:course_length, "TwoYears", - checked: @copied_fields_values&.value?("TwoYears") || @course.course_length == "TwoYears", - label: { text: "Up to 2 years" }, + <%= f.govuk_radio_button(:course_length, t("publish.providers.course_length.edit.two_years.value"), + label: { text: t("publish.providers.course_length.edit.two_years.label") }, data: { qa: "course_course_length_twoyears" }) %> - <%= render partial: "publish/courses/other_course_length_field", locals: { f:, course_object: @source_course ? source_course : @course } %> - + <%= render partial: "publish/courses/other_course_length_field", locals: { f:, course_object: @course } %> <% end %> diff --git a/app/views/publish/courses/_description_content.html.erb b/app/views/publish/courses/_description_content.html.erb index 5bca40a534..e956647b3b 100644 --- a/app/views/publish/courses/_description_content.html.erb +++ b/app/views/publish/courses/_description_content.html.erb @@ -47,7 +47,7 @@ "Course length", value_provided?(course.length), %w[course_length], - action_path: course.cannot_change_course_length? ? nil : "#{fees_publish_provider_recruitment_cycle_course_path(@provider.provider_code, course.recruitment_cycle_year, course.course_code)}#course-length", + action_path: course.cannot_change_course_length? ? nil : length_publish_provider_recruitment_cycle_course_path(@provider.provider_code, course.recruitment_cycle_year, course.course_code), action_visually_hidden_text: "course length" ) %> @@ -103,7 +103,7 @@ "Course length", value_provided?(course.length), %w[course_length], - action_path: course.cannot_change_course_length? ? nil : "#{salary_publish_provider_recruitment_cycle_course_path(@provider.provider_code, course.recruitment_cycle_year, course.course_code)}#course-length", + action_path: course.cannot_change_course_length? ? nil : length_publish_provider_recruitment_cycle_course_path(@provider.provider_code, course.recruitment_cycle_year, course.course_code), action_visually_hidden_text: "course length" ) %> @@ -113,7 +113,7 @@ "Salary", value_provided?(course.salary_details), %w[salary_details], - action_path: course.is_withdrawn? ? nil : "#{salary_publish_provider_recruitment_cycle_course_path(@provider.provider_code, course.recruitment_cycle_year, course.course_code)}#salary", + action_path: course.is_withdrawn? ? nil : salary_publish_provider_recruitment_cycle_course_path(@provider.provider_code, course.recruitment_cycle_year, course.course_code), action_visually_hidden_text: "salary" ) %> <% end %> diff --git a/app/views/publish/courses/_other_course_length_field.html.erb b/app/views/publish/courses/_other_course_length_field.html.erb index d9633117ec..f6a94efd4c 100644 --- a/app/views/publish/courses/_other_course_length_field.html.erb +++ b/app/views/publish/courses/_other_course_length_field.html.erb @@ -1,11 +1,11 @@ -<%= f.govuk_radio_button(:course_length, "Other", +<%= f.govuk_radio_button(:course_length, t("publish.providers.course_length.edit.other.value"), checked: course_object.other_course_length?, - label: { text: "Other" }, + label: { text: t("publish.providers.course_length.edit.other.label") }, data: { qa: "course_course_length_other" }) do %> <%= f.govuk_text_field(:course_length_other_length, value: course_object.other_course_length? ? course_object.course_length : "", - label: { text: "Course length", size: "s" }, + label: { text: t("publish.providers.course_length.edit.custom_length.label"), size: "s" }, width: 20, data: { qa: "course_course_length_other_length" }) %> <% end %> diff --git a/app/views/publish/courses/fees/edit.html.erb b/app/views/publish/courses/fees/edit.html.erb index 559e6bde38..82ffe7d392 100644 --- a/app/views/publish/courses/fees/edit.html.erb +++ b/app/views/publish/courses/fees/edit.html.erb @@ -1,4 +1,4 @@ -<% page_title = "Course length and fees" %> +<% page_title = t("publish.providers.course_fees.edit.course_fees") %> <% content_for :page_title, title_with_error_prefix("#{page_title} – #{@course.name_and_code}", @course_fee_form.errors.any?) %> <% if params[:copy_from].present? %> @@ -26,11 +26,9 @@ <%= page_title %> - <%= render partial: "publish/courses/course_length_field", locals: { f:, form_object: @course_fee_form } %> -
-

Course fees

+

<%= t("publish.providers.course_fees.edit.course_fees") %>

<%= f.govuk_text_field(:fee_uk_eu, form_group: { id: @course_fee_form.errors.key?(:fee_uk_eu) ? "fee_uk_eu-error" : "fee-uk" }, diff --git a/app/views/publish/courses/length/edit.html.erb b/app/views/publish/courses/length/edit.html.erb new file mode 100644 index 0000000000..bc159da531 --- /dev/null +++ b/app/views/publish/courses/length/edit.html.erb @@ -0,0 +1,39 @@ +<% content_for :page_title, + title_with_error_prefix( + t("publish.providers.course_length.edit.page_title", course_name_and_code: @course.name_and_code), + @course_length_form.errors.any? + ) %> + +
+
+ <%= form_with( + model: @course_length_form, + url: length_publish_provider_recruitment_cycle_course_path(@provider.provider_code, + @course.recruitment_cycle_year, + @course.course_code), + data: { qa: "enrichment-form", module: "form-check-leave" }, + method: :patch, + local: true + ) do |f| %> + + <% content_for :before_content do %> + <%= govuk_back_link_to( + back_link_path( + param_form_key: f.object_name.to_sym, + params:, + provider_code: @provider.provider_code, + recruitment_cycle_year: @course.recruitment_cycle_year, + course_code: @course.course_code + ) + ) %> + <% end %> + + <%= f.govuk_error_summary %> + + <%= render partial: "publish/courses/course_length_field", locals: { f:, form_object: @course_length_form } %> + + <%= f.govuk_submit t("publish.providers.course_length.edit.update_course_length") %> + + <% end %> +
+
diff --git a/app/views/publish/courses/salary/edit.html.erb b/app/views/publish/courses/salary/edit.html.erb index 1af3b39f2e..029d848ca6 100644 --- a/app/views/publish/courses/salary/edit.html.erb +++ b/app/views/publish/courses/salary/edit.html.erb @@ -1,4 +1,4 @@ -<% page_title = "Course length and salary" %> +<% page_title = t("publish.providers.course_salary.edit.course_salary") %> <% content_for :page_title, title_with_error_prefix("#{page_title} – #{@course.name_and_code}", @course_salary_form.errors.any?) %> <% content_for :before_content do %> @@ -22,12 +22,6 @@ <%= page_title %> - <%= render partial: "publish/courses/course_length_field", locals: { f:, form_object: @course_salary_form } %> - -
- -

Salary

-

Give details about the salary for this course.

You should: