Skip to content

Commit 4e594ec

Browse files
committed
template spike
1 parent 670ddb4 commit 4e594ec

File tree

5 files changed

+54
-171
lines changed

5 files changed

+54
-171
lines changed

app/controllers/publishers/vacancies/wizard_base_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class WizardBaseController < BaseController
99

1010
private
1111

12-
helper_method :current_step, :step_process, :all_steps_valid?, :next_invalid_step, :back_path
12+
helper_method :current_step, :step_process, :back_path
1313

1414
def step_process
1515
Publishers::Vacancies::VacancyStepProcess.new(

app/form_models/form_sequence.rb

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,26 @@
1-
class Publishers::VacancyFormSequence < FormSequence
1+
class Publishers::VacancyFormSequence
22
def initialize(vacancy:, organisation:, step_process:)
33
@vacancy = vacancy
44
@step_process = step_process
55

6-
super(
7-
model: @vacancy,
8-
organisation: organisation,
9-
step_names: @step_process.steps,
10-
form_prefix: "publishers/job_listing",
11-
)
6+
@organisation = organisation
7+
@step_names = @step_process.steps
8+
@form_prefix = "publishers/job_listing"
9+
end
10+
11+
def all_steps_valid?
12+
validatable_steps.all? { |step| step_form(step).valid? }
1213
end
1314

1415
def next_invalid_step
1516
# Due to subjects being an optional step (no validations) it needs to be handled differently
1617
return :subjects if next_incomplete_step_subjects?
1718

18-
validate_all_steps.filter_map { |step, form| step if form.invalid? }.first
19+
validatable_steps.detect { |step| step_form(step).invalid? }
1920
end
2021

2122
private
2223

23-
def validatable_steps
24-
if @vacancy.published?
25-
dependent_steps
26-
else
27-
super
28-
end
29-
end
30-
31-
def dependent_steps # rubocop:disable Metrics/MethodLength
32-
case @step_process.current_step
33-
when :job_location
34-
%i[education_phases key_stages]
35-
when :job_role
36-
%i[key_stages about_the_role]
37-
when :education_phases
38-
%i[key_stages]
39-
when :key_stages
40-
%i[about_the_role]
41-
when :applying_for_the_job
42-
@vacancy.enable_job_applications ? [] : %i[how_to_receive_applications]
43-
when :how_to_receive_applications
44-
if @vacancy.uploaded_form?
45-
%i[application_form]
46-
else
47-
%i[application_link]
48-
end
49-
when :include_additional_documents
50-
%i[documents]
51-
when :contact_details
52-
@vacancy.contact_email_belongs_to_a_publisher? ? [] : %i[confirm_contact_details]
53-
else
54-
[]
55-
end
56-
end
57-
5824
def next_incomplete_step_subjects?
5925
return false unless @vacancy.allow_subjects?
6026
return false if @vacancy.completed_steps.include?("subjects")
@@ -69,4 +35,17 @@ def next_incomplete_step_subjects?
6935
def not_validatable_steps
7036
%i[subjects review].freeze
7137
end
38+
39+
def validatable_steps
40+
@step_names - not_validatable_steps
41+
end
42+
43+
def step_form(step_name)
44+
step_form_class = File.join(@form_prefix, "#{step_name}_form").camelize.constantize
45+
46+
params = step_form_class.load_form(@vacancy)
47+
.merge(current_organisation: @organisation)
48+
49+
step_form_class.new(params, @vacancy)
50+
end
7251
end

app/services/publishers/vacancies/vacancy_step_process.rb

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,38 @@ def initialize(current_step, vacancy:, organisation:)
2323
private
2424

2525
def job_details_steps
26-
steps = %i[job_location job_title job_role education_phases key_stages subjects contract_information start_date pay_package]
27-
steps.delete(:job_location) if organisation.school?
28-
steps.delete(:education_phases) unless vacancy.allow_phase_to_be_set?
29-
steps.delete(:key_stages) unless vacancy.allow_key_stages?
30-
steps.delete(:subjects) unless vacancy.allow_subjects?
31-
32-
steps
26+
first = if organisation.school?
27+
%i[job_title job_role]
28+
else
29+
%i[job_location job_title job_role]
30+
end
31+
phases = vacancy.allow_phase_to_be_set? ? %i[education_phases] : []
32+
stages = vacancy.allow_key_stages? ? %i[key_stages] : []
33+
last = if vacancy.allow_subjects?
34+
%i[subjects contract_information start_date pay_package]
35+
else
36+
%i[contract_information start_date pay_package]
37+
end
38+
first + phases + stages + last
3339
end
3440

3541
def application_process_steps
3642
# if the user enters a contact email that doesn't belong to a publisher in our service we want to make them confirm it.
37-
core_steps = %i[contact_details confirm_contact_details]
3843
early_steps = vacancy.published? ? [] : %i[applying_for_the_job]
3944

40-
if vacancy.enable_job_applications
41-
early_steps + %i[anonymise_applications] + core_steps
42-
else
43-
first_steps = early_steps + %i[how_to_receive_applications]
44-
# receive_applications may not be present (yet) as it is asked in how_to_receive_applications
45-
if vacancy.receive_applications.present?
46-
last_steps = if vacancy.uploaded_form?
47-
%i[anonymise_applications] + core_steps
48-
else
49-
core_steps
50-
end
51-
first_steps + [APPLICATION_METHOD_TO_STEP.fetch(vacancy.receive_applications.to_sym)] + last_steps
52-
else
53-
first_steps + core_steps
54-
end
55-
end
45+
start_steps = if vacancy.enable_job_applications
46+
early_steps + %i[anonymise_applications]
47+
else
48+
first_steps = early_steps + %i[how_to_receive_applications]
49+
# receive_applications may not be present (yet) as it is asked in how_to_receive_applications
50+
if vacancy.receive_applications.present?
51+
last_steps = vacancy.uploaded_form? ? %i[anonymise_applications] : []
52+
first_steps + [APPLICATION_METHOD_TO_STEP.fetch(vacancy.receive_applications.to_sym)] + last_steps
53+
else
54+
first_steps
55+
end
56+
end
57+
start_steps + %i[contact_details confirm_contact_details]
5658
end
5759

5860
def about_the_role_steps

spec/form_models/publishers/vacancy_form_sequence_spec.rb

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -34,69 +34,9 @@
3434
about_the_role
3535
include_additional_documents
3636
documents
37-
review
3837
]
3938
end
4039

41-
describe "#validate_all_steps" do
42-
it "uses forms to validate each validatable step" do
43-
validatable_steps = all_steps - %i[subjects review]
44-
valid_steps = validatable_steps - [:school_visits]
45-
46-
validated_forms = sequence.validate_all_steps
47-
48-
valid_steps.each do |step|
49-
form = validated_forms[step]
50-
next unless form
51-
52-
expect(form.errors).to be_none
53-
expect(form).to be_valid
54-
end
55-
56-
invalid_form = validated_forms[:school_visits]
57-
expect(invalid_form.errors).to have_key(:school_visits)
58-
expect(invalid_form).not_to be_valid
59-
60-
expect(vacancy.errors).to have_key(:school_visits)
61-
expect(vacancy.errors.where(:school_visits).first.options[:step]).to eq(:school_visits)
62-
end
63-
64-
context "when the vacancy has been published" do
65-
let(:vacancy) { create(:vacancy, :secondary, organisations: [organisation]) }
66-
67-
context "when the current step has dependent steps" do
68-
let(:current_step) { :job_location }
69-
let(:validated_forms) { sequence.validate_all_steps }
70-
71-
context "when the dependent steps are invalid" do
72-
let(:vacancy) { create(:vacancy, phases: nil, key_stages: nil, organisations: [organisation]) }
73-
74-
before { allow(vacancy).to receive(:allow_key_stages?).and_return(true) }
75-
76-
it "returns a hash containing invalid forms for each dependent step" do
77-
validated_forms.each_value { |form| expect(form).to be_invalid }
78-
end
79-
end
80-
81-
context "when the dependent steps are valid" do
82-
let(:vacancy) { create(:vacancy, :secondary, organisations: [organisation]) }
83-
84-
it "returns a hash containing valid forms for each dependent step" do
85-
validated_forms.each_value { |form| expect(form).to be_valid }
86-
end
87-
end
88-
end
89-
90-
context "when the current step does not have dependent steps" do
91-
let(:current_step) { :job_title }
92-
93-
it "returns an empty hash" do
94-
expect(sequence.validate_all_steps).to eq({})
95-
end
96-
end
97-
end
98-
end
99-
10040
describe "#all_steps_valid?" do
10141
it "is true if all steps are valid" do
10242
expect(sequence).not_to be_all_steps_valid
@@ -122,6 +62,7 @@
12262
let(:vacancy) { create(:vacancy, :secondary, organisations: [organisation]) }
12363

12464
it "returns true" do
65+
pending("removing dependant steps")
12566
expect(sequence.all_steps_valid?).to be true
12667
end
12768
end
@@ -131,6 +72,7 @@
13172
let(:current_step) { :job_title }
13273

13374
it "returns true" do
75+
pending("removing dependant steps")
13476
expect(sequence.all_steps_valid?).to be true
13577
end
13678
end
@@ -153,6 +95,7 @@
15395
let(:enable_job_applications) { true }
15496

15597
it "does not validate how_to_receive_applications" do
98+
pending("removing dependant steps")
15699
expect(sequence.all_steps_valid?).to be true
157100
end
158101
end
@@ -170,6 +113,7 @@
170113
let(:belongs_to_publisher) { true }
171114

172115
it "does not validate confirm_contact_details" do
116+
pending("removing dependant steps")
173117
expect(sequence.all_steps_valid?).to be true
174118
end
175119
end
@@ -192,7 +136,7 @@
192136
end
193137

194138
context "when the next incomplete step is subjects" do
195-
let(:vacancy) { create(:vacancy, :secondary, completed_steps: %w[job_location job_role education_phases job_title key_stages]) }
139+
let(:vacancy) { create(:draft_vacancy, :secondary, completed_steps: %w[job_location job_role education_phases job_title key_stages]) }
196140

197141
before { allow(vacancy).to receive(:allow_key_stages?).and_return(true) }
198142

0 commit comments

Comments
 (0)