Skip to content

Commit

Permalink
add subject areas to FE journey
Browse files Browse the repository at this point in the history
- also added form#t to aid in translations for forms
- form strong paramsters now handles checkboxes ie collections
  • Loading branch information
asmega committed Jun 27, 2024
1 parent 59accf5 commit fec33ac
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 16 deletions.
23 changes: 18 additions & 5 deletions app/forms/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def i18n_errors_path(msg, args = {})
I18n.t("#{i18n_namespace}.#{base_key}", default: base_key, **args)
end

def t(key, args = {})
I18n.t(key, scope: "#{i18n_namespace}.forms.#{i18n_form_namespace}", **args)
end

def permitted_params
@permitted_params ||= params.fetch(model_name.param_key, {}).permit(*permitted_attributes)
end
Expand All @@ -49,19 +53,28 @@ def persisted?
private

def permitted_attributes
attribute_names
attributes.keys.map do |key|
field = @attributes[key]

case field.value_before_type_cast
when []
{key => []}
else
key
end
end
end

def i18n_form_namespace
self.class.name.demodulize.gsub("Form", "").underscore
end

def attributes_with_current_value
attributes.each_with_object({}) do |(attribute, _), attributes|
attributes[attribute] = permitted_params[attribute]
next unless attributes[attribute].nil?
attributes.each_with_object({}) do |(attribute, _), hash|
hash[attribute] = permitted_params[attribute]
next unless hash[attribute].nil?

attributes[attribute] = load_current_value(attribute)
hash[attribute] = load_current_value(attribute)
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Journeys
module FurtherEducationPayments
class SubjectsTaughtForm < Form
include ActiveModel::Validations::Callbacks

attribute :subjects_taught, default: []

before_validation :clean_subjects_taught

validates :subjects_taught,
presence: {message: i18n_error_message(:inclusion)},
inclusion: {in: ->(form) { form.radio_options.map(&:id) }, message: i18n_error_message(:inclusion)}

def radio_options
[
OpenStruct.new(id: "building-and-construction", name: "Building and construction"),
OpenStruct.new(id: "chemistry", name: "Chemistry"),
OpenStruct.new(id: "computing", name: "Computing, including digital and ICT"),
OpenStruct.new(id: "early-years", name: "Early years"),
OpenStruct.new(id: "engineering-and-manufacturing", name: "Engineering and manufacturing, including transport engineering and electronics"),
OpenStruct.new(id: "mathematics", name: "Mathematics"),
OpenStruct.new(id: "physics", name: "Physics"),
OpenStruct.new(id: "none", name: "I do not teach any of these subjects")
]
end

def save
return false unless valid?

journey_session.answers.assign_attributes(subjects_taught:)
journey_session.save!
end

private

def clean_subjects_taught
subjects_taught.reject!(&:blank?)
end
end
end
end
3 changes: 2 additions & 1 deletion app/models/journeys/further_education_payments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ module FurtherEducationPayments
"claims" => {
"teaching-responsibilities" => TeachingResponsibilitiesForm,
"further-education-provision-search" => FurtherEducationProvisionSearchForm,
"select-provision" => SelectProvisionForm
"select-provision" => SelectProvisionForm,
"subjects-taught" => SubjectsTaughtForm
}
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class SessionAnswers < Journeys::SessionAnswers
attribute :teaching_responsibilities, :boolean
attribute :provision_search, :string
attribute :school_id, :string # GUID
attribute :subjects_taught, default: []
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SlugSequence
contract-type
teaching-hours-per-week
academic-year-in-further-education
subject-areas
subjects-taught
building-and-construction-courses
teaching-courses
half-teaching-hours
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= form_with model: @form, url: claim_path(current_journey_routing_name), method: :patch, builder: GOVUKDesignSystemFormBuilder::FormBuilder, html: { novalidate: false } do |f| %>
<%= f.govuk_error_summary %>

<%= f.govuk_check_boxes_fieldset :subjects_taught,
legend: {
text: @form.t(:question),
tag: "h1",
size: "l"
},
hint: {
text: @form.t(:hint)
} do %>
<% @form.radio_options[0..-2].each do |option| %>
<%= f.govuk_check_box :subjects_taught, option.id, label: { text: option.name }, link_errors: @form.radio_options.first == option %>
<% end %>

<%= f.govuk_check_box_divider %>

<% option = @form.radio_options.last %>
<%= f.govuk_check_box :subjects_taught, option.id, label: { text: option.name }, exclusive: true %>
<% end %>

<%= f.govuk_submit %>
<% end %>
</div>
</div>
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,11 @@ en:
select_provision:
errors:
blank: Select the college you teach at
subjects_taught:
question: Which subject areas do you teach?
hint: Select all that apply
errors:
inclusion: Select the subject areas you teach in or select you do not teach any of the listed subject areas
activerecord:
errors:
models:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
expect(page).to have_content("FE academic year in further education goes here")
click_button "Continue"

expect(page).to have_content("FE subject areas goes here")
expect(page).to have_content("Which subject areas do you teach?")
check("Building and construction")
click_button "Continue"

expect(page).to have_content("FE building and construction courses goes here")
Expand Down
3 changes: 2 additions & 1 deletion spec/features/further_education_payments/happy_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
expect(page).to have_content("FE academic year in further education goes here")
click_button "Continue"

expect(page).to have_content("FE subject areas goes here")
expect(page).to have_content("Which subject areas do you teach?")
check("Building and construction")
click_button "Continue"

expect(page).to have_content("FE building and construction courses goes here")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require "rails_helper"

RSpec.describe Journeys::FurtherEducationPayments::SubjectsTaughtForm, type: :model do
let(:journey) { Journeys::FurtherEducationPayments }
let(:journey_session) { create(:further_education_payments_session) }
let(:subjects_taught) { [] }

let(:params) do
ActionController::Parameters.new(
claim: {
subjects_taught:
}
)
end

subject do
described_class.new(
journey_session:,
journey:,
params:
)
end

describe "validations" do
context "when no option selected" do
let(:subjects_taught) { [] }

it do
is_expected.not_to(
allow_value([])
.for(:subjects_taught)
.with_message("Select the subject areas you teach in or select you do not teach any of the listed subject areas")
)
end
end

context "when non-existent injection option selected" do
let(:subjects_taught) { ["foo"] }

it do
is_expected.not_to(
allow_value(["foo"])
.for(:subjects_taught)
.with_message("Select the subject areas you teach in or select you do not teach any of the listed subject areas")
)
end
end
end

describe "#save" do
let(:subjects_taught) { ["chemistry", "mathematics"] }

it "updates the journey session" do
expect { expect(subject.save).to be(true) }.to(
change { journey_session.reload.answers.subjects_taught }.to(["chemistry", "mathematics"])
)
end
end
end

0 comments on commit fec33ac

Please sign in to comment.