-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
413 additions
and
185 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 |
---|---|---|
@@ -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 |
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,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 |
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 |
---|---|---|
@@ -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 %> |
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
6 changes: 3 additions & 3 deletions
6
app/views/publish/courses/_other_course_length_field.html.erb
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,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 %> |
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,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> |
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
Oops, something went wrong.