Skip to content

Commit

Permalink
Update program type before save is committed
Browse files Browse the repository at this point in the history
`after_save :update_program_type!` sets the program type after the record has been saved to the database. This doesn't update the database record unless it is saved again.
  • Loading branch information
Nitemaeric committed Nov 22, 2024
1 parent 1fbf3df commit 13e68c5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Course < ApplicationRecord
after_validation :remove_unnecessary_enrichments_validation_message
before_save :set_applications_open_from

after_save :update_program_type!, if: :saved_change_to_funding?
before_save :update_program_type!, if: :will_save_change_to_funding?

belongs_to :provider

Expand Down
7 changes: 7 additions & 0 deletions spec/factories/courses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,22 @@

trait :with_higher_education do
program_type { :higher_education_programme }
funding { :fee }
end

trait :with_school_direct do
program_type { :school_direct_training_programme }
funding { :fee }
end

trait :with_scitt do
program_type { :scitt_programme }
funding { :fee }
end

trait :with_apprenticeship do
program_type { :pg_teaching_apprenticeship }
funding { :apprenticeship }
end

trait :with_teacher_degree_apprenticeship do
Expand All @@ -186,13 +190,15 @@

trait :with_salary do
program_type { :school_direct_salaried_training_programme }
funding { :salary }
end

trait :fee_type_based do
program_type do
%i[higher_education_programme school_direct_training_programme
scitt_programme].sample
end
funding { :fee }
end

trait :fee do
Expand All @@ -209,6 +215,7 @@

trait :salary_type_based do
program_type { %i[pg_teaching_apprenticeship school_direct_salaried_training_programme].sample }
funding { :salary }
end

trait :non_salary_type_based do
Expand Down
21 changes: 14 additions & 7 deletions spec/features/find/result_page_filters/salary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,56 @@ def given_i_have_salaried_and_fee_courses
:secondary,
:open,
site_statuses:,
program_type: :higher_education_programme
program_type: :higher_education_programme,
funding: :fee
)
@course_scitt_salaried_programme = create(
:course,
:secondary,
:open,
site_statuses:,
program_type: :scitt_salaried_programme
program_type: :scitt_salaried_programme,
funding: :salary
)
@course_higher_education_salaried_programme = create(
:course,
:secondary,
:open,
site_statuses:,
program_type: :higher_education_salaried_programme
program_type: :higher_education_salaried_programme,
funding: :salary
)
@course_school_direct_training_programme = create(
:course,
:secondary,
:open,
site_statuses:,
program_type: :school_direct_training_programme
program_type: :school_direct_training_programme,
funding: :fee
)
@course_school_direct_salaried_training_programme = create(
:course,
:secondary,
:open,
site_statuses:,
program_type: :school_direct_salaried_training_programme
program_type: :school_direct_salaried_training_programme,
funding: :salary
)
@course_scitt_programme = create(
:course,
:secondary,
:open,
site_statuses:,
program_type: :scitt_programme
program_type: :scitt_programme,
funding: :fee
)
@course_pg_teaching_apprenticeship = create(
:course,
:secondary,
:open,
site_statuses:,
program_type: :pg_teaching_apprenticeship
program_type: :pg_teaching_apprenticeship,
funding: :apprenticeship
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def and_there_is_a_5_year_course_i_want_to_edit
end

def and_there_is_an_uneditable_course
given_a_course_exists(program_type: 'TDA')
given_a_course_exists(program_type: 'TDA', funding: :apprenticeship)
end

def and_there_is_a_course_i_want_to_edit(course_length)
Expand Down
14 changes: 7 additions & 7 deletions spec/models/course_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1170,28 +1170,28 @@
subject { described_class.with_salary }

let!(:course_higher_education_programme) do
create(:course, program_type: :higher_education_programme)
create(:course, program_type: :higher_education_programme, funding: :fee)
end

let!(:course_scitt_salaried_programme) do
create(:course, program_type: :scitt_salaried_programme)
create(:course, program_type: :scitt_salaried_programme, funding: :salary)
end

let!(:course_higher_education_salaried_programme) do
create(:course, program_type: :higher_education_salaried_programme)
create(:course, program_type: :higher_education_salaried_programme, funding: :salary)
end

let!(:course_school_direct_training_programme) do
create(:course, program_type: :school_direct_training_programme)
create(:course, program_type: :school_direct_training_programme, funding: :fee)
end

let!(:course_school_direct_salaried_training_programme) do
create(:course, program_type: :school_direct_salaried_training_programme)
create(:course, program_type: :school_direct_salaried_training_programme, funding: :salary)
end

let!(:course_scitt_programme) { create(:course, program_type: :scitt_programme) }
let!(:course_scitt_programme) { create(:course, program_type: :scitt_programme, funding: :fee) }
let!(:course_pg_teaching_apprenticeship) do
create(:course, program_type: :pg_teaching_apprenticeship)
create(:course, program_type: :pg_teaching_apprenticeship, funding: :apprenticeship)
end

it 'only returns salaried training programme' do
Expand Down

0 comments on commit 13e68c5

Please sign in to comment.