Skip to content

Commit

Permalink
Update controller and view to use new year group step
Browse files Browse the repository at this point in the history
  • Loading branch information
Kizr committed Jun 17, 2024
1 parent c5dbb26 commit df8639b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
28 changes: 16 additions & 12 deletions app/controllers/placements/schools/placements/build_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def add_additional_subjects
end

def add_year_group
@placement = build_placement
year_groups_for_select
@placement.year_group = session.dig(:add_a_placement, "year_group")
@current_step = Placements::AddPlacement::Steps::YearGroup.new(school:, year_group:)
@previous_step = previous_step(:add_year_group)
end

Expand Down Expand Up @@ -63,14 +61,15 @@ def update
when :check_your_answers
subject = Subject.find(session.dig(:add_a_placement, "subject_id"))
phase = session.dig(:add_a_placement, "phase")
@placement = Placements::Schools::Placements::Build::Placement.new(school:, phase:, subject:)
mentor_step = Placements::AddPlacement::Steps::Mentors.new(school:, mentor_ids:)
@placement.assign_attributes mentor_step.wizard_attributes
year_group_step = Placements::AddPlacement::Steps::YearGroup.new(school:, year_group:)
@placement = Placements::Schools::Placements::Build::Placement.new(school:, phase:, subject:)

@placement.assign_attributes [mentor_step.wizard_attributes, year_group_step.wizard_attributes].inject(:merge)
if subject.has_child_subjects?
@placement.additional_subject_ids = additional_subject_ids
@placement.build_additional_subjects(additional_subject_ids)
end
@placement.year_group = session.dig(:add_a_placement, "year_group") if @placement.phase == "Primary"
@placement.build_mentors(mentor_step.wizard_attributes[:mentor_ids])
@placement.save!

Expand Down Expand Up @@ -119,14 +118,11 @@ def update
render :add_additional_subjects and return
end
when :add_year_group
year_group = params.dig(:placements_schools_placements_build_placement, :year_group)
@placement = build_placement
@placement.year_group = year_group
@current_step = Placements::AddPlacement::Steps::YearGroup.new(school:, year_group: year_group_params)

if @placement.valid_year_group?
session[:add_a_placement][:year_group] = year_group
if @current_step.valid?
session[:add_a_placement][:year_group] = @current_step.year_group
else
year_groups_for_select
render :add_year_group and return
end
when :add_mentors
Expand Down Expand Up @@ -243,6 +239,10 @@ def mentor_ids
@mentor_ids ||= session.dig(:add_a_placement, "mentor_ids")&.compact_blank
end

def year_group
@year_group ||= session.dig(:add_a_placement, "year_group")
end

def phase_params
params.dig(:placements_schools_placements_build_placement, :phase)
end
Expand All @@ -259,6 +259,10 @@ def mentor_ids_params
params.dig(:placements_add_placement_steps_mentors, :mentor_ids).compact_blank
end

def year_group_params
params.dig(:placements_add_placement_steps_year_group, :year_group)
end

def authorize_placement
authorize Placement.new(school:)
end
Expand Down
7 changes: 0 additions & 7 deletions app/models/placements/schools/placements/build/placement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@ def valid_additional_subjects?
end
end

def valid_year_group?
return true if school.phase != "Primary" || year_group.present?

errors.add(:year_group, :invalid)
false
end

def all_valid?
valid_phase? && valid_subject? && valid_additional_subjects?
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<% content_for :page_title, @placement.errors.any? ? t(".title_with_error") : t(".title") %>
<%= render "placements/schools/primary_navigation", school: @school, current_navigation: :placements %>
<% content_for :page_title, @current_step.errors.any? ? t(".title_with_error") : t(".title") %>
<%= render "placements/schools/primary_navigation", school: @current_step.school, current_navigation: :placements %>

<%= content_for(:before_content) do %>
<%= govuk_back_link(href: @previous_step) %>
<% end %>

<div class="govuk-width-container">
<%= form_for(@placement, url: placements_school_placement_build_path(@school, id: :add_year_group), method: :put) do |f| %>
<%= form_for(@current_step, url: placements_school_placement_build_path(@current_step.school, id: :add_year_group), method: :put) do |f| %>
<%= f.govuk_error_summary %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<span class="govuk-caption-l"><%= t(".add_placement") %></span>
<%= f.govuk_collection_radio_buttons :year_group, @year_groups_for_select, :value, :name, :description, legend: { size: "l", text: t(".year_group") } %>
<%= f.govuk_collection_radio_buttons :year_group, @current_step.year_groups_for_selection, :value, :name, :description, legend: { size: "l", text: t(".year_group") } %>

<%= f.govuk_submit t(".continue") %>
</div>
</div>
<% end %>

<p class="govuk-body">
<%= govuk_link_to(t(".cancel"), placements_school_placements_path(@school), no_visited_state: true) %>
<%= govuk_link_to(t(".cancel"), placements_school_placements_path(@current_step.school), no_visited_state: true) %>
</p>
</div>
2 changes: 0 additions & 2 deletions config/locales/en/placements/schools/placements.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ en:
invalid: Select an additional subject
provider_id:
invalid: Select a provider
year_group:
invalid: Select a year group
placement:
attributes:
mentor_ids:
Expand Down

0 comments on commit df8639b

Please sign in to comment.