Skip to content

Commit

Permalink
Add ratifying provider views controller and spec
Browse files Browse the repository at this point in the history
  • Loading branch information
inulty-dfe committed Dec 16, 2024
1 parent d846b38 commit 7a2d735
Show file tree
Hide file tree
Showing 9 changed files with 539 additions and 0 deletions.
172 changes: 172 additions & 0 deletions app/controllers/publish/courses/ratifying_provider_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# frozen_string_literal: true

module Publish
module Courses
class RatifyingProviderController < PublishController
include CourseBasicDetailConcern
before_action :build_course, only: %i[edit update]
before_action :build_course_params, only: :continue
helper_method :accredited_partners

def accredited_partners
if Settings.features.provider_partnerships
@provider.accredited_partners
else
@provider.accredited_bodies
end
end

def show
@course = build_course&.decorate
render_not_found if @course.accrediting_provider.blank?
end

def edit
build_provider
end

def continue
authorize(@provider, :can_create_course?)

code = course_params[:accredited_provider_code]
query = @accredited_provider

@errors = errors_for_search_query(code, query)

if @errors.present?
render :new
elsif other_selected_with_no_autocompleted_code?(code)
redirect_to(
search_new_publish_provider_recruitment_cycle_courses_accredited_provider_path(
query: @accredited_provider,
course: course_params
)
)
else
params[:course][:accredited_provider_code] = @autocompleted_provider_code if @autocompleted_provider_code.present?
super
end
end

def search_new
authorize(provider, :can_create_course?)

# These are not before_action hooks as they conflict with hooks
# defined within the CourseBasicDetailConcern and cannot be overridden
# without causing failures in other routes in this controller
build_new_course
build_provider
build_previous_course_creation_params
@query = params[:query]
@provider_suggestions = recruitment_cycle.providers.with_findable_courses.provider_search(@query).limit(10)
end

def update
build_provider
begin
code = update_course_params[:accredited_provider_code]
query = update_course_params[:accredited_provider]
rescue ActionController::ParameterMissing
@errors = errors_for_search_query(code, query)
return render :edit if @errors.present?
end

if update_params[:accredited_provider_code] == 'other'
redirect_to_provider_search
elsif @course.update(update_params)
course_updated_message('Accredited provider')
redirect_to_update_successful
else
@errors = @course.errors.messages
render :edit
end
end

def search
build_course
@query = params[:query]
@provider_suggestions = recruitment_cycle.providers.with_findable_courses.provider_search(@query).limit(10)
end

private

def build_provider
@provider = RecruitmentCycle.find_by(year: params[:recruitment_cycle_year])
.providers
.find_by(provider_code: params[:provider_code])
end

def error_keys
[:accredited_provider_code]
end

def redirect_to_provider_search
redirect_to(
accredited_provider_search_provider_recruitment_cycle_course_path(
@course.provider_code,
@course.recruitment_cycle_year,
@course.course_code,
query: update_course_params[:accredited_provider]
)
)
end

def redirect_to_update_successful
redirect_to(
details_publish_provider_recruitment_cycle_course_path(
@course.provider_code,
@course.recruitment_cycle_year,
@course.course_code
)
)
end

def current_step
:accredited_provider
end

def build_course_params
@accredited_provider = params[:course].delete(:accredited_provider)
@autocompleted_provider_code = params[:course].delete(:autocompleted_provider_code)
end

def errors_for_search_query(code, query)
errors = {}

if other_selected_with_no_autocompleted_code?(code) && query.length < 2
errors = { accredited_provider: ['Accredited provider search too short, enter 2 or more characters'] }
elsif code.blank?
errors = { accredited_provider_code: ['Select an accredited provider'] }
end

errors
end

def update_course_params
params.require(:course).permit(
:autocompleted_provider_code,
:accredited_provider_code,
:accredited_provider
)
end

def update_params
autocompleted_code = update_course_params[:autocompleted_provider_code]
code = update_course_params[:accredited_provider_code]

{
accredited_provider_code: autocompleted_code.presence || code
}
end

def other_selected_with_no_autocompleted_code?(code)
code == 'other' && @autocompleted_provider_code.blank?
end

def build_course
super
authorize @course
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= render "publish/shared/error_wrapper", error_keys: [:accredited_provider], data_qa: "course__provider_search" do %>
<%= form.label :accredited_provider,
for: "course_accredited_provider",
class: "govuk-label" do %>
Enter the provider name or code <%= render "publish/shared/error_messages", error_keys: [:accredited_provider] %>
<% end %>
<%= form.text_field :accredited_provider,
autocomplete: "off",
class: "govuk-input govuk-!-width-two-thirds" %>
<div id="provider-autocomplete" class="govuk-!-width-two-thirds"></div>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="govuk-radios__item" data-qa="course__accredited_provider_option">
<%= form.radio_button :accredited_provider_code,
provider_suggestion[:provider_code],
checked: provider_suggestion[:provider_code] == @course.accrediting_provider&.provider_code,
class: "govuk-radios__input" %>
<%= form.label :accredited_provider_code,
provider_suggestion[:provider_name],
value: provider_suggestion[:provider_code],
class: "govuk-label govuk-radios__label" %>
</div>
43 changes: 43 additions & 0 deletions app/views/publish/courses/ratifying_provider/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<% content_for :page_title, title_with_error_prefix("Ratifying provider – #{course.name_and_code}", course.errors.any?) %>

<% content_for :before_content do %>
<%= govuk_back_link_to(details_publish_provider_recruitment_cycle_course_path(course.provider_code, course.recruitment_cycle_year, course.course_code)) %>
<% end %>

<%= render "publish/shared/errors" %>

<fieldset class="govuk-fieldset">
<legend class="govuk-fieldset__legend govuk-fieldset__legend--l">
<h1 class="govuk-fieldset__heading">
<%= render CaptionText.new(text: course.name_and_code) %>
Ratifying provider
</h1>
</legend>

<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
<%= form_with model: course,
url: ratifying_provider_publish_provider_recruitment_cycle_course_path(@course.provider_code, @course.recruitment_cycle_year, @course.course_code),
method: :put do |form| %>

<div class="govuk-radios govuk-!-margin-top-2" data-module="govuk-radios" data-qa="course__accredited_provider">
<%= render partial: "provider_suggestion", collection: accredited_partners, locals: { form: } %>
</div>

<div class="govuk-button-group">
<%= form.submit "Update ratifying partner", class: "govuk-button govuk-!-margin-top-5", data: { qa: "course__save" } %>
<%= govuk_link_to(
"Add accredited partner",
search_publish_provider_recruitment_cycle_accredited_providers_path(course.provider_code, course.recruitment_cycle_year),
class: "govuk-!-margin-bottom-6 govuk-!-margin-top-5",
data: { qa: "course__add" }
) %>
</div>
<% end %>

<p class="govuk-body">
<%= govuk_link_to(t("cancel"), details_publish_provider_recruitment_cycle_course_path(@provider.provider_code, @provider.recruitment_cycle_year)) %>
</p>
</div>
</div>
</fieldset>
39 changes: 39 additions & 0 deletions app/views/publish/courses/ratifying_provider/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<% content_for :page_title, title_with_error_prefix("Accredited provider – #{course.name_and_code}", @errors && @errors.any?) %>

<% content_for :before_content do %>
<%= govuk_back_link_to(@back_link_path) %>
<% end %>

<%= render "publish/shared/errors" %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
<%= form_with url: continue_publish_provider_recruitment_cycle_courses_accredited_provider_path(@provider.provider_code, @provider.recruitment_cycle_year), method: :get do |form| %>
<%= render "publish/courses/new_fields_holder", form:, except_keys: [:accredited_provider_code] do |fields| %>
<%= render "publish/shared/error_wrapper", error_keys: [:accredited_provider_code], data_qa: "course__accredited_provider" do %>
<fieldset class="govuk-fieldset">
<legend class="govuk-fieldset__legend govuk-fieldset__legend--l">
<h1 class="govuk-fieldset__heading">
<%= render CaptionText.new(text: t("course.add_course")) %>
Accredited provider
</h1>
</legend>
<%= render "publish/shared/error_messages", error_keys: [:accredited_provider_code] %>

<div class="govuk-radios" data-module="govuk-radios" data-qa="course__accredited_provider">
<% if @provider.accredited_bodies.length > 0 %>
<%= render partial: "provider_suggestion", collection: @provider.accredited_bodies.sort_by { |k| k[:provider_name] }, locals: { form: fields } %>
<% else %>
<%= fields.hidden_field :accredited_provider_code, value: :other %>
<%= render "provider_search_field", form: fields %>
<% end %>
</div>
<% end %>
<% end %>
<p class="govuk-body">
<%= govuk_link_to(t("cancel"), publish_provider_recruitment_cycle_courses_path(@provider.provider_code, @provider.recruitment_cycle_year)) %>
</p>
</div>
</fieldset>
<% end %>
</div>
</div>
57 changes: 57 additions & 0 deletions app/views/publish/courses/ratifying_provider/search.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<% content_for :page_title, title_with_error_prefix("Organisation search", course.errors.any?) %>

<% content_for :before_content do %>
<%= govuk_back_link_to(details_provider_recruitment_cycle_course_path(course.provider_code, course.recruitment_cycle_year, course.course_code)) %>
<% end %>

<%= render "publish/shared/errors" %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-l">
Pick an accredited provider
</h1>

<p class="govuk-body">You searched for ‘<%= @query %>’.</p>

<%= form_with model: course,
url: accredited_provider_provider_recruitment_cycle_course_path(params[:provider_code], params[:recruitment_cycle_year], params[:code]),
method: :put do |form| %>

<% if @provider_suggestions.any? %>
<p class="govuk-body">We found these providers which matched your search:</p>

<div class="govuk-radios" data-module="govuk-radios" data-qa="course__accredited_provider">
<%= render partial: "provider_suggestion", collection: @provider_suggestions, locals: { form: } %>
<div class="govuk-radios__divider">or</div>

<div class="govuk-radios__item">
<%= form.radio_button :accredited_provider_code,
"other",
class: "govuk-radios__input",
data: { "aria-controls" => "other-container" } %>
<%= form.label :accredited_provider_code,
"Try another accrediting provider",
for: "course_accredited_provider_code_other",
value: false,
class: "govuk-label govuk-radios__label" %>
</div>

<div class="govuk-radios__conditional govuk-radios__conditional--hidden" id="other-container">
<%= render "provider_search_field", form: %>
</div>
</div>
<% else %>
<p class="govuk-body">We did not find any accredited bodies which matched your search.</p>

<p class="govuk-body">Try another search:</p>

<%= form.hidden_field :accredited_provider_code, value: :other %>
<%= render "provider_search_field", form: %>
<% end %>

<%= form.submit course.is_running? ? "Save and publish changes" : "Save",
class: "govuk-button govuk-!-margin-top-3", data: { qa: "course__save" } %>
<% end %>
</div>
</div>
61 changes: 61 additions & 0 deletions app/views/publish/courses/ratifying_provider/search_new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<% content_for :page_title, title_with_error_prefix("Organisation search", course.errors.any?) %>

<%= render "publish/shared/errors" %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-l">
Pick an accredited provider
</h1>

<p class="govuk-body">You searched for ‘<%= @query %>’.</p>

<%= form_with model: course,
url: continue_publish_provider_recruitment_cycle_courses_accredited_provider_path(
@provider.provider_code,
@provider.recruitment_cycle_year
),
method: :get do |form| %>

<%= render "publish/shared/course_creation_hidden_fields",
form:,
course_creation_params: @course_creation_params,
except_keys: [:accredited_provider_code] %>

<% if @provider_suggestions.any? %>
<p class="govuk-body">We found these providers which matched your search:</p>

<div class="govuk-radios" data-module="govuk-radios" data-qa="course__accredited_provider">
<%= render partial: "provider_suggestion", collection: @provider_suggestions, locals: { form: } %>
<div class="govuk-radios__divider">or</div>

<div class="govuk-radios__item">
<%= form.radio_button :accredited_provider_code,
"other",
class: "govuk-radios__input",
data: { "aria-controls" => "other-container" } %>
<%= form.label :accredited_provider_code,
"Try another accrediting provider",
for: "course_accredited_provider_code_other",
value: false,
class: "govuk-label govuk-radios__label" %>
</div>

<div class="govuk-radios__conditional govuk-radios__conditional--hidden" id="other-container">
<%= render "provider_search_field", form: %>
</div>
</div>
<% else %>
<p class="govuk-body">We did not find any accredited bodies which matched your search.</p>

<p class="govuk-body">Try another search:</p>

<%= form.hidden_field :accredited_provider_code, value: :other %>
<%= render "provider_search_field", form: %>
<% end %>

<%= form.submit course.is_running? ? "Save and publish changes" : "Save",
class: "govuk-button govuk-!-margin-top-3", data: { qa: "course__save" } %>
<% end %>
</div>
</div>
Loading

0 comments on commit 7a2d735

Please sign in to comment.