Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
inulty-dfe committed May 29, 2024
1 parent 8fdab3e commit 9e96703
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/decorators/course_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def computed_subject_name_or_names
elsif (number_of_subjects == 1 || modern_languages_other?) && LANGUAGE_SUBJECT_CODES.exclude?(subjects.first.subject_code)
first_subject_name.downcase
elsif number_of_subjects == 2
transformed_subjects = course_subjects.map { |subject| LANGUAGE_SUBJECT_CODES.include?(subject.subject.subject_code) ? subject.subject.subject_name : subject.subject.subject_name.downcase }
transformed_subjects = course_subjects.map { |cs| LANGUAGE_SUBJECT_CODES.include?(cs.subject.subject_code) ? cs.subject.subject_name : cs.subject.subject_name.downcase }
"#{transformed_subjects.first} with #{transformed_subjects.second}"
else
object.name.gsub('Modern Languages', 'modern languages')
Expand Down
27 changes: 10 additions & 17 deletions app/services/courses/assign_subjects_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,20 @@ def update_subjects

course
.course_subjects
.build(
subject_id: FurtherEducationSubject.instance.id,
position: 1
)
.build(subject_id: FurtherEducationSubject.instance.id)

else
if course.persisted?
Course.transaction do
course.course_subjects.clear
subject_ids.each_with_index do |subject_id, position|
course.course_subjects.create(subject_id:, position:)
end
course.save
end
else
elsif course.persisted?
Course.transaction do
course.course_subjects.clear
subject_ids.each do |subject_id|
course.course_subjects.build(subject_id:)
course.course_subjects.create(subject_id:)
end
course.save
end
else
subject_ids.each do |subject_id|
course.course_subjects.build(subject_id:)
end

course.subordinate_subject_id = subject_ids.second
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/services/courses/creation_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def build_new_course
course = provider.courses.new
course.assign_attributes(course_attributes.except(:subjects_ids, :study_mode))

AssignSubjectsService.call(course:, subject_ids:)
update_study_mode(course)
update_sites(course)
update_study_sites(course)
Expand All @@ -43,7 +44,6 @@ def build_new_course
course_enrichment.course_length = '4 years'
end

AssignSubjectsService.call(course:, subject_ids:)

course.valid?(:new) if course.errors.blank?

Expand Down
46 changes: 44 additions & 2 deletions spec/services/courses/assign_subjects_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,33 @@
end
end

describe 'subordinate subject present but master missing' do
let(:subject_ids) { [] }

context 'with a new course' do
let(:course) { Course.new(subordinate_subject_id: 33) }

it 'raises missing subject error' do
expect(subject.errors.full_messages).to include('Select a subject')
end
end

context 'with a persisted course' do
let(:course) { create(:course, master_subject_id: nil, subordinate_subject_id: 33) }

it 'raises missing subject error' do
expect(subject.errors.full_messages).to include('Select a subject')
end
end
end

context 'primary course' do
let(:subject_ids) { [primary_subject.id] }
let(:course) { Course.new(level: :primary) }
let(:primary_subject) { find_or_create(:primary_subject, :primary) }

it 'sets the subjects' do
expect(subject.course_subjects.map { _1.subject.id }).to eq([primary_subject.id])
expect(subject.course_subjects.map { _1.subject.id }).to eq(subject_ids)
end

it 'sets the name' do
Expand Down Expand Up @@ -74,7 +94,7 @@
let(:subject_ids) { [secondary_subject2.id, secondary_subject.id] }

it 'sets the subjects' do
expect(subject.course_subjects.map { _1.subject.id }).to eq([secondary_subject2.id, secondary_subject.id])
expect(subject.course_subjects.map { _1.subject.id }).to eq(subject_ids)
end

it 'sets the course subjects position' do
Expand All @@ -89,6 +109,28 @@
expect(subject.name).to eq('English with biology')
end
end

context 'with 3 subjects' do
let(:secondary_english) { find_or_create(:secondary_subject, :english) }
let(:language_subject) { find_or_create(:modern_languages_subject, :german) }
let(:subject_ids) { [secondary_english.id, secondary_subject.id, language_subject.id] }

it 'sets the subjects' do
expect(subject.course_subjects.map { _1.subject.id }).to eq(subject_ids)
end

it 'sets the course subjects position' do
expect(subject.course_subjects.first.position).to eq(0)
expect(subject.course_subjects.first.subject.id).to eq(secondary_english.id)

expect(subject.course_subjects.second.position).to eq(1)
expect(subject.course_subjects.second.subject.id).to eq(secondary_subject.id)
end

it 'sets the name' do
expect(subject.name).to eq('English with biology')
end
end
end

context 'further_education course' do
Expand Down

0 comments on commit 9e96703

Please sign in to comment.