Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
inulty-dfe committed May 28, 2024
1 parent 6ce4ded commit ca3f31c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 29 deletions.
10 changes: 5 additions & 5 deletions app/decorators/course_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ def apprenticeship
end

def sorted_subjects
object.course_subjects.map{ _1.subject.subject_name }.join('<br>').html_safe
object.course_subjects.map { _1.subject.subject_name }.join('<br>').html_safe
end

def chosen_subjects
return sorted_subjects if master_subject_nil?

if main_subject_is_modern_languages?
format_name(modern_language_subjects.to_a.push(additional_subjects.sort_by { |x| [x.subject.type, x.subject.subject_name] }).flatten.uniq.unshift(main_subject))
format_name(modern_language_subjects.to_a.push(additional_subjects.sort_by { |x| [x.type, x.subject_name] }).flatten.uniq.unshift(main_subject))
elsif !main_subject_is_modern_languages? && modern_languages_subjects.present?
format_name(additional_subjects.push(modern_language_subjects.to_a).flatten.uniq.unshift(main_subject))
else
Expand Down Expand Up @@ -489,14 +489,14 @@ def main_subject_is_modern_languages?
end

def main_subject
Subject.find(course.master_subject_id)
@main_subject ||= Subject.find(course.master_subject_id)
end

def additional_subjects
object.course_subjects.reject { |subject| subject.subject_id == main_subject.id }
object.course_subjects.reject { |subject| subject.subject_id == main_subject.id }.map(&:subject)
end

def format_name(subjects)
course_subjects.map { _1.subject.subject_name }.join('<br>').html_safe
subjects.map { _1.subject_name }.join('<br>').html_safe
end
end
22 changes: 7 additions & 15 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ def publish_enrichment(current_user)
end

def is_modern_language_course?
course_subjects.any? { |s| s.subject == SecondarySubject.modern_languages }
course_subjects.any? { |cs| cs.subject == SecondarySubject.modern_languages }
end

def sites=(desired_sites)
Expand Down Expand Up @@ -808,10 +808,10 @@ def remove_carat_from_error_messages
end

def modern_language_subjects
if persisted?
if persisted? && course_subjects.all?(&:persisted?)
subjects.where(type: 'ModernLanguagesSubject')
else
course_subjects.select { _1.subject.type == 'ModernLanguagesSubject' }
course_subjects.select { |cs| cs.subject.type == 'ModernLanguagesSubject' }.map(&:subject)
end
end

Expand All @@ -820,11 +820,7 @@ def master_subject_nil?
end

def has_any_modern_language_subject_type?
if persisted?
subjects.any? { |subject| subject.type == 'ModernLanguagesSubject' }
else
course_subjects.any? { |subject| subject.subject.type == 'ModernLanguagesSubject' }
end
course_subjects.any? { |cs| cs.subject.type == 'ModernLanguagesSubject' }
end

def current_published_enrichment
Expand Down Expand Up @@ -1011,11 +1007,7 @@ def has_the_modern_languages_secondary_subject_type?
raise 'SecondarySubject not found' if SecondarySubject.nil?
raise 'SecondarySubject.modern_languages not found' if SecondarySubject.modern_languages.nil?

if persisted?
subjects.any? { |subject| subject&.id == SecondarySubject.modern_languages.id }
else
course_subjects.any? { |subject| subject.subject&.id == SecondarySubject.modern_languages.id }
end
course_subjects.any? { |cs| cs.subject&.id == SecondarySubject.modern_languages.id }
end

def validate_has_languages
Expand All @@ -1030,9 +1022,9 @@ def validate_subject_count

case level
when 'primary', 'further_education'
errors.add(:subjects, 'has too many subjects') if subjects.count > 1
errors.add(:subjects, 'has too many subjects') if course_subjects.count > 1
when 'secondary'
errors.add(:subjects, 'has too many subjects') if subjects.count > 2 && !has_any_modern_language_subject_type?
errors.add(:subjects, 'has too many subjects') if course_subjects.count > 2 && !has_any_modern_language_subject_type?
end
end

Expand Down
3 changes: 1 addition & 2 deletions app/services/courses/assign_subjects_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,13 @@ def update_subjects
if course.persisted?
Course.transaction do
course.course_subjects.clear
# subject_ids.each_with_index { |subject_id, position| puts subject_id, position}
subject_ids.each_with_index do |subject_id, position|
course.course_subjects.create(subject_id:, position:)
end
course.save
end
else
subject_ids.each_with_index do |subject_id|
subject_ids.each do |subject_id|
course.course_subjects.build(subject_id:)
end
end
Expand Down
3 changes: 1 addition & 2 deletions spec/decorators/course_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@
context 'when modern languages only is chosen' do
let(:french) { build_stubbed(:modern_languages_subject, :french) }
let(:german) { build_stubbed(:modern_languages_subject, :german) }
let(:mod_lang) { build_stubbed(:secondary_subject, :modern_languages) }
let(:subjects) { [mod_lang, french, german] }
let(:subjects) { [french, german] }
let(:course) do
build_stubbed(
:course,
Expand Down
10 changes: 5 additions & 5 deletions spec/models/course_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2693,11 +2693,11 @@
end

context 'when course has two subjects' do
# it 'set the subordinate subject id to the second subjects_id' do
# subordinate_subject = find_or_create(:secondary_subject, :physics)
# course.subjects << subordinate_subject
# expect(course.reload).to have_attributes({ subordinate_subject_id: subordinate_subject.id })
# end
it 'set the subordinate subject id to the second subjects_id' do
subordinate_subject = find_or_create(:secondary_subject, :physics)
course.subjects << subordinate_subject
expect(course.reload).to have_attributes({ subordinate_subject_id: subordinate_subject.id })
end
end
end

Expand Down

0 comments on commit ca3f31c

Please sign in to comment.