From 28c4dde96db301b61c9051cfa515ea3c84a3a41c Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Wed, 28 Feb 2018 17:14:00 +0100 Subject: [PATCH] _explication: display champ.description instead of champ.value --- app/controllers/root_controller.rb | 5 ++-- app/helpers/champ_helper.rb | 6 ++++ .../editable_champs/_explication.html.haml | 3 +- spec/helpers/champ_helper_spec.rb | 29 +++++++++++++++++++ 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 spec/helpers/champ_helper_spec.rb diff --git a/app/controllers/root_controller.rb b/app/controllers/root_controller.rb index ffb592c80..3e6031214 100644 --- a/app/controllers/root_controller.rb +++ b/app/controllers/root_controller.rb @@ -20,7 +20,7 @@ class RootController < ApplicationController end def patron - description = 'a not so long description' + description = 'aller voir le super site : https://demarches-simplifiees.fr' all_champs = TypeDeChamp.type_champs .map { |name, _| TypeDeChamp.new(type_champ: name, private: false, libelle: name, description: description, mandatory: true) } @@ -45,8 +45,7 @@ class RootController < ApplicationController type_champ_values = { 'date': '2016-07-26', 'datetime': '26/07/2016 07:35', - 'textarea': 'Une description de mon projet', - 'explication': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In erat mauris, faucibus quis pharetra sit amet, pretium ac libero. Etiam vehicula eleifend bibendum. Morbi gravida metus ut sapien condimentum sodales mollis augue sodales. Vestibulum quis quam at sem placerat aliquet', + 'textarea': 'Une description de mon projet' } type_champ_values.each do |(type_champ, value)| diff --git a/app/helpers/champ_helper.rb b/app/helpers/champ_helper.rb index b9145628a..aefbb0490 100644 --- a/app/helpers/champ_helper.rb +++ b/app/helpers/champ_helper.rb @@ -2,4 +2,10 @@ module ChampHelper def is_not_header_nor_explication?(champ) !['header_section', 'explication'].include?(champ.type_champ) end + + def html_formatted_description(description) + html_formatted = simple_format(description) + with_links = html_formatted.gsub(URI.regexp, '\0') + sanitize(with_links, attributes: %w(href target)) + end end diff --git a/app/views/shared/dossiers/editable_champs/_explication.html.haml b/app/views/shared/dossiers/editable_champs/_explication.html.haml index 0baedbbae..07ef71490 100644 --- a/app/views/shared/dossiers/editable_champs/_explication.html.haml +++ b/app/views/shared/dossiers/editable_champs/_explication.html.haml @@ -1,2 +1,3 @@ %h2.explication-libelle= champ.libelle -%p.explication= champ.value +.explication + = html_formatted_description(champ.description) diff --git a/spec/helpers/champ_helper_spec.rb b/spec/helpers/champ_helper_spec.rb new file mode 100644 index 000000000..581e1be2c --- /dev/null +++ b/spec/helpers/champ_helper_spec.rb @@ -0,0 +1,29 @@ +RSpec.describe ChampHelper, type: :helper do + describe "#html_formatted_description" do + subject { html_formatted_description(description) } + + context "with some simple texte" do + let(:description) { "1er ligne \n 2ieme ligne" } + + it { is_expected.to eq("

1er ligne \n
2ieme ligne

") } + end + + context "with a link" do + let(:description) { "https://d-s.fr" } + + it { is_expected.to eq("

https://d-s.fr

") } + end + + context "with empty decription" do + let(:description) { nil } + + it { is_expected.to eq('

') } + end + + context "with a bad script" do + let(:description) { '' } + + it { is_expected.to eq('

bad

') } + end + end +end