Skip to content

Commit

Permalink
Merge tag '2023-06-15-01' into feature/import-2023-06-15
Browse files Browse the repository at this point in the history
  • Loading branch information
maatinito committed Aug 29, 2023
2 parents f946aa4 + 908e531 commit 96ba3ee
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
14 changes: 9 additions & 5 deletions app/models/concerns/dossier_prefillable_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
module DossierPrefillableConcern
extend ActiveSupport::Concern

def prefill!(champs_public_attributes)
return unless champs_public_attributes.any?
def prefill!(champs_attributes)
return unless champs_attributes.any?

attr = { prefilled: true }
attr[:champs_public_all_attributes] = champs_public_attributes.map { |h| h.merge(prefilled: true) }
attributes = { prefilled: true }
attributes[:champs_attributes] = champs_attributes.map { |h| h.merge(prefilled: true) }

assign_attributes(attr)
assign_attributes(attributes)
save(validate: false)
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
6 changes: 0 additions & 6 deletions app/models/dossier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1239,12 +1239,6 @@ def self.purge_discarded
termine_expired_to_delete.find_each(&:purge_discarded)
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?
return true if brouillon? && procedure.declarative?
return true if for_procedure_preview?
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/api/public/v1/dossiers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,6 @@
private

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
2 changes: 1 addition & 1 deletion spec/controllers/users/dossiers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,6 @@
private

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
17 changes: 13 additions & 4 deletions spec/models/concern/dossier_prefillable_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

RSpec.describe DossierPrefillableConcern 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(:types_de_champ_public) { [] }
let(:types_de_champ_private) { [] }

subject(:fill) do
dossier.prefill!(values)
Expand All @@ -17,7 +18,7 @@
end
end

context 'when champs_public_attributes is empty' do
context 'when champs_attributes is empty' do
let(:values) { [] }

it "doesn't mark the dossier as prefilled" do
Expand All @@ -29,9 +30,11 @@
end
end

context 'when champs_public_attributes has values' do
context 'when champs_attributes has values' do
context 'when the champs are valid' do
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(:value_1) { "any value" }
let(:champ_id_1) { find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).id }
Expand All @@ -40,7 +43,11 @@
let(:value_2) { "33612345678" }
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'

Expand All @@ -51,6 +58,8 @@
expect(dossier.champs_public.first.prefilled).to eq(true)
expect(dossier.champs_public.last.value).to eq(value_2)
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

Expand Down

0 comments on commit 96ba3ee

Please sign in to comment.