Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
pskl committed Nov 21, 2024
1 parent 4a32f3e commit 4c7d0ee
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/controllers/ribs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def rib_params
:bic,
:name,
:owner_type
).with_defaults(student: @student, establishment_id: current_establishment.id)
).with_defaults(student: @student, establishment: current_establishment)
end

def bulk_ribs_params
Expand Down
6 changes: 2 additions & 4 deletions app/models/student.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,8 @@ def born_in_france?
end

def create_new_rib(rib_params)
transaction do
rib.archive! if rib.present? && rib.archivable?
ribs.create(rib_params)
end
rib.archive! if rib.present? && rib.archivable?
ribs.create(rib_params)
end

def adult_at?(date)
Expand Down
13 changes: 9 additions & 4 deletions spec/models/rib_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
let(:establishment1) { create(:establishment) } # rubocop:disable RSpec/IndexedLet
let(:establishment2) { create(:establishment) } # rubocop:disable RSpec/IndexedLet

let(:classe) { create(:classe, :with_students, establishment: establishment1) }

before do
create(:rib, student: student, establishment: establishment1)
create(:rib, student: student, establishment: establishment2)
create(:schooling, student: student, classe: classe)
end

it "allows multiple active RIBs for different establishments" do
Expand All @@ -47,10 +50,12 @@
expect(new_rib.errors[:student_id]).to include(I18n.t("activerecord.errors.models.rib.attributes.student_id.unarchivable_rib")) # rubocop:disable Layout/LineLength
end

it "allows a new RIB for an establishment if the previous one is archived" do
student.ribs.find_by(establishment: establishment1).archive!
new_rib = build(:rib, student: student, establishment: establishment1)
expect(new_rib).to be_valid
it "allows a new RIB for an establishment if the previous one is archived" do # rubocop:disable RSpec/MultipleExpectations
old_rib = student.ribs.first
new_rib_attrs = build(:rib, student: student, establishment: establishment1).attributes
rib = student.create_new_rib(new_rib_attrs)
expect(old_rib.reload).to be_archived
expect(rib).to be_valid
end
end

Expand Down

0 comments on commit 4c7d0ee

Please sign in to comment.