From 09581ad028c73a0d1d1006601d5c1485c78feb22 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Thu, 4 Jul 2024 21:02:07 +0200 Subject: [PATCH] feat(attestation): render multiple drop down list as list --- .../multiple_drop_down_list_presentation.rb | 35 +++++++++++++++++++ .../multiple_drop_down_list_type_de_champ.rb | 2 +- app/services/tiptap_service.rb | 7 +++- ...ltiple_drop_down_list_presentation_spec.rb | 27 ++++++++++++++ .../multiple_drop_down_list_champ_spec.rb | 5 +++ spec/services/tiptap_service_spec.rb | 18 ++++++++-- 6 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 app/models/champ_presentations/multiple_drop_down_list_presentation.rb create mode 100644 spec/models/champ_presentations/multiple_drop_down_list_presentation_spec.rb diff --git a/app/models/champ_presentations/multiple_drop_down_list_presentation.rb b/app/models/champ_presentations/multiple_drop_down_list_presentation.rb new file mode 100644 index 000000000..21290193e --- /dev/null +++ b/app/models/champ_presentations/multiple_drop_down_list_presentation.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +class ChampPresentations::MultipleDropDownListPresentation + attr_reader :selected_options + + def initialize(selected_options) + @selected_options = selected_options + end + + def to_s + selected_options.join(', ') + end + + def to_tiptap_node + { + type: 'bulletList', + content: selected_options.map do |text| + { + type: 'listItem', + content: [ + { + type: 'paragraph', + content: [ + { + type: 'text', + text: text + } + ] + } + ] + } + end + } + end +end diff --git a/app/models/types_de_champ/multiple_drop_down_list_type_de_champ.rb b/app/models/types_de_champ/multiple_drop_down_list_type_de_champ.rb index 9a4b54679..2e8adf47d 100644 --- a/app/models/types_de_champ/multiple_drop_down_list_type_de_champ.rb +++ b/app/models/types_de_champ/multiple_drop_down_list_type_de_champ.rb @@ -7,7 +7,7 @@ class TypesDeChamp::MultipleDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampB end def champ_value_for_tag(champ, path = :value) - champ.selected_options.join(', ') + ChampPresentations::MultipleDropDownListPresentation.new(champ.selected_options) end def champ_value_for_export(champ, path = :value) diff --git a/app/services/tiptap_service.rb b/app/services/tiptap_service.rb index d86415d93..6fd0a4e35 100644 --- a/app/services/tiptap_service.rb +++ b/app/services/tiptap_service.rb @@ -90,7 +90,12 @@ class TiptapService text end in type: 'mention', attrs: { id: }, **rest - text = substitutions.fetch(id) { "--#{id}--" } + text_or_representation = substitutions.fetch(id) { "--#{id}--" } + text = if text_or_representation.respond_to?(:to_tiptap_node) + node_to_html(text_or_representation.to_tiptap_node, substitutions, level + 1) + else + text_or_representation + end if rest[:marks].present? apply_marks(text, rest[:marks]) diff --git a/spec/models/champ_presentations/multiple_drop_down_list_presentation_spec.rb b/spec/models/champ_presentations/multiple_drop_down_list_presentation_spec.rb new file mode 100644 index 000000000..5a23f6ae1 --- /dev/null +++ b/spec/models/champ_presentations/multiple_drop_down_list_presentation_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +describe ChampPresentations::MultipleDropDownListPresentation do + let(:options) { ["Option 1", "Option 2", "Option 3"] } + let(:representation) { described_class.new(options) } + + describe '#to_s' do + it 'returns a comma-separated string of options' do + expect(representation.to_s).to eq("Option 1, Option 2, Option 3") + end + end + + describe '#to_tiptap_node' do + it 'returns the correct node structure' do + expected_node = { + type: "bulletList", + content: [ + { content: [{ content: [{ :text => "Option 1", type: "text" }], type: "paragraph" }], type: "listItem" }, + { content: [{ content: [{ :text => "Option 2", type: "text" }], type: "paragraph" }], type: "listItem" }, + { content: [{ content: [{ :text => "Option 3", type: "text" }], type: "paragraph" }], type: "listItem" } + ] + } + + expect(representation.to_tiptap_node).to eq(expected_node) + end + end +end diff --git a/spec/models/champs/multiple_drop_down_list_champ_spec.rb b/spec/models/champs/multiple_drop_down_list_champ_spec.rb index 7702f9494..6ee9805c3 100644 --- a/spec/models/champs/multiple_drop_down_list_champ_spec.rb +++ b/spec/models/champs/multiple_drop_down_list_champ_spec.rb @@ -84,4 +84,9 @@ describe Champs::MultipleDropDownListChamp do it { expect(champ.next_checkbox_id("val1")).to eq(nil) } end end + + describe "#for_tag" do + let(:value) { ["val1", "val2"] } + it { expect(champ.for_tag.to_s).to eq("val1, val2") } + end end diff --git a/spec/services/tiptap_service_spec.rb b/spec/services/tiptap_service_spec.rb index 93d6fc91c..669a70329 100644 --- a/spec/services/tiptap_service_spec.rb +++ b/spec/services/tiptap_service_spec.rb @@ -138,6 +138,19 @@ RSpec.describe TiptapService do } ] }, + { + type: 'paragraph', + content: [ + { + type: 'text', + text: 'Langages de prédilection:' + }, + { + type: 'mention', + attrs: { id: 'languages', label: 'Langages' } + } + ] + }, { type: 'footer', content: [{ type: 'text', text: 'Footer' }] @@ -147,7 +160,7 @@ RSpec.describe TiptapService do end describe '.to_html' do - let(:substitutions) { { 'name' => 'Paul' } } + let(:substitutions) { { 'name' => 'Paul', 'languages' => ChampPresentations::MultipleDropDownListPresentation.new(['ruby', 'rust']) } } let(:html) do [ '
Left
Right
', @@ -158,6 +171,7 @@ RSpec.describe TiptapService do '

Bonjour Paul !

', '', '
  1. Item 1

  2. Item 2

', + '

Langages de prédilection:

', # TODO: fix this markup,