Remove dependency type_de_champ -> procedure
This commit is contained in:
parent
d2452980fe
commit
2052bc7840
16 changed files with 87 additions and 81 deletions
|
@ -12,6 +12,6 @@ class PrefillTypeDeChampsController < ApplicationController
|
|||
end
|
||||
|
||||
def set_prefill_type_de_champ
|
||||
@type_de_champ = TypesDeChamp::PrefillTypeDeChamp.build(@procedure.active_revision.types_de_champ.fillable.find(params[:id]))
|
||||
@type_de_champ = TypesDeChamp::PrefillTypeDeChamp.build(@procedure.active_revision.types_de_champ.fillable.find(params[:id]), @procedure.active_revision)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,7 +15,7 @@ class PrefillDescription < SimpleDelegator
|
|||
end
|
||||
|
||||
def types_de_champ
|
||||
TypesDeChamp::PrefillTypeDeChamp.wrap(active_revision.types_de_champ_public.fillable.partition(&:prefillable?).flatten)
|
||||
TypesDeChamp::PrefillTypeDeChamp.wrap(active_revision.types_de_champ_public.fillable.partition(&:prefillable?).flatten, active_revision)
|
||||
end
|
||||
|
||||
def include?(type_de_champ_id)
|
||||
|
@ -40,7 +40,7 @@ class PrefillDescription < SimpleDelegator
|
|||
end
|
||||
|
||||
def prefilled_champs
|
||||
@prefilled_champs ||= TypesDeChamp::PrefillTypeDeChamp.wrap(active_fillable_public_types_de_champ.where(id: selected_type_de_champ_ids))
|
||||
@prefilled_champs ||= TypesDeChamp::PrefillTypeDeChamp.wrap(active_fillable_public_types_de_champ.where(id: selected_type_de_champ_ids), active_revision)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
class PrefillParams
|
||||
attr_reader :dossier, :params
|
||||
|
||||
def initialize(dossier, params)
|
||||
@dossier = dossier
|
||||
@params = params
|
||||
|
@ -11,15 +13,15 @@ class PrefillParams
|
|||
private
|
||||
|
||||
def build_prefill_values
|
||||
value_by_stable_id = @params
|
||||
value_by_stable_id = params
|
||||
.map { |prefixed_typed_id, value| [stable_id_from_typed_id(prefixed_typed_id), value] }
|
||||
.filter { |stable_id, value| stable_id.present? && value.present? }
|
||||
.to_h
|
||||
|
||||
@dossier
|
||||
dossier
|
||||
.find_champs_by_stable_ids(value_by_stable_id.keys)
|
||||
.map { |champ| [champ, value_by_stable_id[champ.stable_id]] }
|
||||
.map { |champ, value| PrefillValue.new(champ:, value:) }
|
||||
.map { |champ, value| PrefillValue.new(champ:, value:, dossier:) }
|
||||
end
|
||||
|
||||
def stable_id_from_typed_id(prefixed_typed_id)
|
||||
|
@ -46,11 +48,12 @@ class PrefillParams
|
|||
TypeDeChamp.type_champs.fetch(:epci)
|
||||
]
|
||||
|
||||
attr_reader :champ, :value
|
||||
attr_reader :champ, :value, :dossier
|
||||
|
||||
def initialize(champ:, value:)
|
||||
def initialize(champ:, value:, dossier:)
|
||||
@champ = champ
|
||||
@value = value
|
||||
@dossier = dossier
|
||||
end
|
||||
|
||||
def prefillable?
|
||||
|
@ -59,7 +62,7 @@ class PrefillParams
|
|||
|
||||
def champ_attributes
|
||||
TypesDeChamp::PrefillTypeDeChamp
|
||||
.build(champ.type_de_champ)
|
||||
.build(champ.type_de_champ, dossier.revision)
|
||||
.to_assignable_attributes(champ, value)
|
||||
end
|
||||
|
||||
|
|
|
@ -32,6 +32,8 @@ class ProcedureRevision < ApplicationRecord
|
|||
|
||||
validate :conditions_are_valid?
|
||||
|
||||
delegate :path, to: :procedure, prefix: true
|
||||
|
||||
def build_champs_public
|
||||
# reload: it can be out of sync in test if some tdcs are added wihtout using add_tdc
|
||||
types_de_champ_public.reload.map(&:build_champ)
|
||||
|
|
|
@ -128,8 +128,6 @@ class TypeDeChamp < ApplicationRecord
|
|||
has_one :procedure, through: :revision
|
||||
|
||||
delegate :estimated_fill_duration, :estimated_read_duration, :tags_for_template, :libelle_for_export, to: :dynamic_type
|
||||
delegate :active_revision, to: :procedure, prefix: true
|
||||
delegate :path, to: :procedure
|
||||
|
||||
class WithIndifferentAccess
|
||||
def self.load(options)
|
||||
|
@ -489,12 +487,6 @@ class TypeDeChamp < ApplicationRecord
|
|||
end
|
||||
end
|
||||
|
||||
def active_revision_type_de_champ
|
||||
procedure_active_revision.revision_types_de_champ_public.find do |rtc|
|
||||
rtc.type_de_champ_id == id
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
DEFAULT_EMPTY = ['']
|
||||
|
|
|
@ -17,7 +17,7 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh
|
|||
return [] unless value.is_a?(Array)
|
||||
|
||||
value.map.with_index do |repetition, index|
|
||||
PrefillRepetitionRow.new(champ, repetition, index).to_assignable_attributes
|
||||
PrefillRepetitionRow.new(champ, repetition, index, @revision).to_assignable_attributes
|
||||
end.reject(&:blank?)
|
||||
end
|
||||
|
||||
|
@ -37,27 +37,28 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh
|
|||
end
|
||||
|
||||
def prefillable_subchamps
|
||||
return [] unless active_revision_type_de_champ
|
||||
|
||||
@prefillable_subchamps ||=
|
||||
TypesDeChamp::PrefillTypeDeChamp.wrap(active_revision_type_de_champ.revision_types_de_champ.map(&:type_de_champ).filter(&:prefillable?))
|
||||
TypesDeChamp::PrefillTypeDeChamp.wrap(@revision.children_of(self).filter(&:prefillable?), @revision)
|
||||
end
|
||||
|
||||
class PrefillRepetitionRow
|
||||
def initialize(champ, repetition, index)
|
||||
attr_reader :champ, :repetition, :index, :revision
|
||||
|
||||
def initialize(champ, repetition, index, revision)
|
||||
@champ = champ
|
||||
@repetition = repetition
|
||||
@index = index
|
||||
@revision = revision
|
||||
end
|
||||
|
||||
def to_assignable_attributes
|
||||
row = @champ.rows[@index] || @champ.add_row(@champ.dossier_revision)
|
||||
row = champ.rows[index] || champ.add_row(champ.dossier_revision)
|
||||
|
||||
JSON.parse(@repetition).map do |key, value|
|
||||
JSON.parse(repetition).map do |key, value|
|
||||
subchamp = row.find { |champ| champ.type_de_champ_to_typed_id == key }
|
||||
next unless subchamp
|
||||
|
||||
TypesDeChamp::PrefillTypeDeChamp.build(subchamp.type_de_champ).to_assignable_attributes(subchamp, value)
|
||||
TypesDeChamp::PrefillTypeDeChamp.build(subchamp.type_de_champ, revision).to_assignable_attributes(subchamp, value)
|
||||
rescue JSON::ParserError # On ignore les valeurs qu'on n'arrive pas à parser
|
||||
end.compact
|
||||
end
|
||||
|
|
|
@ -4,29 +4,34 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator
|
|||
|
||||
POSSIBLE_VALUES_THRESHOLD = 5
|
||||
|
||||
def self.build(type_de_champ)
|
||||
def initialize(type_de_champ, revision)
|
||||
super(type_de_champ)
|
||||
@revision = revision
|
||||
end
|
||||
|
||||
def self.build(type_de_champ, revision)
|
||||
case type_de_champ.type_champ
|
||||
when TypeDeChamp.type_champs.fetch(:drop_down_list)
|
||||
TypesDeChamp::PrefillDropDownListTypeDeChamp.new(type_de_champ)
|
||||
TypesDeChamp::PrefillDropDownListTypeDeChamp.new(type_de_champ, revision)
|
||||
when TypeDeChamp.type_champs.fetch(:multiple_drop_down_list)
|
||||
TypesDeChamp::PrefillMultipleDropDownListTypeDeChamp.new(type_de_champ)
|
||||
TypesDeChamp::PrefillMultipleDropDownListTypeDeChamp.new(type_de_champ, revision)
|
||||
when TypeDeChamp.type_champs.fetch(:pays)
|
||||
TypesDeChamp::PrefillPaysTypeDeChamp.new(type_de_champ)
|
||||
TypesDeChamp::PrefillPaysTypeDeChamp.new(type_de_champ, revision)
|
||||
when TypeDeChamp.type_champs.fetch(:regions)
|
||||
TypesDeChamp::PrefillRegionTypeDeChamp.new(type_de_champ)
|
||||
TypesDeChamp::PrefillRegionTypeDeChamp.new(type_de_champ, revision)
|
||||
when TypeDeChamp.type_champs.fetch(:repetition)
|
||||
TypesDeChamp::PrefillRepetitionTypeDeChamp.new(type_de_champ)
|
||||
TypesDeChamp::PrefillRepetitionTypeDeChamp.new(type_de_champ, revision)
|
||||
when TypeDeChamp.type_champs.fetch(:departements)
|
||||
TypesDeChamp::PrefillDepartementTypeDeChamp.new(type_de_champ)
|
||||
TypesDeChamp::PrefillDepartementTypeDeChamp.new(type_de_champ, revision)
|
||||
when TypeDeChamp.type_champs.fetch(:epci)
|
||||
TypesDeChamp::PrefillEpciTypeDeChamp.new(type_de_champ)
|
||||
TypesDeChamp::PrefillEpciTypeDeChamp.new(type_de_champ, revision)
|
||||
else
|
||||
new(type_de_champ)
|
||||
new(type_de_champ, revision)
|
||||
end
|
||||
end
|
||||
|
||||
def self.wrap(collection)
|
||||
collection.map { |type_de_champ| build(type_de_champ) }
|
||||
def self.wrap(collection, revision)
|
||||
collection.map { |type_de_champ| build(type_de_champ, revision) }
|
||||
end
|
||||
|
||||
def possible_values
|
||||
|
@ -61,7 +66,7 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator
|
|||
|
||||
link_to(
|
||||
I18n.t("views.prefill_descriptions.edit.possible_values.link.text"),
|
||||
Rails.application.routes.url_helpers.prefill_type_de_champ_path(path, self),
|
||||
Rails.application.routes.url_helpers.prefill_type_de_champ_path(revision.procedure_path, self),
|
||||
title: new_tab_suffix(I18n.t("views.prefill_descriptions.edit.possible_values.link.title")),
|
||||
**external_link_attributes
|
||||
)
|
||||
|
|
|
@ -22,7 +22,7 @@ RSpec.describe PrefillDescription, type: :model do
|
|||
|
||||
it { expect(types_de_champ.count).to eq(1) }
|
||||
|
||||
it { expect(types_de_champ.first).to eql(TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ)) }
|
||||
it { expect(types_de_champ.first).to eql(TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ, procedure.active_revision)) }
|
||||
|
||||
shared_examples "filters out non fillable types de champ" do |type_de_champ_name|
|
||||
context "when the procedure has a #{type_de_champ_name} champ" do
|
||||
|
@ -91,7 +91,7 @@ RSpec.describe PrefillDescription, type: :model do
|
|||
let(:type_de_champ_text) { build(:type_de_champ_text, procedure: procedure) }
|
||||
let(:type_de_champ_epci) { build(:type_de_champ_epci, procedure: procedure) }
|
||||
let(:type_de_champ_repetition) { create(:type_de_champ_repetition, :with_types_de_champ, :with_region_types_de_champ, procedure: procedure) }
|
||||
let(:prefillable_subchamps) { TypesDeChamp::PrefillRepetitionTypeDeChamp.new(type_de_champ_repetition).send(:prefillable_subchamps) }
|
||||
let(:prefillable_subchamps) { TypesDeChamp::PrefillRepetitionTypeDeChamp.new(type_de_champ_repetition, procedure.active_revision).send(:prefillable_subchamps) }
|
||||
let(:region_repetition) { prefillable_subchamps.third }
|
||||
let(:prefill_description) { described_class.new(procedure) }
|
||||
|
||||
|
@ -116,9 +116,9 @@ RSpec.describe PrefillDescription, type: :model do
|
|||
expect(prefill_description.prefill_link).to eq(
|
||||
commencer_url(
|
||||
path: procedure.path,
|
||||
"champ_#{type_de_champ_text.to_typed_id}" => TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_text).example_value,
|
||||
"champ_#{type_de_champ_epci.to_typed_id}" => TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_epci).example_value,
|
||||
"champ_#{type_de_champ_repetition.to_typed_id}" => TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_repetition).example_value
|
||||
"champ_#{type_de_champ_text.to_typed_id}" => TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_text, procedure.active_revision).example_value,
|
||||
"champ_#{type_de_champ_epci.to_typed_id}" => TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_epci, procedure.active_revision).example_value,
|
||||
"champ_#{type_de_champ_repetition.to_typed_id}" => TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_repetition, procedure.active_revision).example_value
|
||||
)
|
||||
)
|
||||
end
|
||||
|
@ -129,14 +129,14 @@ RSpec.describe PrefillDescription, type: :model do
|
|||
let(:type_de_champ_text) { create(:type_de_champ_text, procedure: procedure) }
|
||||
let(:type_de_champ_epci) { TypesDeChamp::PrefillTypeDeChamp.build(create(:type_de_champ_epci, procedure: procedure)) }
|
||||
let(:type_de_champ_repetition) { build(:type_de_champ_repetition, :with_types_de_champ, :with_region_types_de_champ, procedure: procedure) }
|
||||
let(:prefillable_subchamps) { TypesDeChamp::PrefillRepetitionTypeDeChamp.new(type_de_champ_repetition).send(:prefillable_subchamps) }
|
||||
let(:prefillable_subchamps) { TypesDeChamp::PrefillRepetitionTypeDeChamp.new(type_de_champ_repetition, procedure.active_revision).send(:prefillable_subchamps) }
|
||||
let(:region_repetition) { prefillable_subchamps.third }
|
||||
let(:prefill_description) { described_class.new(procedure) }
|
||||
let(:expected_query) do
|
||||
<<~TEXT
|
||||
curl --request POST '#{api_public_v1_dossiers_url(procedure)}' \\
|
||||
--header 'Content-Type: application/json' \\
|
||||
--data '{"champ_#{type_de_champ_text.to_typed_id}": "#{TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_text).example_value}", "champ_#{type_de_champ_epci.to_typed_id}": #{TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_epci).example_value}, "champ_#{type_de_champ_repetition.to_typed_id}": #{TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_repetition).example_value}}'
|
||||
--data '{"champ_#{type_de_champ_text.to_typed_id}": "#{TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_text, procedure.active_revision).example_value}", "champ_#{type_de_champ_epci.to_typed_id}": #{TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_epci, procedure.active_revision).example_value}, "champ_#{type_de_champ_repetition.to_typed_id}": #{TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_repetition, procedure.active_revision).example_value}}'
|
||||
TEXT
|
||||
end
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ RSpec.describe TypesDeChamp::PrefillDepartementTypeDeChamp, type: :model do
|
|||
end
|
||||
|
||||
describe 'ancestors' do
|
||||
subject { described_class.build(type_de_champ) }
|
||||
subject { described_class.build(type_de_champ, procedure.active_revision) }
|
||||
|
||||
it { is_expected.to be_kind_of(TypesDeChamp::PrefillTypeDeChamp) }
|
||||
end
|
||||
|
@ -20,7 +20,7 @@ RSpec.describe TypesDeChamp::PrefillDepartementTypeDeChamp, type: :model do
|
|||
let(:expected_values) {
|
||||
"Un <a href=\"https://fr.wikipedia.org/wiki/Num%C3%A9rotation_des_d%C3%A9partements_fran%C3%A7ais\" target=\"_blank\">numéro de département</a><br><a title=\"Toutes les valeurs possibles — Nouvel onglet\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"/procedures/#{procedure.path}/prefill_type_de_champs/#{type_de_champ.id}\">Voir toutes les valeurs possibles</a>"
|
||||
}
|
||||
subject(:possible_values) { described_class.new(type_de_champ).possible_values }
|
||||
subject(:possible_values) { described_class.new(type_de_champ, procedure.active_revision).possible_values }
|
||||
|
||||
before { type_de_champ.reload }
|
||||
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe TypesDeChamp::PrefillEpciTypeDeChamp do
|
||||
let(:type_de_champ) { build(:type_de_champ_epci) }
|
||||
let(:procedure) { create(:procedure) }
|
||||
let(:type_de_champ) { build(:type_de_champ_epci, procedure: procedure) }
|
||||
let(:champ) { create(:champ_epci, type_de_champ: type_de_champ) }
|
||||
let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }
|
||||
|
||||
|
@ -11,7 +12,7 @@ RSpec.describe TypesDeChamp::PrefillEpciTypeDeChamp do
|
|||
end
|
||||
|
||||
describe 'ancestors' do
|
||||
subject { described_class.new(type_de_champ) }
|
||||
subject { described_class.new(type_de_champ, procedure.active_revision) }
|
||||
|
||||
it { is_expected.to be_kind_of(TypesDeChamp::PrefillEpciTypeDeChamp) }
|
||||
end
|
||||
|
@ -20,7 +21,7 @@ RSpec.describe TypesDeChamp::PrefillEpciTypeDeChamp do
|
|||
let(:expected_values) do
|
||||
departements.map { |departement| "#{departement[:code]} (#{departement[:name]}) : https://geo.api.gouv.fr/epcis?codeDepartement=#{departement[:code]}" }
|
||||
end
|
||||
subject(:possible_values) { described_class.new(type_de_champ).possible_values }
|
||||
subject(:possible_values) { described_class.new(type_de_champ, procedure.active_revision).possible_values }
|
||||
|
||||
before do
|
||||
VCR.insert_cassette('api_geo_departements')
|
||||
|
@ -38,7 +39,7 @@ RSpec.describe TypesDeChamp::PrefillEpciTypeDeChamp do
|
|||
describe '#example_value' do
|
||||
let(:departement_code) { departements.pick(:code) }
|
||||
let(:epci_code) { APIGeoService.epcis(departement_code).pick(:code) }
|
||||
subject(:example_value) { described_class.new(type_de_champ).example_value }
|
||||
subject(:example_value) { described_class.new(type_de_champ, procedure.active_revision).example_value }
|
||||
|
||||
before do
|
||||
VCR.insert_cassette('api_geo_departements')
|
||||
|
@ -54,7 +55,7 @@ RSpec.describe TypesDeChamp::PrefillEpciTypeDeChamp do
|
|||
end
|
||||
|
||||
describe '#to_assignable_attributes' do
|
||||
subject(:to_assignable_attributes) { described_class.build(type_de_champ).to_assignable_attributes(champ, value) }
|
||||
subject(:to_assignable_attributes) { described_class.build(type_de_champ, procedure.active_revision).to_assignable_attributes(champ, value) }
|
||||
|
||||
context 'when the value is nil' do
|
||||
let(:value) { nil }
|
||||
|
|
|
@ -4,14 +4,14 @@ RSpec.describe TypesDeChamp::PrefillPaysTypeDeChamp, type: :model do
|
|||
let(:type_de_champ) { build(:type_de_champ_pays, procedure: procedure) }
|
||||
|
||||
describe 'ancestors' do
|
||||
subject { described_class.build(type_de_champ) }
|
||||
subject { described_class.build(type_de_champ, procedure.active_revision) }
|
||||
|
||||
it { is_expected.to be_kind_of(TypesDeChamp::PrefillTypeDeChamp) }
|
||||
end
|
||||
|
||||
describe '#possible_values' do
|
||||
let(:expected_values) { "Un <a href=\"https://en.wikipedia.org/wiki/ISO_3166-2\" target=\"_blank\" rel=\"noopener noreferrer\">code pays ISO 3166-2</a><br><a title=\"Toutes les valeurs possibles — Nouvel onglet\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"/procedures/#{procedure.path}/prefill_type_de_champs/#{type_de_champ.id}\">Voir toutes les valeurs possibles</a>" }
|
||||
subject(:possible_values) { described_class.new(type_de_champ).possible_values }
|
||||
subject(:possible_values) { described_class.new(type_de_champ, procedure.active_revision).possible_values }
|
||||
|
||||
before { type_de_champ.reload }
|
||||
|
||||
|
|
|
@ -11,14 +11,14 @@ RSpec.describe TypesDeChamp::PrefillRegionTypeDeChamp, type: :model do
|
|||
end
|
||||
|
||||
describe 'ancestors' do
|
||||
subject { described_class.build(type_de_champ) }
|
||||
subject { described_class.build(type_de_champ, procedure.active_revision) }
|
||||
|
||||
it { is_expected.to be_kind_of(TypesDeChamp::PrefillTypeDeChamp) }
|
||||
end
|
||||
|
||||
describe '#possible_values', vcr: { cassette_name: 'api_geo_regions' } do
|
||||
let(:expected_values) { "Un <a href=\"https://fr.wikipedia.org/wiki/R%C3%A9gion_fran%C3%A7aise\" target=\"_blank\" rel=\"noopener noreferrer\">code INSEE de région</a><br><a title=\"Toutes les valeurs possibles — Nouvel onglet\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"/procedures/#{procedure.path}/prefill_type_de_champs/#{type_de_champ.id}\">Voir toutes les valeurs possibles</a>" }
|
||||
subject(:possible_values) { described_class.new(type_de_champ).possible_values }
|
||||
subject(:possible_values) { described_class.new(type_de_champ, procedure.active_revision).possible_values }
|
||||
|
||||
before { type_de_champ.reload }
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe TypesDeChamp::PrefillRepetitionTypeDeChamp, type: :model, vcr: { cassette_name: 'api_geo_regions' } do
|
||||
let(:procedure) { build(:procedure) }
|
||||
let(:procedure) { create(:procedure) }
|
||||
let(:type_de_champ) { build(:type_de_champ_repetition, :with_types_de_champ, :with_region_types_de_champ, procedure: procedure) }
|
||||
let(:champ) { create(:champ_repetition, type_de_champ: type_de_champ) }
|
||||
let(:prefillable_subchamps) { TypesDeChamp::PrefillRepetitionTypeDeChamp.new(type_de_champ).send(:prefillable_subchamps) }
|
||||
let(:prefillable_subchamps) { TypesDeChamp::PrefillRepetitionTypeDeChamp.new(type_de_champ, procedure.active_revision).send(:prefillable_subchamps) }
|
||||
let(:text_repetition) { prefillable_subchamps.first }
|
||||
let(:integer_repetition) { prefillable_subchamps.second }
|
||||
let(:region_repetition) { prefillable_subchamps.third }
|
||||
|
@ -16,13 +16,13 @@ RSpec.describe TypesDeChamp::PrefillRepetitionTypeDeChamp, type: :model, vcr: {
|
|||
end
|
||||
|
||||
describe 'ancestors' do
|
||||
subject { described_class.build(type_de_champ) }
|
||||
subject { described_class.build(type_de_champ, procedure.active_revision) }
|
||||
|
||||
it { is_expected.to be_kind_of(TypesDeChamp::PrefillTypeDeChamp) }
|
||||
end
|
||||
|
||||
describe '#possible_values' do
|
||||
subject(:possible_values) { described_class.new(type_de_champ).possible_values }
|
||||
subject(:possible_values) { described_class.new(type_de_champ, procedure.active_revision).possible_values }
|
||||
let(:expected_value) {
|
||||
"Un tableau de dictionnaires avec les valeurs possibles pour chaque champ de la répétition.</br><ul><li>#{text_repetition.to_typed_id}: Un texte court<br></li><li>#{integer_repetition.to_typed_id}: Un nombre entier<br></li><li>#{region_repetition.to_typed_id}: Un <a href=\"https://fr.wikipedia.org/wiki/R%C3%A9gion_fran%C3%A7aise\" target=\"_blank\" rel=\"noopener noreferrer\">code INSEE de région</a><br><a title=\"Toutes les valeurs possibles — Nouvel onglet\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"/procedures/#{procedure.path}/prefill_type_de_champs/#{region_repetition.id}\">Voir toutes les valeurs possibles</a></li></ul>"
|
||||
}
|
||||
|
@ -33,14 +33,14 @@ RSpec.describe TypesDeChamp::PrefillRepetitionTypeDeChamp, type: :model, vcr: {
|
|||
end
|
||||
|
||||
describe '#example_value' do
|
||||
subject(:example_value) { described_class.new(type_de_champ).example_value }
|
||||
subject(:example_value) { described_class.new(type_de_champ, procedure.active_revision).example_value }
|
||||
let(:expected_value) { ["{\"#{text_repetition.to_typed_id}\":\"Texte court\", \"#{integer_repetition.to_typed_id}\":\"42\", \"#{region_repetition.to_typed_id}\":\"53\"}", "{\"#{text_repetition.to_typed_id}\":\"Texte court\", \"#{integer_repetition.to_typed_id}\":\"42\", \"#{region_repetition.to_typed_id}\":\"53\"}"] }
|
||||
|
||||
it { expect(example_value).to eq(expected_value) }
|
||||
end
|
||||
|
||||
describe '#to_assignable_attributes' do
|
||||
subject(:to_assignable_attributes) { described_class.build(type_de_champ).to_assignable_attributes(champ, value) }
|
||||
subject(:to_assignable_attributes) { described_class.build(type_de_champ, procedure.active_revision).to_assignable_attributes(champ, value) }
|
||||
|
||||
context 'when the value is nil' do
|
||||
let(:value) { nil }
|
||||
|
|
|
@ -4,60 +4,62 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do
|
|||
include ActionView::Helpers::UrlHelper
|
||||
include ApplicationHelper
|
||||
|
||||
let(:procedure) { create(:procedure) }
|
||||
|
||||
describe '.build' do
|
||||
subject(:built) { described_class.build(type_de_champ) }
|
||||
subject(:built) { described_class.build(type_de_champ, procedure.active_revision) }
|
||||
|
||||
context 'when the type de champ is a drop_down_list' do
|
||||
let(:type_de_champ) { build(:type_de_champ_drop_down_list) }
|
||||
let(:type_de_champ) { build(:type_de_champ_drop_down_list, procedure: procedure) }
|
||||
|
||||
it { expect(built).to be_kind_of(TypesDeChamp::PrefillDropDownListTypeDeChamp) }
|
||||
end
|
||||
|
||||
context 'when the type de champ is a multiple_drop_down_list' do
|
||||
let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list) }
|
||||
let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list, procedure: procedure) }
|
||||
|
||||
it { expect(built).to be_kind_of(TypesDeChamp::PrefillMultipleDropDownListTypeDeChamp) }
|
||||
end
|
||||
|
||||
context 'when the type de champ is a pays' do
|
||||
let(:type_de_champ) { build(:type_de_champ_pays) }
|
||||
let(:type_de_champ) { build(:type_de_champ_pays, procedure: procedure) }
|
||||
|
||||
it { expect(built).to be_kind_of(TypesDeChamp::PrefillPaysTypeDeChamp) }
|
||||
end
|
||||
|
||||
context 'when the type de champ is a regions' do
|
||||
let(:type_de_champ) { build(:type_de_champ_regions) }
|
||||
let(:type_de_champ) { build(:type_de_champ_regions, procedure: procedure) }
|
||||
|
||||
it { expect(built).to be_kind_of(TypesDeChamp::PrefillRegionTypeDeChamp) }
|
||||
end
|
||||
|
||||
context 'when the type de champ is a repetition' do
|
||||
let(:type_de_champ) { build(:type_de_champ_repetition) }
|
||||
let(:type_de_champ) { build(:type_de_champ_repetition, procedure: procedure) }
|
||||
|
||||
it { expect(built).to be_kind_of(TypesDeChamp::PrefillRepetitionTypeDeChamp) }
|
||||
end
|
||||
|
||||
context 'when the type de champ is a departements' do
|
||||
let(:type_de_champ) { build(:type_de_champ_departements) }
|
||||
let(:type_de_champ) { build(:type_de_champ_departements, procedure: procedure) }
|
||||
|
||||
it { expect(built).to be_kind_of(TypesDeChamp::PrefillDepartementTypeDeChamp) }
|
||||
end
|
||||
|
||||
context 'when the type de champ is a epci' do
|
||||
let(:type_de_champ) { build(:type_de_champ_epci) }
|
||||
let(:type_de_champ) { build(:type_de_champ_epci, procedure: procedure) }
|
||||
|
||||
it { expect(built).to be_kind_of(TypesDeChamp::PrefillEpciTypeDeChamp) }
|
||||
end
|
||||
|
||||
context 'when any other type de champ' do
|
||||
let(:type_de_champ) { build(:type_de_champ_date) }
|
||||
let(:type_de_champ) { build(:type_de_champ_date, procedure: procedure) }
|
||||
|
||||
it { expect(built).to be_kind_of(TypesDeChamp::PrefillTypeDeChamp) }
|
||||
end
|
||||
end
|
||||
|
||||
describe '.wrap' do
|
||||
subject(:wrapped) { described_class.wrap([build(:type_de_champ_drop_down_list), build(:type_de_champ_email)]) }
|
||||
subject(:wrapped) { described_class.wrap([build(:type_de_champ_drop_down_list, procedure: procedure), build(:type_de_champ_email, procedure: procedure)], procedure.active_revision) }
|
||||
|
||||
it 'wraps the collection' do
|
||||
expect(wrapped.first).to be_kind_of(TypesDeChamp::PrefillDropDownListTypeDeChamp)
|
||||
|
@ -66,7 +68,7 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do
|
|||
end
|
||||
|
||||
describe '#possible_values' do
|
||||
let(:built) { described_class.build(type_de_champ) }
|
||||
let(:built) { described_class.build(type_de_champ, procedure.active_revision) }
|
||||
subject(:possible_values) { built.possible_values }
|
||||
|
||||
context 'when the type de champ is prefillable' do
|
||||
|
@ -88,7 +90,7 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do
|
|||
let(:link_to_all_possible_values) {
|
||||
link_to(
|
||||
I18n.t("views.prefill_descriptions.edit.possible_values.link.text"),
|
||||
Rails.application.routes.url_helpers.prefill_type_de_champ_path(type_de_champ.path, type_de_champ),
|
||||
Rails.application.routes.url_helpers.prefill_type_de_champ_path(procedure.path, type_de_champ),
|
||||
title: new_tab_suffix(I18n.t("views.prefill_descriptions.edit.possible_values.link.title")),
|
||||
**external_link_attributes
|
||||
)
|
||||
|
@ -113,33 +115,33 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do
|
|||
end
|
||||
|
||||
context 'when the type de champ is not prefillable' do
|
||||
let(:type_de_champ) { build(:type_de_champ_mesri) }
|
||||
let(:type_de_champ) { build(:type_de_champ_mesri, procedure: procedure) }
|
||||
|
||||
it { expect(possible_values).to be_empty }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#example_value' do
|
||||
subject(:example_value) { described_class.build(type_de_champ).example_value }
|
||||
subject(:example_value) { described_class.build(type_de_champ, procedure.active_revision).example_value }
|
||||
|
||||
context 'when the type de champ is not prefillable' do
|
||||
let(:type_de_champ) { build(:type_de_champ_mesri) }
|
||||
let(:type_de_champ) { build(:type_de_champ_mesri, procedure: procedure) }
|
||||
|
||||
it { expect(example_value).to be_nil }
|
||||
end
|
||||
|
||||
context 'when the type de champ is prefillable' do
|
||||
let(:type_de_champ) { build(:type_de_champ_email) }
|
||||
let(:type_de_champ) { build(:type_de_champ_email, procedure: procedure) }
|
||||
|
||||
it { expect(example_value).to eq(I18n.t("views.prefill_descriptions.edit.examples.#{type_de_champ.type_champ}")) }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_assignable_attributes' do
|
||||
let(:type_de_champ) { build(:type_de_champ_email) }
|
||||
let(:type_de_champ) { build(:type_de_champ_email, procedure: procedure) }
|
||||
let(:champ) { build(:champ, type_de_champ: type_de_champ) }
|
||||
let(:value) { "any@email.org" }
|
||||
subject(:to_assignable_attributes) { described_class.build(type_de_champ).to_assignable_attributes(champ, value) }
|
||||
subject(:to_assignable_attributes) { described_class.build(type_de_champ, procedure.active_revision).to_assignable_attributes(champ, value) }
|
||||
|
||||
it { is_expected.to match({ id: champ.id, value: value }) }
|
||||
end
|
||||
|
|
|
@ -22,7 +22,7 @@ describe 'Prefilling a dossier (with a GET request):' do
|
|||
]
|
||||
}
|
||||
let(:epci_value) { ['01', '200029999'] }
|
||||
let(:sub_type_de_champs_repetition) { type_de_champ_repetition.active_revision_type_de_champ.revision_types_de_champ.map(&:type_de_champ) }
|
||||
let(:sub_type_de_champs_repetition) { procedure.active_revision.children_of(type_de_champ_repetition) }
|
||||
let(:text_repetition_libelle) { sub_type_de_champs_repetition.first.libelle }
|
||||
let(:integer_repetition_libelle) { sub_type_de_champs_repetition.second.libelle }
|
||||
let(:text_repetition_value) { "First repetition text" }
|
||||
|
|
|
@ -22,7 +22,7 @@ describe 'Prefilling a dossier (with a POST request):' do
|
|||
]
|
||||
}
|
||||
let(:epci_value) { ['01', '200029999'] }
|
||||
let(:sub_type_de_champs_repetition) { type_de_champ_repetition.active_revision_type_de_champ.revision_types_de_champ.map(&:type_de_champ) }
|
||||
let(:sub_type_de_champs_repetition) { procedure.active_revision.children_of(type_de_champ_repetition) }
|
||||
let(:text_repetition_libelle) { sub_type_de_champs_repetition.first.libelle }
|
||||
let(:integer_repetition_libelle) { sub_type_de_champs_repetition.second.libelle }
|
||||
let(:text_repetition_value) { "First repetition text" }
|
||||
|
|
Loading…
Reference in a new issue