Improve format prefill possible_values (tests missing)

This commit is contained in:
Damien Le Thiec 2023-02-09 18:08:51 +01:00
parent c5f1f80d25
commit a4d707f942
10 changed files with 45 additions and 62 deletions

View file

@ -1,5 +1,5 @@
class TypesDeChamp::PrefillDropDownListTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp
def possible_values
def possible_values_list
if drop_down_other?
drop_down_list_enabled_non_empty_options.insert(
0,
@ -11,6 +11,6 @@ class TypesDeChamp::PrefillDropDownListTypeDeChamp < TypesDeChamp::PrefillTypeDe
end
def example_value
possible_values.first
possible_values_list.first
end
end

View file

@ -1,5 +1,5 @@
class TypesDeChamp::PrefillPaysTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp
def possible_values
def possible_values_list
countries.map { |country| "#{country[:code]} (#{country[:name]})" }
end

View file

@ -1,5 +1,5 @@
class TypesDeChamp::PrefillRegionTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp
def possible_values
def possible_values_list
regions.map { |region| "#{region[:code]} (#{region[:name]})" }
end

View file

@ -3,24 +3,26 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh
include ApplicationHelper
def possible_values
prefillable_subchamps.map do |prefill_type_de_champ|
if prefill_type_de_champ.too_many_possible_values?
link = link_to I18n.t("views.prefill_descriptions.edit.possible_values.link.text"), Rails.application.routes.url_helpers.prefill_type_de_champ_path(prefill_type_de_champ.path, prefill_type_de_champ), title: ActionController::Base.helpers.sanitize(new_tab_suffix(I18n.t("views.prefill_descriptions.edit.possible_values.link.title"))), **external_link_attributes
"#{prefill_type_de_champ.libelle}: #{link}"
else
"#{prefill_type_de_champ.libelle}: #{prefill_type_de_champ.possible_values_sentence}"
end
end
[
I18n.t("views.prefill_descriptions.edit.possible_values.#{type_champ}_html"),
subchamps_possible_values_list
].join("</br>").html_safe # rubocop:disable Rails/OutputSafety
end
def possible_values_sentence
"#{I18n.t("views.prefill_descriptions.edit.possible_values.#{type_champ}_html")}<br>#{possible_values.join("<br>")}".html_safe # rubocop:disable Rails/OutputSafety
def subchamps_possible_values_list
"<ul>" + prefillable_subchamps.map do |prefill_type_de_champ|
"<li>#{prefill_type_de_champ.libelle}: #{prefill_type_de_champ.possible_values}</li>"
end.join + "</ul>"
end
def example_value
[row_values_format, row_values_format].map { |row| row.to_s.gsub("=>", ":") }
end
def too_many_possible_values?
false
end
private
def row_values_format
@ -33,6 +35,8 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh
def prefillable_subchamps
return [] unless active_revision_type_de_champ
TypesDeChamp::PrefillTypeDeChamp.wrap(active_revision_type_de_champ.revision_types_de_champ.map(&:type_de_champ).filter(&:prefillable?))
@prefillable_subchamps ||=
TypesDeChamp::PrefillTypeDeChamp.wrap(active_revision_type_de_champ.revision_types_de_champ.map(&:type_de_champ).filter(&:prefillable?))
end
end

View file

@ -1,5 +1,8 @@
class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator
POSSIBLE_VALUES_THRESHOLD = 5
include ActionView::Helpers::UrlHelper
include ApplicationHelper
POSSIBLE_VALUES_THRESHOLD = 1
def self.build(type_de_champ)
case type_de_champ.type_champ
@ -21,11 +24,21 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator
end
def possible_values
[possible_values_list_display, link_to_all_possible_values].compact.join('<br>').html_safe
end
def possible_values_list
return [] unless prefillable?
[I18n.t("views.prefill_descriptions.edit.possible_values.#{type_champ}_html")]
end
def link_to_all_possible_values
return unless too_many_possible_values?
link_to I18n.t("views.prefill_descriptions.edit.possible_values.link.text"), Rails.application.routes.url_helpers.prefill_type_de_champ_path(path, self), title: new_tab_suffix(I18n.t("views.prefill_descriptions.edit.possible_values.link.title")), **external_link_attributes
end
def example_value
return nil unless prefillable?
@ -33,14 +46,16 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator
end
def too_many_possible_values?
possible_values.count > POSSIBLE_VALUES_THRESHOLD
possible_values_list.count > POSSIBLE_VALUES_THRESHOLD
end
def possible_values_sentence
private
def possible_values_list_display
if too_many_possible_values?
I18n.t("views.prefill_descriptions.edit.possible_values.#{type_champ}_html").html_safe # rubocop:disable Rails/OutputSafety
else
possible_values.to_sentence
possible_values_list.to_sentence
end
end
end

View file

@ -39,10 +39,7 @@
%th
= t("views.prefill_descriptions.edit.possible_values.title")
%td
= type_de_champ.possible_values_sentence
%br
- if type_de_champ.too_many_possible_values?
= link_to t("views.prefill_descriptions.edit.possible_values.link.text"), prefill_type_de_champ_path(prefill_description.path, type_de_champ), title: new_tab_suffix(t("views.prefill_descriptions.edit.possible_values.link.title")), **external_link_attributes
= type_de_champ.possible_values
%tr{ class: prefillable ? "" : "fr-text-mention--grey" }
%th
= t("views.prefill_descriptions.edit.examples.title")

View file

@ -26,7 +26,7 @@
= t("views.prefill_descriptions.edit.possible_values.title")
%td
.fr-grid-row.fr-grid-row--gutters.fr-py-5w
- @type_de_champ.possible_values.each do |possible_value|
- @type_de_champ.possible_values_list.each do |possible_value|
.fr-col-lg-3.fr-col-md-4.fr-col-sm-6.fr-col-12
= possible_value
%tr

View file

@ -123,7 +123,7 @@ fr:
datetime_html: Datetime au format ISO8601
date_html: Date au format ISO8601
drop_down_list_other_html: Toute valeur
repetition_html: Un array de hash avec les valeurs possibles pour chaque champ de la répétition.
repetition_html: Un tableau de dictionnaires avec les valeurs possibles pour chaque champ de la répétition.
examples:
title: Exemple
text: Texte court

View file

@ -34,7 +34,7 @@ RSpec.describe TypesDeChamp::PrefillRepetitionTypeDeChamp, type: :model, vcr: {
describe '#possible_values_sentence' do
subject(:possible_values_sentence) { described_class.new(type_de_champ).possible_values_sentence }
let(:expected_value) { "Un array de hash avec les valeurs possibles pour chaque champ de la répétition.<br>sub type de champ: Un texte court<br>sub type de champ2: Un nombre entier<br>region sub_champ: <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>" }
let(:expected_value) { "Un tableau de dictionnaires avec les valeurs possibles pour chaque champ de la répétition.<br>sub type de champ: Un texte court<br>sub type de champ2: Un nombre entier<br>region sub_champ: <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>" }
it { expect(possible_values_sentence).to eq(expected_value) }
end

View file

@ -53,33 +53,17 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do
it { expect(possible_values).to be_empty }
end
context 'when the type de champ is prefillable' do
let(:type_de_champ) { build(:type_de_champ_email) }
it { expect(possible_values).to match(["Une adresse email"]) }
end
end
describe '#possible_values_sentence' do
subject(:possible_values_sentence) { described_class.build(type_de_champ).possible_values_sentence }
context 'when the type de champ is not prefillable' do
let(:type_de_champ) { build(:type_de_champ_mesri) }
it { expect(possible_values_sentence).to be_empty }
end
context 'when there is too many possible values' do
let(:type_de_champ) { build(:type_de_champ_drop_down_list) }
let(:type_de_champ) { create(:type_de_champ_drop_down_list) }
before { type_de_champ.drop_down_options = (1..described_class::POSSIBLE_VALUES_THRESHOLD + 1).map(&:to_s) }
it { expect(possible_values_sentence).to match("Un choix parmi ceux sélectionnés à la création de la procédure") }
it { expect(possible_values).to match("Un choix parmi ceux sélectionnés à la création de la procédure") }
end
context 'when the type de champ is prefillable' do
let(:type_de_champ) { build(:type_de_champ_email) }
it { expect(possible_values_sentence).to match("Une adresse email") }
it { expect(possible_values).to match("Une adresse email") }
end
end
@ -98,21 +82,4 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do
it { expect(example_value).to eq(I18n.t("views.prefill_descriptions.edit.examples.#{type_de_champ.type_champ}")) }
end
end
describe '#too_many_possible_values?' do
let(:type_de_champ) { build(:type_de_champ_drop_down_list) }
subject(:too_many_possible_values) { described_class.build(type_de_champ).too_many_possible_values? }
context 'when there are too many possible values' do
before { type_de_champ.drop_down_options = (1..described_class::POSSIBLE_VALUES_THRESHOLD + 1).map(&:to_s) }
it { expect(too_many_possible_values).to eq(true) }
end
context 'when there are not too many possible values' do
before { type_de_champ.drop_down_options = (1..described_class::POSSIBLE_VALUES_THRESHOLD).map(&:to_s) }
it { expect(too_many_possible_values).to eq(false) }
end
end
end