From e7ed408e085cf4024a90dd96032027a3031cd93e Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 26 Sep 2019 17:40:52 +0200 Subject: [PATCH] Auto-link valeur des champs closes #2865 --- Gemfile | 1 + Gemfile.lock | 2 ++ app/helpers/champ_helper.rb | 8 ++++++++ app/views/shared/champs/textarea/_show.html.haml | 2 +- app/views/shared/dossiers/_champ_row.html.haml | 2 +- spec/views/shared/dossiers/_champs.html.haml_spec.rb | 12 ++++++++++++ 6 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 455fe2e23..1690262c7 100644 --- a/Gemfile +++ b/Gemfile @@ -7,6 +7,7 @@ gem 'active_model_serializers' gem 'activestorage-openstack', git: 'https://github.com/fredZen/activestorage-openstack.git', branch: 'frederic/fix_upload_signature' gem 'administrate' gem 'after_party' +gem 'anchored' gem 'axlsx', '~> 3.0.0.pre' # https://github.com/randym/axlsx/issues/501#issuecomment-373640365 gem 'bcrypt' gem 'bootstrap-sass', '>= 3.4.1' diff --git a/Gemfile.lock b/Gemfile.lock index fdc04cea0..b1382ec9c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -95,6 +95,7 @@ GEM selectize-rails (~> 0.6) aes_key_wrap (1.0.1) after_party (1.10.0) + anchored (1.1.0) arel (9.0.0) ast (2.4.0) attr_required (1.0.1) @@ -722,6 +723,7 @@ DEPENDENCIES activestorage-openstack! administrate after_party + anchored axlsx (~> 3.0.0.pre) bcrypt bootstrap-sass (>= 3.4.1) diff --git a/app/helpers/champ_helper.rb b/app/helpers/champ_helper.rb index ba376d8bb..b761e914d 100644 --- a/app/helpers/champ_helper.rb +++ b/app/helpers/champ_helper.rb @@ -17,4 +17,12 @@ module ChampHelper { type_de_champ_id: champ.type_de_champ_id } end end + + def format_text_value(text) + sanitized_text = sanitize(text) + auto_linked_text = Anchored::Linker.auto_link(sanitized_text, target: '_blank', rel: 'noopener') do |link_href| + truncate(link_href, length: 60) + end + simple_format(auto_linked_text, {}, sanitize: false) + end end diff --git a/app/views/shared/champs/textarea/_show.html.haml b/app/views/shared/champs/textarea/_show.html.haml index cd7684777..5e5b33245 100644 --- a/app/views/shared/champs/textarea/_show.html.haml +++ b/app/views/shared/champs/textarea/_show.html.haml @@ -1 +1 @@ -= simple_format(champ.to_s) += format_text_value(champ.to_s) diff --git a/app/views/shared/dossiers/_champ_row.html.haml b/app/views/shared/dossiers/_champ_row.html.haml index 66d5b4807..8670a15c0 100644 --- a/app/views/shared/dossiers/_champ_row.html.haml +++ b/app/views/shared/dossiers/_champ_row.html.haml @@ -35,7 +35,7 @@ - when TypeDeChamp.type_champs.fetch(:datetime) = c.to_s - else - = sanitize(c.to_s) + = format_text_value(c.to_s) - if c.type_champ != TypeDeChamp.type_champs.fetch(:header_section) %td.updated-at diff --git a/spec/views/shared/dossiers/_champs.html.haml_spec.rb b/spec/views/shared/dossiers/_champs.html.haml_spec.rb index 81566f02a..dfce7ae25 100644 --- a/spec/views/shared/dossiers/_champs.html.haml_spec.rb +++ b/spec/views/shared/dossiers/_champs.html.haml_spec.rb @@ -40,6 +40,18 @@ describe 'shared/dossiers/champs.html.haml', type: :view do expect(subject).not_to include(champ3.libelle) expect(subject).not_to include(champ3.value) end + + context "with auto-link" do + let(:champ1) { create(:champ_text, value: "https://github.com/tchak") } + let(:champ2) { create(:champ_textarea, value: "https://github.com/LeSim") } + let(:link1) { 'https://github.com/tchak' } + let(:link2) { 'https://github.com/LeSim' } + + it "render links" do + expect(subject).to include(link1) + expect(subject).to include(link2) + end + end end context "with a dossier champ, but we are not authorized to acces the dossier" do