Skip to content

Commit

Permalink
Update AddCourseButton
Browse files Browse the repository at this point in the history
  • Loading branch information
inulty-dfe committed Jan 23, 2025
1 parent 484dc5a commit af3264b
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 64 deletions.
6 changes: 3 additions & 3 deletions app/components/add_course_button.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% if required_organisation_details_present? %>
<%= govuk_button_link_to(
"Add course",
t("components.add_course_button.add_course"),
new_publish_provider_recruitment_cycle_course_path(
provider_code: provider.provider_code,
recruitment_cycle_year: provider.recruitment_cycle_year
Expand All @@ -11,11 +11,11 @@
<% else %>

<div class="govuk-inset-text">
<p class="govuk-body">Before adding a course, you need to:</p>
<p class="govuk-body"><%= t("components.add_course_button.before_adding") %></p>
<ul class="govuk-list govuk-list--bullet">
<% incomplete_sections.each do |section| %>
<li>
<%= govuk_link_to(section.name, section.path) %>
<%= govuk_link_to(section.text, section.path) %>
</li>
<% end %>
</ul>
Expand Down
60 changes: 18 additions & 42 deletions app/components/add_course_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,42 @@ class AddCourseButton < ViewComponent::Base
include RecruitmentCycleHelper
attr_reader :provider

Section = Struct.new(:name, :path, keyword_init: true)
Section = Struct.new(:text, :path, keyword_init: true)

def initialize(provider:)
@provider = provider
super
@provider = provider
end

private

def incomplete_sections
incomplete_sections_hash.keys.select { |section| send(section) }.map do |section|
Section.new(name: "add #{incomplete_section_article(section)} #{incomplete_section_label_suffix(section)}", path: incomplete_sections_hash[section])
end
end

def incomplete_sections_hash
{
site_not_present?: publish_provider_recruitment_cycle_schools_path(provider.provider_code, provider.recruitment_cycle_year),
accredited_provider_not_present?: publish_provider_recruitment_cycle_accredited_providers_path(provider.provider_code, provider.recruitment_cycle_year)
}
end

def incomplete_section_label_suffix(section)
labels = {
accredited_provider_not_present?: 'accredited provider',
site_not_present?: 'school'
}

labels[section]
def required_organisation_details_present?
incomplete_sections.empty?
end

def required_organisation_details_present?
accredited_provider_present? && site_present?
def incomplete_sections
@incomplete_sections ||= [school, accredited_provider].compact
end

def accredited_provider_present?
return true if accredited_provider?
private

provider.accredited_providers.any?
end
def accredited_provider
return if accredited_partner_present?

def site_present?
provider.sites.any?
Section.new(text: t('components.add_course_button.add_accredited_provider'), path: publish_provider_recruitment_cycle_accredited_partnerships_path(provider.provider_code, provider.recruitment_cycle_year))
end

def accredited_provider_not_present?
return false if provider.accredited?
def school
return if school_present?

!accredited_provider_present?
Section.new(text: t('components.add_course_button.add_school'), path: publish_provider_recruitment_cycle_schools_path(provider.provider_code, provider.recruitment_cycle_year))
end

def site_not_present?
!site_present?
end
def accredited_partner_present?
return true if provider.accredited?

def accredited_provider?
provider.accredited?
provider.accredited_partners.any?
end

def incomplete_section_article(section)
incomplete_section_label_suffix(section) == 'accredited provider' ? 'an' : 'a'
def school_present?
provider.sites.any?
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class AddCourseButtonPartnerships < ViewComponent::Base
class AddCourseButtonEnrichments < ViewComponent::Base
include RecruitmentCycleHelper
attr_reader :provider

Expand All @@ -22,48 +22,48 @@ def incomplete_sections
def incomplete_sections_hash
{
site_not_present?: publish_provider_recruitment_cycle_schools_path(provider.provider_code, provider.recruitment_cycle_year),
accredited_partner_not_present?: publish_provider_recruitment_cycle_accredited_partnerships_path(provider.provider_code, provider.recruitment_cycle_year)
accredited_provider_not_present?: publish_provider_recruitment_cycle_accredited_providers_path(provider.provider_code, provider.recruitment_cycle_year)
}
end

def incomplete_section_label_suffix(section)
labels = {
accredited_partner_not_present?: 'accredited partner',
accredited_provider_not_present?: 'accredited provider',
site_not_present?: 'school'
}

labels[section]
end

def required_organisation_details_present?
accredited_partner_present? && site_present?
accredited_provider_present? && site_present?
end

def accredited_partner_present?
def accredited_provider_present?
return true if accredited_provider?

provider.accredited_partners.any?
provider.accredited_providers.any?
end

def site_present?
provider.sites.any?
end

def accredited_partner_not_present?
return false if provider.accredited_provider?
def accredited_provider_not_present?
return false if provider.accredited?

!accredited_partner_present?
!accredited_provider_present?
end

def site_not_present?
!site_present?
end

def accredited_provider?
provider.accredited_provider?
provider.accredited?
end

def incomplete_section_article(section)
incomplete_section_label_suffix(section) == 'accredited partner' ? 'an' : 'a'
incomplete_section_label_suffix(section) == 'accredited provider' ? 'an' : 'a'
end
end
5 changes: 4 additions & 1 deletion app/views/publish/courses/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
<%= content_for :before_content, render_breadcrumbs(:courses) %>

<h1 class="govuk-heading-l">Courses</h1>

<% if Settings.features.provider_partnerships %>
<%= render AddCourseButton.new(provider: @provider) %>
<% else %>
<%= render AddCourseButtonEnrichments.new(provider: @provider) %>
<% end %>

<% if @self_accredited_courses %>
<section data-qa="courses__table-section">
Expand Down
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ en:
school_direct_salaried_training_programme:
label: "Salaried"
components:
add_course_button:
before_adding: "Before adding a course, you need to:"
add_course: Add course
add_accredited_provider: add an accredited provider
add_school: add a school
providers:
copy_course_content_warning_component:
copied_fields_from:
Expand Down
186 changes: 186 additions & 0 deletions spec/components/add_course_button_enrichments_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
# frozen_string_literal: true

require 'rails_helper'

describe AddCourseButtonEnrichments do
include Rails.application.routes.url_helpers

let(:recruitment_cycle) { build(:recruitment_cycle, :next) }
let(:provider) { build(:provider, recruitment_cycle:) }

before do
render_inline(described_class.new(provider:))
end

context 'when the provider has not filled out any required sections' do
it 'renders an accredited provider link' do
expect(rendered_content).to have_link(
'add an accredited provider',
href: publish_provider_recruitment_cycle_accredited_providers_path(
provider.provider_code,
provider.recruitment_cycle.year
)
)
end

it 'renders a schools link' do
expect(rendered_content).to have_link(
'add a school',
href: publish_provider_recruitment_cycle_schools_path(
provider.provider_code,
provider.recruitment_cycle.year
)
)
end
end

context 'when the provider has only added a study site' do
let(:provider) { build(:provider, study_sites: [build(:site, :study_site)], recruitment_cycle:) }

it 'renders a study sites link' do
expect(rendered_content).to have_no_link(
'add a study site',
href: publish_provider_recruitment_cycle_study_sites_path(
provider.provider_code,
provider.recruitment_cycle_year
)
)
end

it 'renders an accredited provider link' do
expect(rendered_content).to have_link(
'add an accredited provider',
href: publish_provider_recruitment_cycle_accredited_providers_path(
provider.provider_code,
provider.recruitment_cycle.year
)
)
end

it 'renders a schools link' do
expect(rendered_content).to have_link(
'add a school',
href: publish_provider_recruitment_cycle_schools_path(
provider.provider_code,
provider.recruitment_cycle.year
)
)
end
end

context 'when the provider is an accredited provider' do
let(:provider) { build(:provider, :accredited_provider, recruitment_cycle:) }

it 'renders an accredited provider link' do
expect(rendered_content).to have_no_link(
'add an accredited provider',
href: publish_provider_recruitment_cycle_accredited_providers_path(
provider.provider_code,
provider.recruitment_cycle.year
)
)
end

it 'renders a schools link' do
expect(rendered_content).to have_link(
'add a school',
href: publish_provider_recruitment_cycle_schools_path(
provider.provider_code,
provider.recruitment_cycle.year
)
)
end
end

context 'when the provider has only added a site' do
let(:provider) { build(:provider, sites: [create(:site)], recruitment_cycle:) }

it 'renders an accredited provider link' do
expect(rendered_content).to have_link(
'add an accredited provider',
href: publish_provider_recruitment_cycle_accredited_providers_path(
provider.provider_code,
provider.recruitment_cycle.year
)
)
end

it 'renders a schools link' do
expect(rendered_content).to have_no_link(
'add a school',
href: publish_provider_recruitment_cycle_schools_path(
provider.provider_code,
provider.recruitment_cycle.year
)
)
end
end

context 'when the provider has added a site and a study site' do
let(:provider) { build(:provider, study_sites: [build(:site, :study_site)], sites: [build(:site)], recruitment_cycle:) }

it 'renders a study sites link' do
expect(rendered_content).to have_no_link(
'add a study site',
href: publish_provider_recruitment_cycle_study_sites_path(
provider.provider_code,
provider.recruitment_cycle_year
)
)
end

it 'renders an accredited provider link' do
expect(rendered_content).to have_link(
'add an accredited provider',
href: publish_provider_recruitment_cycle_accredited_providers_path(
provider.provider_code,
provider.recruitment_cycle.year
)
)
end

it 'renders a schools link' do
expect(rendered_content).to have_no_link(
'add a school',
href: publish_provider_recruitment_cycle_schools_path(
provider.provider_code,
provider.recruitment_cycle.year
)
)
end
end

context 'when the provider has added all required organisation details' do
let(:provider) { build(:provider, :accredited_provider, study_sites: [build(:site, :study_site)], sites: [build(:site)], recruitment_cycle:) }

it 'renders a study sites link' do
expect(rendered_content).to have_no_link(
'add a study site',
href: publish_provider_recruitment_cycle_study_sites_path(
provider.provider_code,
provider.recruitment_cycle_year
)
)
end

it 'renders an accredited provider link' do
expect(rendered_content).to have_no_link(
'add an accredited provider',
href: publish_provider_recruitment_cycle_accredited_providers_path(
provider.provider_code,
provider.recruitment_cycle.year
)
)
end

it 'renders a schools link' do
expect(rendered_content).to have_no_link(
'add a school',
href: publish_provider_recruitment_cycle_schools_path(
provider.provider_code,
provider.recruitment_cycle.year
)
)
end
end
end
Loading

0 comments on commit af3264b

Please sign in to comment.