Merge pull request #9197 from tchak/fix-prefill-annotations
ETQ Administrateur, je voudrais proposer de pré-remplir les annotations privées
This commit is contained in:
commit
908e531057
5 changed files with 24 additions and 17 deletions
|
@ -3,13 +3,17 @@
|
||||||
module DossierPrefillableConcern
|
module DossierPrefillableConcern
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
def prefill!(champs_public_attributes)
|
def prefill!(champs_attributes)
|
||||||
return unless champs_public_attributes.any?
|
return unless champs_attributes.any?
|
||||||
|
|
||||||
attr = { prefilled: true }
|
attributes = { prefilled: true }
|
||||||
attr[:champs_public_all_attributes] = champs_public_attributes.map { |h| h.merge(prefilled: true) }
|
attributes[:champs_attributes] = champs_attributes.map { |h| h.merge(prefilled: true) }
|
||||||
|
|
||||||
assign_attributes(attr)
|
assign_attributes(attributes)
|
||||||
save(validate: false)
|
save(validate: false)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def find_champs_by_stable_ids(stable_ids)
|
||||||
|
champs.joins(:type_de_champ).where(types_de_champ: { stable_id: stable_ids.compact.uniq })
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1229,12 +1229,6 @@ class Dossier < ApplicationRecord
|
||||||
termine_expired_to_delete.find_each(&:purge_discarded)
|
termine_expired_to_delete.find_each(&:purge_discarded)
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_champs_by_stable_ids(stable_ids)
|
|
||||||
return [] if stable_ids.compact.empty?
|
|
||||||
|
|
||||||
champs.joins(:type_de_champ).where(types_de_champ: { stable_id: stable_ids })
|
|
||||||
end
|
|
||||||
|
|
||||||
def skip_user_notification_email?
|
def skip_user_notification_email?
|
||||||
return true if brouillon? && procedure.declarative?
|
return true if brouillon? && procedure.declarative?
|
||||||
return true if for_procedure_preview?
|
return true if for_procedure_preview?
|
||||||
|
|
|
@ -201,6 +201,6 @@ RSpec.describe API::Public::V1::DossiersController, type: :controller do
|
||||||
private
|
private
|
||||||
|
|
||||||
def find_champ_by_stable_id(dossier, stable_id)
|
def find_champ_by_stable_id(dossier, stable_id)
|
||||||
dossier.champs_public.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id })
|
dossier.champs.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1319,6 +1319,6 @@ describe Users::DossiersController, type: :controller do
|
||||||
private
|
private
|
||||||
|
|
||||||
def find_champ_by_stable_id(dossier, stable_id)
|
def find_champ_by_stable_id(dossier, stable_id)
|
||||||
dossier.champs_public.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id })
|
dossier.champs.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,9 +2,10 @@
|
||||||
|
|
||||||
RSpec.describe DossierPrefillableConcern do
|
RSpec.describe DossierPrefillableConcern do
|
||||||
describe '.prefill!' do
|
describe '.prefill!' do
|
||||||
let(:procedure) { create(:procedure, :published, types_de_champ_public:) }
|
let(:procedure) { create(:procedure, :published, types_de_champ_public:, types_de_champ_private:) }
|
||||||
let(:dossier) { create(:dossier, :brouillon, procedure: procedure) }
|
let(:dossier) { create(:dossier, :brouillon, procedure: procedure) }
|
||||||
let(:types_de_champ_public) { [] }
|
let(:types_de_champ_public) { [] }
|
||||||
|
let(:types_de_champ_private) { [] }
|
||||||
|
|
||||||
subject(:fill) do
|
subject(:fill) do
|
||||||
dossier.prefill!(values)
|
dossier.prefill!(values)
|
||||||
|
@ -17,7 +18,7 @@ RSpec.describe DossierPrefillableConcern do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when champs_public_attributes is empty' do
|
context 'when champs_attributes is empty' do
|
||||||
let(:values) { [] }
|
let(:values) { [] }
|
||||||
|
|
||||||
it "doesn't mark the dossier as prefilled" do
|
it "doesn't mark the dossier as prefilled" do
|
||||||
|
@ -29,9 +30,11 @@ RSpec.describe DossierPrefillableConcern do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when champs_public_attributes has values' do
|
context 'when champs_attributes has values' do
|
||||||
context 'when the champs are valid' do
|
context 'when the champs are valid' do
|
||||||
let(:types_de_champ_public) { [{ type: :text }, { type: :phone }] }
|
let(:types_de_champ_public) { [{ type: :text }, { type: :phone }] }
|
||||||
|
let(:types_de_champ_private) { [{ type: :text }] }
|
||||||
|
|
||||||
let(:type_de_champ_1) { procedure.published_revision.types_de_champ_public.first }
|
let(:type_de_champ_1) { procedure.published_revision.types_de_champ_public.first }
|
||||||
let(:value_1) { "any value" }
|
let(:value_1) { "any value" }
|
||||||
let(:champ_id_1) { find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).id }
|
let(:champ_id_1) { find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).id }
|
||||||
|
@ -40,7 +43,11 @@ RSpec.describe DossierPrefillableConcern do
|
||||||
let(:value_2) { "33612345678" }
|
let(:value_2) { "33612345678" }
|
||||||
let(:champ_id_2) { find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).id }
|
let(:champ_id_2) { find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).id }
|
||||||
|
|
||||||
let(:values) { [{ id: champ_id_1, value: value_1 }, { id: champ_id_2, value: value_2 }] }
|
let(:type_de_champ_3) { procedure.published_revision.types_de_champ_private.first }
|
||||||
|
let(:value_3) { "some value" }
|
||||||
|
let(:champ_id_3) { find_champ_by_stable_id(dossier, type_de_champ_3.stable_id).id }
|
||||||
|
|
||||||
|
let(:values) { [{ id: champ_id_1, value: value_1 }, { id: champ_id_2, value: value_2 }, { id: champ_id_3, value: value_3 }] }
|
||||||
|
|
||||||
it_behaves_like 'a dossier marked as prefilled'
|
it_behaves_like 'a dossier marked as prefilled'
|
||||||
|
|
||||||
|
@ -51,6 +58,8 @@ RSpec.describe DossierPrefillableConcern do
|
||||||
expect(dossier.champs_public.first.prefilled).to eq(true)
|
expect(dossier.champs_public.first.prefilled).to eq(true)
|
||||||
expect(dossier.champs_public.last.value).to eq(value_2)
|
expect(dossier.champs_public.last.value).to eq(value_2)
|
||||||
expect(dossier.champs_public.last.prefilled).to eq(true)
|
expect(dossier.champs_public.last.prefilled).to eq(true)
|
||||||
|
expect(dossier.champs_private.first.value).to eq(value_3)
|
||||||
|
expect(dossier.champs_private.first.prefilled).to eq(true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue