commit
7f5876eb0d
8 changed files with 108 additions and 75 deletions
|
@ -39,15 +39,36 @@ class ProcedureRevision < ApplicationRecord
|
|||
end
|
||||
|
||||
def add_type_de_champ(params)
|
||||
if params[:parent_id]
|
||||
find_or_clone_type_de_champ(params.delete(:parent_id))
|
||||
.types_de_champ
|
||||
.tap do |types_de_champ|
|
||||
params[:order_place] = types_de_champ.present? ? types_de_champ.last.order_place + 1 : 0
|
||||
end.create(params).migrate_parent!
|
||||
parent_stable_id = params[:parent_id]
|
||||
|
||||
coordinate = {}
|
||||
|
||||
if parent_stable_id.present?
|
||||
clone_parent_to_draft_revision(parent_stable_id)
|
||||
|
||||
parent_coordinate, parent = coordinate_and_tdc(parent_stable_id)
|
||||
|
||||
coordinate[:parent_id] = parent_coordinate.id
|
||||
coordinate[:position] = children_of(parent).count
|
||||
|
||||
# old system
|
||||
params[:order_place] = coordinate[:position]
|
||||
params[:parent_id] = parent.id
|
||||
elsif params[:private]
|
||||
coordinate[:position] = revision_types_de_champ_private.count
|
||||
else
|
||||
types_de_champ.create(params)
|
||||
coordinate[:position] = revision_types_de_champ_public.count
|
||||
end
|
||||
|
||||
tdc = TypeDeChamp.new(params)
|
||||
if tdc.save
|
||||
coordinate[:type_de_champ] = tdc
|
||||
revision_types_de_champ.create!(coordinate)
|
||||
end
|
||||
|
||||
tdc
|
||||
rescue => e
|
||||
TypeDeChamp.new.tap { |tdc| tdc.errors.add(:base, e.message) }
|
||||
end
|
||||
|
||||
def find_or_clone_type_de_champ(stable_id)
|
||||
|
@ -66,9 +87,7 @@ class ProcedureRevision < ApplicationRecord
|
|||
# Ensure that if this is a child, it's parent is cloned to the new revision
|
||||
clone_parent_to_draft_revision(stable_id)
|
||||
|
||||
coordinate = revision_types_de_champ
|
||||
.joins(:type_de_champ)
|
||||
.find_by(type_de_champ: { stable_id: stable_id })
|
||||
coordinate, _ = coordinate_and_tdc(stable_id)
|
||||
|
||||
siblings = coordinate.siblings.to_a
|
||||
|
||||
|
@ -81,11 +100,7 @@ class ProcedureRevision < ApplicationRecord
|
|||
# Ensure that if this is a child, it's parent is cloned to the new revision
|
||||
clone_parent_to_draft_revision(stable_id)
|
||||
|
||||
coordinate = revision_types_de_champ
|
||||
.joins(:type_de_champ)
|
||||
.find_by(type_de_champ: { stable_id: stable_id })
|
||||
|
||||
tdc = coordinate.type_de_champ
|
||||
coordinate, tdc = coordinate_and_tdc(stable_id)
|
||||
|
||||
coordinate.destroy
|
||||
|
||||
|
@ -149,6 +164,14 @@ class ProcedureRevision < ApplicationRecord
|
|||
|
||||
private
|
||||
|
||||
def coordinate_and_tdc(stable_id)
|
||||
coordinate = revision_types_de_champ
|
||||
.joins(:type_de_champ)
|
||||
.find_by(type_de_champ: { stable_id: stable_id })
|
||||
|
||||
[coordinate, coordinate.type_de_champ]
|
||||
end
|
||||
|
||||
def reorder(siblings)
|
||||
siblings.to_a.compact.each.with_index do |sibling, position|
|
||||
sibling.update(position: position)
|
||||
|
|
|
@ -22,8 +22,6 @@ class ProcedureRevisionTypeDeChamp < ApplicationRecord
|
|||
scope :public_only, -> { joins(:type_de_champ).where(types_de_champ: { private: false }) }
|
||||
scope :private_only, -> { joins(:type_de_champ).where(types_de_champ: { private: true }) }
|
||||
|
||||
before_create :set_position
|
||||
|
||||
def private?
|
||||
type_de_champ.private?
|
||||
end
|
||||
|
@ -37,18 +35,4 @@ class ProcedureRevisionTypeDeChamp < ApplicationRecord
|
|||
revision.revision_types_de_champ_public
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_position
|
||||
self.position ||= begin
|
||||
types_de_champ = (private? ? revision.revision_types_de_champ_private : revision.revision_types_de_champ_public).filter(&:persisted?)
|
||||
|
||||
if types_de_champ.present?
|
||||
types_de_champ.last.position + 1
|
||||
else
|
||||
0
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -353,11 +353,12 @@ def add_types_de_champs(types_de_champ, to: nil, scope: :public)
|
|||
revision = to
|
||||
association_name = scope == :private ? :revision_types_de_champ_private : :revision_types_de_champ_public
|
||||
|
||||
types_de_champ.each do |type_de_champ|
|
||||
types_de_champ.each.with_index do |type_de_champ, i|
|
||||
type_de_champ.private = (scope == :private)
|
||||
|
||||
revision.public_send(association_name) << build(:procedure_revision_type_de_champ,
|
||||
revision: revision,
|
||||
position: type_de_champ.order_place,
|
||||
position: i,
|
||||
type_de_champ: type_de_champ)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -17,16 +17,13 @@ FactoryBot.define do
|
|||
if evaluator.procedure
|
||||
revision = evaluator.procedure.active_revision
|
||||
|
||||
build(:procedure_revision_type_de_champ,
|
||||
position: evaluator.position,
|
||||
evaluator.procedure.save
|
||||
|
||||
create(:procedure_revision_type_de_champ,
|
||||
position: evaluator.position || 0,
|
||||
revision: revision,
|
||||
type_de_champ: type_de_champ)
|
||||
|
||||
if type_de_champ.private?
|
||||
revision.types_de_champ_private << type_de_champ
|
||||
else
|
||||
revision.types_de_champ_public << type_de_champ
|
||||
end
|
||||
elsif evaluator.parent
|
||||
type_de_champ.order_place = evaluator.position || evaluator.parent.types_de_champ.size
|
||||
evaluator.parent.types_de_champ << type_de_champ
|
||||
|
|
|
@ -28,7 +28,10 @@ describe Dossier do
|
|||
end
|
||||
|
||||
describe 'with_champs' do
|
||||
let(:procedure) { create(:procedure, types_de_champ: [build(:type_de_champ, libelle: 'l1', position: 1), build(:type_de_champ, libelle: 'l3', position: 3), build(:type_de_champ, libelle: 'l2', position: 2)]) }
|
||||
let(:procedure) { create(:procedure) }
|
||||
let!(:tdc_1) { create(:type_de_champ, libelle: 'l1', position: 1, procedure: procedure) }
|
||||
let!(:tdc_3) { create(:type_de_champ, libelle: 'l3', position: 3, procedure: procedure) }
|
||||
let!(:tdc_2) { create(:type_de_champ, libelle: 'l2', position: 2, procedure: procedure) }
|
||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||
|
||||
it do
|
||||
|
@ -392,14 +395,20 @@ describe Dossier do
|
|||
end
|
||||
|
||||
describe '#champs' do
|
||||
let(:procedure) { create(:procedure, types_de_champ: [build(:type_de_champ, :private, libelle: 'l1', position: 1), build(:type_de_champ, :private, libelle: 'l3', position: 3), build(:type_de_champ, :private, libelle: 'l2', position: 2)]) }
|
||||
let(:procedure) { create(:procedure) }
|
||||
let!(:tdc_1) { create(:type_de_champ, libelle: 'l1', position: 1, procedure: procedure) }
|
||||
let!(:tdc_3) { create(:type_de_champ, libelle: 'l3', position: 3, procedure: procedure) }
|
||||
let!(:tdc_2) { create(:type_de_champ, libelle: 'l2', position: 2, procedure: procedure) }
|
||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||
|
||||
it { expect(dossier.champs.pluck(:libelle)).to match(['l1', 'l2', 'l3']) }
|
||||
end
|
||||
|
||||
describe '#champs_private' do
|
||||
let(:procedure) { create(:procedure, types_de_champ_private: [build(:type_de_champ, :private, libelle: 'l1', position: 1), build(:type_de_champ, :private, libelle: 'l3', position: 3), build(:type_de_champ, :private, libelle: 'l2', position: 2)]) }
|
||||
let(:procedure) { create(:procedure) }
|
||||
let!(:tdc_1) { create(:type_de_champ, :private, libelle: 'l1', position: 1, procedure: procedure) }
|
||||
let!(:tdc_3) { create(:type_de_champ, :private, libelle: 'l3', position: 3, procedure: procedure) }
|
||||
let!(:tdc_2) { create(:type_de_champ, :private, libelle: 'l2', position: 2, procedure: procedure) }
|
||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
||||
|
||||
it { expect(dossier.champs_private.pluck(:libelle)).to match(['l1', 'l2', 'l3']) }
|
||||
|
|
|
@ -9,40 +9,54 @@ describe ProcedureRevision do
|
|||
end
|
||||
|
||||
describe '#add_type_de_champ' do
|
||||
# tdc: public: text, repetition ; private: text ; +1 text child of repetition
|
||||
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, :with_repetition) }
|
||||
let(:text_params) { { type_champ: :text, libelle: 'text' } }
|
||||
let(:tdc_params) { text_params }
|
||||
let(:last_coordinate) { draft.revision_types_de_champ.last }
|
||||
|
||||
it 'type_de_champ' do
|
||||
expect(draft.types_de_champ_public.size).to eq(2)
|
||||
new_type_de_champ = draft.add_type_de_champ({
|
||||
type_champ: TypeDeChamp.type_champs.fetch(:text),
|
||||
libelle: "Un champ text"
|
||||
})
|
||||
draft.reload
|
||||
expect(draft.types_de_champ_public.size).to eq(3)
|
||||
expect(draft.types_de_champ_public.last).to eq(new_type_de_champ)
|
||||
expect(draft.revision_types_de_champ_public.last.position).to eq(2)
|
||||
expect(draft.revision_types_de_champ_public.last.type_de_champ).to eq(new_type_de_champ)
|
||||
subject { draft.add_type_de_champ(tdc_params) }
|
||||
|
||||
context 'with a text tdc' do
|
||||
it 'public' do
|
||||
expect { subject }.to change { draft.types_de_champ_public.size }.from(2).to(3)
|
||||
expect(draft.types_de_champ_public.last).to eq(subject)
|
||||
|
||||
expect(last_coordinate.position).to eq(2)
|
||||
expect(last_coordinate.type_de_champ).to eq(subject)
|
||||
end
|
||||
end
|
||||
|
||||
it 'type_de_champ_private' do
|
||||
expect(draft.types_de_champ_private.size).to eq(1)
|
||||
draft.add_type_de_champ({
|
||||
type_champ: TypeDeChamp.type_champs.fetch(:text),
|
||||
libelle: "Un champ text",
|
||||
private: true
|
||||
})
|
||||
draft.reload
|
||||
expect(draft.types_de_champ_private.size).to eq(2)
|
||||
context 'with a private tdc' do
|
||||
let(:tdc_params) { text_params.merge(private: true) }
|
||||
|
||||
it { expect { subject }.to change { draft.types_de_champ_private.count }.from(1).to(2) }
|
||||
end
|
||||
|
||||
it 'type_de_champ_repetition' do
|
||||
expect(type_de_champ_repetition.types_de_champ.size).to eq(1)
|
||||
draft.add_type_de_champ({
|
||||
type_champ: TypeDeChamp.type_champs.fetch(:text),
|
||||
libelle: "Un champ text",
|
||||
parent_id: type_de_champ_repetition.stable_id
|
||||
})
|
||||
expect(type_de_champ_repetition.types_de_champ.size).to eq(2)
|
||||
context 'with a repetition child' do
|
||||
let(:tdc_params) { text_params.merge(parent_id: type_de_champ_repetition.stable_id) }
|
||||
|
||||
it do
|
||||
expect { subject }.to change { draft.reload.types_de_champ.count }.from(4).to(5)
|
||||
expect(draft.children_of(type_de_champ_repetition).last).to eq(subject)
|
||||
|
||||
expect(last_coordinate.position).to eq(1)
|
||||
|
||||
parent_coordinate = draft.revision_types_de_champ.find_by(type_de_champ: type_de_champ_repetition)
|
||||
expect(last_coordinate.parent).to eq(parent_coordinate)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when a libelle is missing' do
|
||||
let(:tdc_params) { text_params.except(:libelle) }
|
||||
|
||||
it { expect(subject.errors.full_messages).to eq(["Libelle doit être rempli"]) }
|
||||
end
|
||||
|
||||
context 'when a parent is incorrect' do
|
||||
let(:tdc_params) { text_params.merge(parent_id: 123456789) }
|
||||
|
||||
it { expect(subject.errors.full_messages).not_to be_empty }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -353,7 +353,7 @@ describe ProcedureExportService do
|
|||
let(:other_parent) { create(:type_de_champ_repetition, stable_id: champ_repetition.stable_id) }
|
||||
|
||||
before do
|
||||
create(:procedure_revision_type_de_champ, type_de_champ: other_parent, revision: create(:procedure).active_revision)
|
||||
create(:procedure_revision_type_de_champ, type_de_champ: other_parent, revision: create(:procedure).active_revision, position: 0)
|
||||
create(:type_de_champ, parent: other_parent)
|
||||
end
|
||||
|
||||
|
@ -403,7 +403,7 @@ describe ProcedureExportService do
|
|||
let!(:another_champ_repetition) { create(:champ_repetition, type_de_champ: type_de_champ_repetition, dossier: dossier) }
|
||||
|
||||
it 'should have sheets' do
|
||||
expect(subject.sheets.map(&:name)).to eq(['Dossiers', 'Etablissements', 'Avis', champ_repetition.libelle_for_export, another_champ_repetition.libelle_for_export])
|
||||
expect(subject.sheets.map(&:name)).to eq(['Dossiers', 'Etablissements', 'Avis', another_champ_repetition.libelle_for_export, champ_repetition.libelle_for_export])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -246,12 +246,17 @@ describe 'fetch API Particulier Data', js: true do
|
|||
let(:api_particulier_token) { '29eb50b65f64e8e00c0847a8bbcbd150e1f847' }
|
||||
|
||||
let(:procedure) do
|
||||
create(:procedure, :for_individual, :with_service, :with_cnaf, :with_dgfip, :with_pole_emploi, :with_mesri, :published,
|
||||
create(:procedure, :for_individual, :with_service, :published,
|
||||
libelle: 'libellé de la procédure',
|
||||
path: 'libelle-de-la-procedure',
|
||||
instructeurs: [instructeur],
|
||||
api_particulier_sources: expected_sources,
|
||||
api_particulier_token: api_particulier_token)
|
||||
api_particulier_token: api_particulier_token).tap do |p|
|
||||
p.active_revision.add_type_de_champ(type_champ: :cnaf, libelle: 'cnaf')
|
||||
p.active_revision.add_type_de_champ(type_champ: :dgfip, libelle: 'dgfip')
|
||||
p.active_revision.add_type_de_champ(type_champ: :pole_emploi, libelle: 'pole_emploi')
|
||||
p.active_revision.add_type_de_champ(type_champ: :mesri, libelle: 'mesri')
|
||||
end
|
||||
end
|
||||
|
||||
before { login_as user, scope: :user }
|
||||
|
|
Loading…
Reference in a new issue