piece_justificative_service: fix for missing order_place

In production some pieces justificatives don't have an order place.

In this case, insert the champs after the ones that have an order place.
This commit is contained in:
Pierre de La Morinerie 2019-05-28 17:20:07 +02:00
parent c7f2113972
commit 44c410d40d
2 changed files with 17 additions and 7 deletions

View file

@ -33,12 +33,8 @@ class PiecesJustificativesService
end
def self.types_pj_as_types_de_champ(procedure)
last_champ = procedure.types_de_champ.last
if last_champ.present?
order_place = last_champ.order_place + 1
else
order_place = 0
end
max_order_place = procedure.types_de_champ.pluck(:order_place).compact.max || -1
order_place = max_order_place + 1
types_de_champ = [
TypeDeChamp.new(

View file

@ -112,11 +112,25 @@ describe PiecesJustificativesService do
create(
:procedure,
types_de_piece_justificative: tpjs,
types_de_champ: [build(:type_de_champ, order_place: 0)]
types_de_champ: [build(:type_de_champ, order_place: 0), build(:type_de_champ, order_place: 1)]
)
end
it 'generates a sequence of incrementing order_places that continues where the last type de champ left off' do
expect(subject.pluck(:order_place)).to contain_exactly(2, 3)
end
end
context 'with pre-existing champs without an order place' do
let(:procedure) do
create(
:procedure,
types_de_piece_justificative: tpjs,
types_de_champ: [build(:type_de_champ, order_place: 0), build(:type_de_champ, order_place: nil)]
)
end
it 'ignores champs without an order place' do
expect(subject.pluck(:order_place)).to contain_exactly(1, 2)
end
end