hide values and examples for unprefillable champs (#8309)

* hide values and examples for unprefillable champs

*  put prefillable champs first
This commit is contained in:
Sébastien Carceles 2022-12-20 14:37:25 +01:00 committed by GitHub
parent 32b5daeb9a
commit e4a7a4c391
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 8 deletions

View file

@ -15,7 +15,7 @@ class PrefillDescription < SimpleDelegator
end end
def types_de_champ def types_de_champ
active_revision.types_de_champ_public.fillable active_revision.types_de_champ_public.fillable.partition(&:prefillable?).flatten
end end
def include?(type_de_champ_id) def include?(type_de_champ_id)
@ -31,7 +31,7 @@ class PrefillDescription < SimpleDelegator
end end
def prefilled_champs def prefilled_champs
@prefilled_champs ||= types_de_champ.where(id: selected_type_de_champ_ids) @prefilled_champs ||= active_fillable_public_types_de_champ.where(id: selected_type_de_champ_ids)
end end
private private
@ -47,4 +47,8 @@ class PrefillDescription < SimpleDelegator
def example_value(type_de_champ) def example_value(type_de_champ)
I18n.t("views.prefill_descriptions.edit.examples.#{type_de_champ.type_champ}") I18n.t("views.prefill_descriptions.edit.examples.#{type_de_champ.type_champ}")
end end
def active_fillable_public_types_de_champ
active_revision.types_de_champ_public.fillable
end
end end

View file

@ -1,6 +1,8 @@
= turbo_frame_tag "#{dom_id(@prefill_description)}_types_de_champs" do = turbo_frame_tag "#{dom_id(@prefill_description)}_types_de_champs" do
.fr-grid-row.fr-grid-row--gutters.fr-py-5w .fr-grid-row.fr-grid-row--gutters.fr-py-5w
- prefill_description.types_de_champ.each do |type_de_champ| - prefill_description.types_de_champ.each do |type_de_champ|
- prefillable = type_de_champ.prefillable?
.fr-col-md-6.fr-col-12 .fr-col-md-6.fr-col-12
.card .card
.card-title.flex.justify-between.align-center .card-title.flex.justify-between.align-center
@ -10,7 +12,7 @@
- (prefill_description.selected_type_de_champ_ids - [type_de_champ.id.to_s]).each do |id| - (prefill_description.selected_type_de_champ_ids - [type_de_champ.id.to_s]).each do |id|
= f.hidden_field :selected_type_de_champ_ids, value: id, multiple: true = f.hidden_field :selected_type_de_champ_ids, value: id, multiple: true
= f.submit t("views.prefill_descriptions.edit.champ_remove"), class: 'fr-btn fr-btn--secondary fr-btn--md' = f.submit t("views.prefill_descriptions.edit.champ_remove"), class: 'fr-btn fr-btn--secondary fr-btn--md'
- elsif type_de_champ.prefillable? - elsif prefillable
- (prefill_description.selected_type_de_champ_ids + [type_de_champ.id.to_s]).each do |id| - (prefill_description.selected_type_de_champ_ids + [type_de_champ.id.to_s]).each do |id|
= f.hidden_field :selected_type_de_champ_ids, value: id, multiple: true = f.hidden_field :selected_type_de_champ_ids, value: id, multiple: true
= f.submit t("views.prefill_descriptions.edit.champ_add"), class: 'fr-btn fr-btn--md' = f.submit t("views.prefill_descriptions.edit.champ_add"), class: 'fr-btn fr-btn--md'
@ -32,13 +34,13 @@
= t("views.prefill_descriptions.edit.champ_type") = t("views.prefill_descriptions.edit.champ_type")
%td %td
= t("activerecord.attributes.type_de_champ.type_champs.#{type_de_champ.type_champ}") = t("activerecord.attributes.type_de_champ.type_champs.#{type_de_champ.type_champ}")
%tr %tr{ class: prefillable ? "" : "fr-text-mention--grey" }
%th %th
= t("views.prefill_descriptions.edit.possible_values.title") = t("views.prefill_descriptions.edit.possible_values.title")
%td %td
= t("views.prefill_descriptions.edit.possible_values.#{type_de_champ.type_champ}") = t("views.prefill_descriptions.edit.possible_values.#{type_de_champ.type_champ}") if prefillable
%tr %tr{ class: prefillable ? "" : "fr-text-mention--grey" }
%th %th
= t("views.prefill_descriptions.edit.examples.title") = t("views.prefill_descriptions.edit.examples.title")
%td %td
= t("views.prefill_descriptions.edit.examples.#{type_de_champ.type_champ}") = t("views.prefill_descriptions.edit.examples.#{type_de_champ.type_champ}") if prefillable

View file

@ -17,7 +17,7 @@ RSpec.describe PrefillDescription, type: :model do
describe '#types_de_champ' do describe '#types_de_champ' do
let(:procedure) { create(:procedure) } let(:procedure) { create(:procedure) }
let(:type_de_champ) { create(:type_de_champ_text, procedure: procedure) } let!(:type_de_champ) { create(:type_de_champ_text, procedure: procedure) }
let(:prefill_description) { described_class.new(procedure) } let(:prefill_description) { described_class.new(procedure) }
it { expect(prefill_description.types_de_champ).to match([type_de_champ]) } it { expect(prefill_description.types_de_champ).to match([type_de_champ]) }
@ -32,6 +32,19 @@ RSpec.describe PrefillDescription, type: :model do
it_behaves_like "filters out non fillable types de champ", :type_de_champ_header_section it_behaves_like "filters out non fillable types de champ", :type_de_champ_header_section
it_behaves_like "filters out non fillable types de champ", :type_de_champ_explication it_behaves_like "filters out non fillable types de champ", :type_de_champ_explication
context 'when the procedure contains prefillable and non prefillable types de champ' do
let!(:non_prefillable_type_de_champ) { create(:type_de_champ_carte, procedure: procedure) }
let!(:prefillable_type_de_champ) { create(:type_de_champ_decimal_number, procedure: procedure) }
it "sort types de champ by putting prefillable ones first" do
expect(prefill_description.types_de_champ).to eq([
type_de_champ,
prefillable_type_de_champ,
non_prefillable_type_de_champ
])
end
end
end end
describe '#include?' do describe '#include?' do