From 4c7d0ee614c67b437a319e13d3a4157acb4b4097 Mon Sep 17 00:00:00 2001 From: pskl Date: Thu, 21 Nov 2024 13:56:11 +0100 Subject: [PATCH] Fix bug --- app/controllers/ribs_controller.rb | 2 +- app/models/student.rb | 6 ++---- spec/models/rib_spec.rb | 13 +++++++++---- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/controllers/ribs_controller.rb b/app/controllers/ribs_controller.rb index 3c5593311..ab4659371 100644 --- a/app/controllers/ribs_controller.rb +++ b/app/controllers/ribs_controller.rb @@ -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 diff --git a/app/models/student.rb b/app/models/student.rb index 599bb9182..f9615f607 100644 --- a/app/models/student.rb +++ b/app/models/student.rb @@ -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) diff --git a/spec/models/rib_spec.rb b/spec/models/rib_spec.rb index 136cad9dc..bbda55e6f 100644 --- a/spec/models/rib_spec.rb +++ b/spec/models/rib_spec.rb @@ -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 @@ -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