dry(export_template_form): extract checkbox component

This commit is contained in:
mfo 2024-11-04 16:43:39 +01:00
parent 9530099d23
commit f014006542
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
5 changed files with 108 additions and 25 deletions

View file

@ -1,5 +1,5 @@
%fieldset.fr-fieldset{ id: "#{component_prefix}-fieldset", data: { controller: 'checkbox-select-all' } } %fieldset.fr-fieldset{ id: "#{component_prefix}-fieldset", data: { controller: 'checkbox-select-all' } }
%legend.fr-fieldset__legend--regular.fr-fieldset__legend %legend.fr-fieldset__legend--regular.fr-fieldset__legend.fr-h5.fr-pb-0
= title = title
.checkbox-group-bordered.fr-mx-1w.fr-mb-2w .checkbox-group-bordered.fr-mx-1w.fr-mb-2w
.fr-fieldset__element.fr-background-contrast--grey.fr-py-2w.fr-px-4w .fr-fieldset__element.fr-background-contrast--grey.fr-py-2w.fr-px-4w
@ -16,14 +16,8 @@
.fieldset-bordered.fr-ml-3v .fieldset-bordered.fr-ml-3v
- grouped_columns.each do |exported_column| - grouped_columns.each do |exported_column|
.fr-fieldset__element.fr-px-3v .fr-fieldset__element.fr-px-3v
.fr-checkbox-group .fr-checkbox-group= render ExportTemplate::CheckboxComponent.new(export_template:, exported_column:)
- id = sanitize_to_id(field_id('export_template', 'exported_columns', exported_column.id))
= check_box_tag field_name('export_template', 'exported_columns', ''), exported_column.id, export_template.exported_columns.map(&:column).include?(exported_column.column), class: 'fr-checkbox', id: id, data: { "checkbox-select-all-target": 'checkbox' }
= label_tag id, historical_libelle(exported_column.column)
- else - else
- grouped_columns.each do |exported_column| - grouped_columns.each do |exported_column|
.fr-fieldset__element.fr-px-4w .fr-fieldset__element.fr-px-4w
.fr-checkbox-group .fr-checkbox-group= render ExportTemplate::CheckboxComponent.new(export_template:, exported_column:)
- id = sanitize_to_id(field_id('export_template', 'exported_columns', exported_column.id))
= check_box_tag field_name('export_template', 'exported_columns', ''), exported_column.id, export_template.exported_columns.map(&:column).include?(exported_column.column), class: 'fr-checkbox', id: id, data: { "checkbox-select-all-target": 'checkbox' }
= label_tag id, historical_libelle(exported_column.column)

View file

@ -0,0 +1,32 @@
# frozen_string_literal: true
class ExportTemplate::CheckboxComponent < ApplicationComponent
attr_reader :exported_column, :export_template
def initialize(export_template:, exported_column:)
@export_template = export_template
@exported_column = exported_column
end
def call
safe_join([
check_box,
label_tag(label_id, exported_column.libelle)
])
end
def check_box
check_box_tag(
'export_template[exported_columns][]',
exported_column.id,
export_template.in_export?(exported_column),
class: 'fr-checkbox',
id: sanitize_to_id(label_id), # sanitize_to_id is used by rails in label_tag
data: { "checkbox-select-all-target": 'checkbox' }
)
end
def label_id
exported_column.column.id
end
end

View file

@ -1,5 +1,5 @@
%fieldset.fr-fieldset{ id: "#{title.parameterize}-fieldset", data: { controller: 'checkbox-select-all' } } %fieldset.fr-fieldset{ id: "#{title.parameterize}-fieldset", data: { controller: 'checkbox-select-all' } }
%legend.fr-fieldset__legend--regular.fr-fieldset__legend %legend.fr-fieldset__legend--regular.fr-fieldset__legend.fr-h5.fr-pb-0
= title = title
.checkbox-group-bordered.fr-mx-1w.fr-mb-2w .checkbox-group-bordered.fr-mx-1w.fr-mb-2w
@ -11,6 +11,4 @@
- all_columns.each do |column| - all_columns.each do |column|
.fr-fieldset__element.fr-px-4w .fr-fieldset__element.fr-px-4w
.fr-checkbox-group .fr-checkbox-group
- id = sanitize_to_id(field_id('export_template', 'exported_columns', { id: column.id, libelle: column.label, parent: nil }.to_json)) = render ExportTemplate::CheckboxComponent.new(export_template:, exported_column: ExportedColumn.new(libelle: column.label, column:))
= check_box_tag field_name('export_template', 'exported_columns', ''), { id: column.id, libelle: column.label, parent: nil }.to_json, checked_columns.map(&:column).include?(column), class: 'fr-checkbox', id: id, data: { "checkbox-select-all-target": 'checkbox' }
= label_tag id, column.label

View file

@ -28,30 +28,30 @@
%fieldset.fr-fieldset.fr-fieldset--inline %fieldset.fr-fieldset.fr-fieldset--inline
%legend#radio-inline-legend.fr-fieldset__legend.fr-text--regular %legend#radio-inline-legend.fr-fieldset__legend.fr-text--regular
Format export Format export
= asterisk
.fr-fieldset__element.fr-fieldset__element--inline .fr-fieldset__element.fr-fieldset__element--inline
.fr-radio-group
= f.radio_button :kind, "ods", id: "ods"
%label.fr-label{ for: "ods" } ods
.fr-radio-group .fr-radio-group
= f.radio_button :kind, "xlsx", id: "xlsx" = f.radio_button :kind, "xlsx", id: "xlsx"
%label.fr-label{ for: "xlsx" } xlsx %label.fr-label{ for: "xlsx" } xlsx
.fr-radio-group
= f.radio_button :kind, "ods", id: "ods"
%label.fr-label{ for: "ods" } ods
.fr-radio-group .fr-radio-group
= f.radio_button :kind, "csv", id: "csv" = f.radio_button :kind, "csv", id: "csv"
%label.fr-label{ for: "csv" } csv %label.fr-label{ for: "csv" } csv
%h2 Contenu de l'export %h2 Contenu de l'export
= render partial: 'checkbox_group', locals: { title: 'Colonnes Usager', all_columns: @export_template.procedure.usager_columns_for_export, checked_columns: @export_template.exported_columns } %p Sélectionnez les colonnes que vous souhaitez voir affichées dans le tableau de votre export.
= render partial: 'checkbox_group', locals: { title: 'Colonnes Infos dossier', all_columns: @export_template.procedure.dossier_columns_for_export, checked_columns: @export_template.exported_columns }
= render ExportTemplate::ChampsComponent.new("Informations formulaire", @export_template, @types_de_champ_public) = render partial: 'checkbox_group', locals: { title: 'Informations usager', all_columns: @export_template.procedure.usager_columns_for_export, export_template: @export_template }
= render ExportTemplate::ChampsComponent.new("Informations annotations", @export_template, @types_de_champ_private) if @types_de_champ_private.any? = render partial: 'checkbox_group', locals: { title: 'Informations dossier', all_columns: @export_template.procedure.dossier_columns_for_export, export_template: @export_template }
= render ExportTemplate::ChampsComponent.new("Formulaire usager", @export_template, @types_de_champ_public)
= render ExportTemplate::ChampsComponent.new("Annotations privées", @export_template, @types_de_champ_private) if @types_de_champ_private.any?
.fixed-footer .fixed-footer
.fr-container .fr-container
%ul.fr-btns-group.fr-btns-group--inline-md %ul.fr-btns-group.fr-btns-group--inline-md
%li
= f.submit "Enregistrer", class: "fr-btn"
%li %li
= link_to "Annuler", instructeur_procedure_path(@procedure), class: "fr-btn fr-btn--secondary" = link_to "Annuler", instructeur_procedure_path(@procedure), class: "fr-btn fr-btn--secondary"
- if @export_template.persisted? %li
%li = f.submit "Enregistrer", class: "fr-btn", data: @export_template.persisted? ? { confirm: t('.warning') } : {}
= link_to "Supprimer", [:instructeur, @procedure, @export_template], method: :delete, data: { confirm: "Voulez-vous vraiment supprimer ce modèle ? Il sera supprimé pour tous les instructeurs du groupe"}, class: "fr-btn fr-btn--secondary"

View file

@ -82,4 +82,63 @@ describe ExportTemplate do
end end
end end
end end
describe 'dossier_exported_columns' do
context 'when exported_columns is empty' do
it 'returns an empty array' do
expect(export_template.dossier_exported_columns).to eq([])
end
end
context 'when exported_columns is not empty' do
before do
export_template.exported_columns = [
ExportedColumn.new(libelle: 'Colonne usager', column: procedure.find_column(label: "Email")),
ExportedColumn.new(libelle: 'Ça va ?', column: procedure.find_column(label: "Ca va ?"))
]
end
it 'returns all columns except tdc columns' do
expect(export_template.dossier_exported_columns.size).to eq(1) # exclude tdc
expect(export_template.dossier_exported_columns.first.libelle).to eq("Colonne usager")
end
end
end
describe 'columns_for_stable_id' do
before do
export_template.exported_columns = procedure.published_revision.types_de_champ.first.columns(procedure: procedure).map do |column|
ExportedColumn.new(libelle: column.label, column:)
end
end
context 'when procedure has a TypeDeChamp::Commune' do
let(:types_de_champ_public) do
[
{ type: :communes, libelle: "Commune", mandatory: true, stable_id: 17 }
]
end
it 'is able to resolve stable_id' do
expect(export_template.columns_for_stable_id(17).size).to eq(3)
end
end
context 'when procedure has a TypeDeChamp::Siret' do
let(:types_de_champ_public) do
[
{ type: :siret, libelle: 'siret', stable_id: 20 }
]
end
it 'is able to resolve stable_id' do
expect(export_template.columns_for_stable_id(20).size).to eq(5)
end
end
context 'when procedure has a TypeDeChamp::Text' do
let(:types_de_champ_public) do
[
{ type: :text, libelle: "Text", mandatory: true, stable_id: 15 }
]
end
it 'is able to resolve stable_id' do
expect(export_template.columns_for_stable_id(15).size).to eq(1)
end
end
end
end end