Insert type_de_champ with correct initial position

This commit is contained in:
Paul Chavard 2020-07-22 11:09:51 +02:00
parent 3193784c24
commit 69ab2e44fb
3 changed files with 25 additions and 6 deletions

View file

@ -14,12 +14,18 @@ class ProcedureRevision < ApplicationRecord
find_or_clone_type_de_champ(params.delete(:parent_id))
.types_de_champ
.tap do |types_de_champ|
params[:order_place] = types_de_champ.size
params[:order_place] = types_de_champ.present? ? types_de_champ.last.order_place + 1 : 0
end.create(params)
elsif params[:private]
types_de_champ_private.create(params)
types_de_champ_private.tap do |types_de_champ|
# FIXUP: needed during transition to revisions
params[:order_place] = types_de_champ.present? ? types_de_champ.last.order_place + 1 : 0
end.create(params)
else
types_de_champ.create(params)
types_de_champ.tap do |types_de_champ|
# FIXUP: needed during transition to revisions
params[:order_place] = types_de_champ.present? ? types_de_champ.last.order_place + 1 : 0
end.create(params)
end
end

View file

@ -16,9 +16,17 @@ class ProcedureRevisionTypeDeChamp < ApplicationRecord
def set_position
self.position ||= if private?
revision.types_de_champ_private.size
if revision.types_de_champ_private.present?
revision.revision_types_de_champ_private.filter(&:persisted?).last.position + 1
else
0
end
else
revision.types_de_champ.size
if revision.types_de_champ.present?
revision.revision_types_de_champ.filter(&:persisted?).last.position + 1
else
0
end
end
end
end

View file

@ -16,13 +16,18 @@ describe ProcedureRevision do
describe '#add_type_de_champ' do
it 'type_de_champ' do
expect(revision.types_de_champ.size).to eq(2)
revision.add_type_de_champ({
new_type_de_champ = revision.add_type_de_champ({
type_champ: TypeDeChamp.type_champs.fetch(:text),
libelle: "Un champ text"
})
procedure.reload
expect(revision.types_de_champ.size).to eq(3)
expect(procedure.types_de_champ.size).to eq(3)
expect(procedure.types_de_champ.last).to eq(new_type_de_champ)
expect(revision.types_de_champ.last).to eq(new_type_de_champ)
expect(revision.revision_types_de_champ.last.position).to eq(2)
expect(revision.revision_types_de_champ.last.type_de_champ).to eq(new_type_de_champ)
end
it 'type_de_champ_private' do