-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- also added form#t to aid in translations for forms - form strong paramsters now handles checkboxes ie collections
- Loading branch information
Showing
11 changed files
with
159 additions
and
16 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
41 changes: 41 additions & 0 deletions
41
app/forms/journeys/further_education_payments/subjects_taught_form.rb
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,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 |
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
7 changes: 0 additions & 7 deletions
7
app/views/further_education_payments/claims/subject_areas.html.erb
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
app/views/further_education_payments/claims/subjects_taught.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 |
---|---|---|
@@ -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> |
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
59 changes: 59 additions & 0 deletions
59
spec/forms/journeys/further_education_payments/subjects_taught_form_spec.rb
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,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 |