Skip to content

Commit

Permalink
Move Course Length to its own page
Browse files Browse the repository at this point in the history
  • Loading branch information
elceebee committed May 22, 2024
1 parent 781a383 commit e864f70
Show file tree
Hide file tree
Showing 22 changed files with 413 additions and 185 deletions.
2 changes: 1 addition & 1 deletion app/controllers/publish/courses/fees_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 61 additions & 0 deletions app/controllers/publish/courses/length_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/controllers/publish/courses/salary_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 0 additions & 3 deletions app/forms/publish/course_fee_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ class CourseFeeForm < BaseModelForm
include FundingTypeFormMethods

FIELDS = %i[
course_length
course_length_other_length
fee_uk_eu
fee_international
fee_details
Expand All @@ -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? }

Expand Down
40 changes: 40 additions & 0 deletions app/forms/publish/course_length_form.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 1 addition & 6 deletions app/forms/publish/course_salary_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
22 changes: 0 additions & 22 deletions app/forms/publish/funding_type_form_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

module Publish
module FundingTypeFormMethods
def other_course_length?
course_length_is_other?(course_length)
end

private

def compute_fields
Expand All @@ -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
27 changes: 16 additions & 11 deletions app/views/publish/courses/_course_length_field.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
<legend>
<h1 class="govuk-heading-l">
<span class="govuk-caption-l"><%= @course.name_and_code %></span>
<%= t("publish.providers.course_length.edit.course_length") %>
</h1>
</legend>
<%= 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 %>
6 changes: 3 additions & 3 deletions app/views/publish/courses/_description_content.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
) %>

Expand Down Expand Up @@ -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"
) %>

Expand All @@ -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 %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/publish/courses/_other_course_length_field.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
6 changes: 2 additions & 4 deletions app/views/publish/courses/fees/edit.html.erb
Original file line number Diff line number Diff line change
@@ -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? %>
Expand Down Expand Up @@ -26,11 +26,9 @@
<%= page_title %>
</h1>

<%= render partial: "publish/courses/course_length_field", locals: { f:, form_object: @course_fee_form } %>

<hr class="govuk-section-break govuk-section-break--visible govuk-section-break--l">

<h3 class="govuk-heading-m">Course fees</h3>
<h2 class="govuk-heading-m"><%= t("publish.providers.course_fees.edit.course_fees") %></h2>

<%= f.govuk_text_field(:fee_uk_eu,
form_group: { id: @course_fee_form.errors.key?(:fee_uk_eu) ? "fee_uk_eu-error" : "fee-uk" },
Expand Down
39 changes: 39 additions & 0 deletions app/views/publish/courses/length/edit.html.erb
Original file line number Diff line number Diff line change
@@ -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?
) %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= 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 %>
</div>
</div>
8 changes: 1 addition & 7 deletions app/views/publish/courses/salary/edit.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
Expand All @@ -22,12 +22,6 @@
<%= page_title %>
</h1>

<%= render partial: "publish/courses/course_length_field", locals: { f:, form_object: @course_salary_form } %>

<hr class="govuk-section-break govuk-section-break--visible govuk-section-break--l">

<h3 class="govuk-heading-m">Salary</h3>

<p class="govuk-body">Give details about the salary for this course.</p>
<p class="govuk-body">You should:</p>
<ul class="govuk-list govuk-list--bullet">
Expand Down
Loading

0 comments on commit e864f70

Please sign in to comment.