refactor(dossier): clone only current revision champs
This commit is contained in:
parent
deb19177f7
commit
8687d73a73
2 changed files with 19 additions and 17 deletions
|
@ -42,11 +42,10 @@ module DossierCloneConcern
|
||||||
end
|
end
|
||||||
|
|
||||||
def make_diff(editing_fork)
|
def make_diff(editing_fork)
|
||||||
# TODO: remove champs_public_all usage
|
origin_champs_index = champs_for_revision(:public).index_by(&:stable_id_with_row)
|
||||||
origin_champs_index = champs_public_all.index_by(&:stable_id_with_row)
|
forked_champs_index = editing_fork.champs_for_revision(:public).index_by(&:stable_id_with_row)
|
||||||
forked_champs_index = editing_fork.champs_public_all.index_by(&:stable_id_with_row)
|
|
||||||
updated_champs_index = editing_fork
|
updated_champs_index = editing_fork
|
||||||
.champs_public_all
|
.champs_for_revision(:public)
|
||||||
.filter { _1.updated_at > editing_fork.created_at }
|
.filter { _1.updated_at > editing_fork.created_at }
|
||||||
.index_by(&:stable_id_with_row)
|
.index_by(&:stable_id_with_row)
|
||||||
|
|
||||||
|
@ -81,7 +80,7 @@ module DossierCloneConcern
|
||||||
dossier_attributes += [:groupe_instructeur_id] if fork
|
dossier_attributes += [:groupe_instructeur_id] if fork
|
||||||
relationships = [:individual, :etablissement]
|
relationships = [:individual, :etablissement]
|
||||||
|
|
||||||
cloned_champs = champs
|
cloned_champs = champs_for_revision
|
||||||
.index_by(&:id)
|
.index_by(&:id)
|
||||||
.transform_values { [_1, _1.clone(fork)] }
|
.transform_values { [_1, _1.clone(fork)] }
|
||||||
|
|
||||||
|
@ -143,7 +142,7 @@ module DossierCloneConcern
|
||||||
end
|
end
|
||||||
|
|
||||||
def apply_diff(diff)
|
def apply_diff(diff)
|
||||||
champs_index = (champs_public_all + diff[:added]).index_by(&:stable_id_with_row)
|
champs_index = (champs_for_revision(:public) + diff[:added]).index_by(&:stable_id_with_row)
|
||||||
|
|
||||||
diff[:added].each do |champ|
|
diff[:added].each do |champ|
|
||||||
if champ.child?
|
if champ.child?
|
||||||
|
|
|
@ -1,20 +1,23 @@
|
||||||
RSpec.describe DossierCloneConcern do
|
RSpec.describe DossierCloneConcern do
|
||||||
let(:procedure) do
|
let(:procedure) do
|
||||||
create(:procedure, types_de_champ_public: [
|
create(:procedure, types_de_champ_public:, types_de_champ_private:).tap(&:publish!)
|
||||||
|
end
|
||||||
|
let(:types_de_champ_public) do
|
||||||
|
[
|
||||||
{ type: :text, libelle: "Un champ text", stable_id: 99 },
|
{ type: :text, libelle: "Un champ text", stable_id: 99 },
|
||||||
{ type: :text, libelle: "Un autre champ text", stable_id: 991 },
|
{ type: :text, libelle: "Un autre champ text", stable_id: 991 },
|
||||||
{ type: :yes_no, libelle: "Un champ yes no", stable_id: 992 },
|
{ type: :yes_no, libelle: "Un champ yes no", stable_id: 992 },
|
||||||
{ type: :repetition, libelle: "Un champ répétable", stable_id: 993, mandatory: true, children: [{ type: :text, libelle: 'Nom', stable_id: 994 }] }
|
{ type: :repetition, libelle: "Un champ répétable", stable_id: 993, mandatory: true, children: [{ type: :text, libelle: 'Nom', stable_id: 994 }] }
|
||||||
])
|
]
|
||||||
end
|
end
|
||||||
|
let(:types_de_champ_private) { [] }
|
||||||
let(:dossier) { create(:dossier, :en_construction, procedure:) }
|
let(:dossier) { create(:dossier, :en_construction, procedure:) }
|
||||||
let(:forked_dossier) { dossier.find_or_create_editing_fork(dossier.user) }
|
let(:forked_dossier) { dossier.find_or_create_editing_fork(dossier.user) }
|
||||||
|
|
||||||
before { procedure.publish! }
|
|
||||||
|
|
||||||
describe '#clone' do
|
describe '#clone' do
|
||||||
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private) }
|
let(:dossier) { create(:dossier, :en_construction, :with_populated_champs, procedure:) }
|
||||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
let(:types_de_champ_public) { [{}] }
|
||||||
|
let(:types_de_champ_private) { [{}] }
|
||||||
let(:fork) { false }
|
let(:fork) { false }
|
||||||
let(:new_dossier) { dossier.clone(fork:) }
|
let(:new_dossier) { dossier.clone(fork:) }
|
||||||
|
|
||||||
|
@ -137,9 +140,9 @@ RSpec.describe DossierCloneConcern do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'for Champs::PieceJustificative, original_champ.piece_justificative_file is duped' do
|
context 'for Champs::PieceJustificative, original_champ.piece_justificative_file is duped' do
|
||||||
let(:dossier) { create(:dossier) }
|
let(:types_de_champ_public) { [{ type: :piece_justificative }] }
|
||||||
let(:champ_piece_justificative) { create(:champ_piece_justificative, dossier_id: dossier.id) }
|
let(:champ_piece_justificative) { dossier.champs_public.first }
|
||||||
before { dossier.champs_public << champ_piece_justificative }
|
|
||||||
it { expect(Champs::PieceJustificativeChamp.where(dossier: new_dossier).first.piece_justificative_file.first.blob).to eq(champ_piece_justificative.piece_justificative_file.first.blob) }
|
it { expect(Champs::PieceJustificativeChamp.where(dossier: new_dossier).first.piece_justificative_file.first.blob).to eq(champ_piece_justificative.piece_justificative_file.first.blob) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -181,8 +184,8 @@ RSpec.describe DossierCloneConcern do
|
||||||
it { expect(new_dossier.champs_public[0].updated_at).to eq(dossier.champs_public[0].updated_at) }
|
it { expect(new_dossier.champs_public[0].updated_at).to eq(dossier.champs_public[0].updated_at) }
|
||||||
|
|
||||||
context "piece justificative champ" do
|
context "piece justificative champ" do
|
||||||
let(:champ_pj) { create(:champ_piece_justificative, dossier_id: dossier.id) }
|
let(:types_de_champ_public) { [{ type: :piece_justificative }] }
|
||||||
before { dossier.champs_public << champ_pj.reload }
|
let(:champ_pj) { dossier.champs_public.first }
|
||||||
|
|
||||||
it {
|
it {
|
||||||
champ_pj_fork = Champs::PieceJustificativeChamp.where(dossier: new_dossier).first
|
champ_pj_fork = Champs::PieceJustificativeChamp.where(dossier: new_dossier).first
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue