Merge pull request #2245 from betagouv/fix_2241_linked_list_in_private_champs
Fix 2241 linked list in private champs
This commit is contained in:
commit
d47d0da496
3 changed files with 29 additions and 16 deletions
|
@ -187,7 +187,7 @@ module NewGestionnaire
|
||||||
|
|
||||||
def champs_private_params
|
def champs_private_params
|
||||||
params.require(:dossier).permit(champs_private_attributes: [
|
params.require(:dossier).permit(champs_private_attributes: [
|
||||||
:id, :piece_justificative_file, :value, value: [],
|
:id, :primary_value, :secondary_value, :piece_justificative_file, :value, value: [],
|
||||||
etablissement_attributes: Champs::SiretChamp::ETABLISSEMENT_ATTRIBUTES
|
etablissement_attributes: Champs::SiretChamp::ETABLISSEMENT_ATTRIBUTES
|
||||||
])
|
])
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,27 +1,28 @@
|
||||||
class Champs::LinkedDropDownListChamp < Champ
|
class Champs::LinkedDropDownListChamp < Champ
|
||||||
attr_reader :primary_value, :secondary_value
|
|
||||||
delegate :primary_options, :secondary_options, to: :type_de_champ
|
delegate :primary_options, :secondary_options, to: :type_de_champ
|
||||||
|
|
||||||
after_initialize :unpack_value
|
def primary_value
|
||||||
|
|
||||||
def unpack_value
|
|
||||||
if value.present?
|
if value.present?
|
||||||
primary, secondary = JSON.parse(value)
|
JSON.parse(value)[0]
|
||||||
else
|
else
|
||||||
primary = secondary = ''
|
''
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def secondary_value
|
||||||
|
if value.present?
|
||||||
|
JSON.parse(value)[1]
|
||||||
|
else
|
||||||
|
''
|
||||||
end
|
end
|
||||||
@primary_value ||= primary
|
|
||||||
@secondary_value ||= secondary
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def primary_value=(value)
|
def primary_value=(value)
|
||||||
@primary_value = value
|
pack_value(value, secondary_value)
|
||||||
pack_value
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def secondary_value=(value)
|
def secondary_value=(value)
|
||||||
@secondary_value = value
|
pack_value(primary_value, value)
|
||||||
pack_value
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def main_value_name
|
def main_value_name
|
||||||
|
@ -46,7 +47,7 @@ class Champs::LinkedDropDownListChamp < Champ
|
||||||
"#{primary_value || ''};#{secondary_value || ''}"
|
"#{primary_value || ''};#{secondary_value || ''}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def pack_value
|
def pack_value(primary, secondary)
|
||||||
self.value = JSON.generate([ primary_value, secondary_value ])
|
self.value = JSON.generate([ primary, secondary ])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -350,12 +350,16 @@ describe NewGestionnaire::DossiersController, type: :controller do
|
||||||
create(:type_de_champ_multiple_drop_down_list, :private, libelle: 'libelle').champ.create
|
create(:type_de_champ_multiple_drop_down_list, :private, libelle: 'libelle').champ.create
|
||||||
end
|
end
|
||||||
|
|
||||||
|
let(:champ_linked_drop_down_list) do
|
||||||
|
create(:type_de_champ_linked_drop_down_list, :private, libelle: 'libelle').champ.create
|
||||||
|
end
|
||||||
|
|
||||||
let(:champ_datetime) do
|
let(:champ_datetime) do
|
||||||
create(:type_de_champ_datetime, :private, libelle: 'libelle').champ.create
|
create(:type_de_champ_datetime, :private, libelle: 'libelle').champ.create
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:dossier) do
|
let(:dossier) do
|
||||||
create(:dossier, :en_construction, procedure: procedure, champs_private: [champ_multiple_drop_down_list, champ_datetime])
|
create(:dossier, :en_construction, procedure: procedure, champs_private: [champ_multiple_drop_down_list, champ_linked_drop_down_list, champ_datetime])
|
||||||
end
|
end
|
||||||
|
|
||||||
before do
|
before do
|
||||||
|
@ -375,16 +379,24 @@ describe NewGestionnaire::DossiersController, type: :controller do
|
||||||
'value(3i)': 21,
|
'value(3i)': 21,
|
||||||
'value(4i)': 13,
|
'value(4i)': 13,
|
||||||
'value(5i)': 17
|
'value(5i)': 17
|
||||||
|
},
|
||||||
|
'2': {
|
||||||
|
id: champ_linked_drop_down_list.id,
|
||||||
|
primary_value: 'primary',
|
||||||
|
secondary_value: 'secondary'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
champ_multiple_drop_down_list.reload
|
champ_multiple_drop_down_list.reload
|
||||||
|
champ_linked_drop_down_list.reload
|
||||||
champ_datetime.reload
|
champ_datetime.reload
|
||||||
end
|
end
|
||||||
|
|
||||||
it { expect(champ_multiple_drop_down_list.value).to eq('["un", "deux"]') }
|
it { expect(champ_multiple_drop_down_list.value).to eq('["un", "deux"]') }
|
||||||
|
it { expect(champ_linked_drop_down_list.primary_value).to eq('primary') }
|
||||||
|
it { expect(champ_linked_drop_down_list.secondary_value).to eq('secondary') }
|
||||||
it { expect(champ_datetime.value).to eq('21/12/2019 13:17') }
|
it { expect(champ_datetime.value).to eq('21/12/2019 13:17') }
|
||||||
it { expect(response).to redirect_to(annotations_privees_gestionnaire_dossier_path(dossier.procedure, dossier)) }
|
it { expect(response).to redirect_to(annotations_privees_gestionnaire_dossier_path(dossier.procedure, dossier)) }
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue