Skip to content

Commit

Permalink
Use proc
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie authored and JamieCleare2525 committed May 20, 2024
1 parent 529c3a5 commit db93362
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
7 changes: 3 additions & 4 deletions app/models/placement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
# fk_rails_... (subject_id => subjects.id)
#
class Placement < ApplicationRecord
after_save :assign_subject # TODO: Remove when data migrated
# TODO: Remove when data migrated
after_save :assign_subject, unless: proc { |placement| placement.subject.present? }

has_many :placement_mentor_joins, dependent: :destroy
has_many :mentors, through: :placement_mentor_joins, class_name: "Placements::Mentor"
Expand Down Expand Up @@ -58,9 +59,7 @@ def self.order_by_school_ids(school_ids)

# TODO: Remove after data migrated
def assign_subject
return if subjects.blank? ||
subject.present? ||
subject == subjects.last
return if subjects.blank?

update!(subject: subjects.last)
end
Expand Down
2 changes: 2 additions & 0 deletions spec/factories/placements/placement_additional_subjects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@
#
FactoryBot.define do
factory :placement_additional_subject, class: "Placements::PlacementAdditionalSubject" do
association :subject
association :placement
end
end
8 changes: 4 additions & 4 deletions spec/models/placement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@
end
end

context "when a subject has been associated with the placement" do
context "when the subject is not same as the already associated subject" do
context "when subjects have been associated with the placement" do
context "when subject is nil" do
let(:subjects) { [create(:subject)] }
let(:a_subject) { nil }

Expand All @@ -120,9 +120,9 @@
end
end

context "when the subject is same as the already associated subject" do
context "when subject is not nil" do
let(:subjects) { [create(:subject)] }
let(:a_subject) { subjects.last }
let(:a_subject) { create(:subject) }

it "keeps the original subject assigned to the placement" do
expect(placement.subject).to eq(a_subject)
Expand Down

0 comments on commit db93362

Please sign in to comment.