From a017addaa145dc74a6b7f8076780f3bcc0fda291 Mon Sep 17 00:00:00 2001 From: JenDiamond Date: Mon, 23 Oct 2023 12:31:36 -0700 Subject: [PATCH] feat: APPS-2472-2473 Add autolink for url aware (#1129) * add autolink so the contents_note and related_ito fields can be url aware * add autolink to Gemfile * comment out older related_to code --- Gemfile | 1 + Gemfile.lock | 5 +++ app/controllers/catalog_controller.rb | 4 +- app/helpers/application_helper.rb | 29 +++++++++++++ app/helpers/blacklight_helper.rb | 20 ++++----- app/processors/auto_link.rb | 20 +++++++++ app/processors/custom_join.rb | 2 +- .../_note_metadata.html.erb | 7 ---- config/initializers/blacklight.rb | 11 +++-- config/metadata/note_metadata.yml | 1 + .../ursus/note_metadata_presenter_spec.rb | 41 +++++++++++-------- 11 files changed, 99 insertions(+), 42 deletions(-) create mode 100644 app/helpers/application_helper.rb create mode 100644 app/processors/auto_link.rb diff --git a/Gemfile b/Gemfile index 79ba28429..3904a0d49 100644 --- a/Gemfile +++ b/Gemfile @@ -26,6 +26,7 @@ gem 'mysql2', '~> 0.5' gem 'pkg-config', '~> 1.1' gem 'puma', '~> 5.5' # app server gem 'rails', '~> 5.2' +gem 'rails_autolink' gem 'rollbar' # Error reporting tool gem 'rsolr', '>= 1.0' gem 'sassc-rails', '>= 2.1.2' # SASS -> CSS compiler diff --git a/Gemfile.lock b/Gemfile.lock index 64cd44c78..b47129a3a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -322,6 +322,10 @@ GEM nokogiri (>= 1.6) rails-html-sanitizer (1.4.2) loofah (~> 2.3) + rails_autolink (1.1.8) + actionview (> 3.1) + activesupport (> 3.1) + railties (> 3.1) railties (5.2.7) actionpack (= 5.2.7) activesupport (= 5.2.7) @@ -503,6 +507,7 @@ DEPENDENCIES puma (~> 5.5) rails (~> 5.2) rails-controller-testing (>= 1.0.4) + rails_autolink rollbar rsolr (>= 1.0) rspec-collection_matchers diff --git a/app/controllers/catalog_controller.rb b/app/controllers/catalog_controller.rb index dbbf2dcae..0f0ec6b1f 100644 --- a/app/controllers/catalog_controller.rb +++ b/app/controllers/catalog_controller.rb @@ -218,13 +218,13 @@ class CatalogController < ApplicationController config.add_show_field 'description_tesim', label: 'Description' config.add_show_field 'caption_tesim', label: 'Caption' config.add_show_field 'toc_tesim', label: 'Table of Contents' - config.add_show_field 'contents_note_tesim', label: 'Contents note' + config.add_show_field 'contents_note_tesim', label: 'Contents note', auto_link: true # make this field url aware config.add_show_field 'provenance_tesim', label: 'Provenance' config.add_show_field 'colophon_tesim', label: 'Colophon' config.add_show_field 'note_tesim', label: 'Note' config.add_show_field 'resp_statement_tesim', label: 'Statement of Responsibility' config.add_show_field 'citation_source_tesim', label: 'References' - config.add_show_field 'related_to_ssm', label: 'Related Items' + config.add_show_field 'related_to_ssm', label: 'Related Items', auto_link: true # make this field url aware # Physical description config.add_show_field 'medium_tesim', label: 'Medium' diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb new file mode 100644 index 000000000..79cc6bcf6 --- /dev/null +++ b/app/helpers/application_helper.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true +require 'rails_autolink' + +module ApplicationHelper + include ERB::Util # provides html_escape + + # Uses Rails auto_link to add links to fields + # + # @param field [String,Hash] string to format and escape, or a hash as per helper_method + # @option field [SolrDocument] :document + # @option field [String] :field name of the solr field + # @option field [Blacklight::Configuration::IndexField, Blacklight::Configuration::ShowField] :config + # @option field [Array] :value array of values for the field + # @param show_link [Boolean] + # @return [ActiveSupport::SafeBuffer] + def iconify_auto_link(field, show_link = true) + if field.is_a? Hash + options = field[:config].separator_options || {} + text = field[:value].to_sentence(options) + else + text = field + end + # this block is only executed when a link is inserted; + # if we pass text containing no links, it just returns text. + auto_link(html_escape(text)) do |value| + "#{(' ' + value) if show_link}" + end + end +end diff --git a/app/helpers/blacklight_helper.rb b/app/helpers/blacklight_helper.rb index 131a7aaa6..20567ba9d 100644 --- a/app/helpers/blacklight_helper.rb +++ b/app/helpers/blacklight_helper.rb @@ -64,17 +64,17 @@ def render_other_versions_link data.html_safe end - def render_related_to_markup - related_to_text = '' - urls = @document[:related_to_ssm] - urls.each { |url| related_to_text += '' + url + '
' } - related_to_text - end + # def render_related_to_markup + # related_to_text = '' + # urls = @document[:related_to_ssm] + # urls.each { |url| related_to_text += '' + url + '
' } + # related_to_text + # end - def render_related_to_link - data = render_related_to_markup - data.html_safe - end + # def render_related_to_link + # data = render_related_to_markup + # data.html_safe + # end def render_table_of_contents_key unless @document[:toc_tesim].nil? || @document[:toc_tesim].empty? diff --git a/app/processors/auto_link.rb b/app/processors/auto_link.rb new file mode 100644 index 000000000..2bfda83a1 --- /dev/null +++ b/app/processors/auto_link.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true +# Joins values using configured value, linebreak, or delimiter +class AutoLink < Blacklight::Rendering::AbstractStep + include ActionView::Helpers::TextHelper + + def render + if config.auto_link + linked_values = values.map do |value| + auto_link(html_escape(value), &:to_s) + end + next_step(linked_values) + else + next_step(values) + end + end + + def html_escape(*args) + ERB::Util.html_escape(*args) + end +end diff --git a/app/processors/custom_join.rb b/app/processors/custom_join.rb index a9254a10a..97bc4c6f3 100644 --- a/app/processors/custom_join.rb +++ b/app/processors/custom_join.rb @@ -8,7 +8,7 @@ def render next_step(values.map { |x| html_escape(x) }) else joiner = (config.join_with || '
'.html_safe) - next_step(safe_join(values, joiner)) + next_step(safe_join(values.map(&:html_safe), joiner)) end end diff --git a/app/views/catalog/work_record--ursus/_note_metadata.html.erb b/app/views/catalog/work_record--ursus/_note_metadata.html.erb index 0895b3992..4a0656ba6 100644 --- a/app/views/catalog/work_record--ursus/_note_metadata.html.erb +++ b/app/views/catalog/work_record--ursus/_note_metadata.html.erb @@ -20,17 +20,10 @@
<%= raw doc_presenter.field_value field %>
- <% end %> - <% if @document[:related_to_ssm] %> -
Related Item(s)
- -
<%= render_related_to_link %>
- <% end %> -
<%= toc_key %>
<%= raw toc_value %>
diff --git a/config/initializers/blacklight.rb b/config/initializers/blacklight.rb index 2b9325552..e5ba2e984 100644 --- a/config/initializers/blacklight.rb +++ b/config/initializers/blacklight.rb @@ -1,7 +1,10 @@ # frozen_string_literal: true ActiveSupport::Reloader.to_prepare do - Blacklight::Rendering::Pipeline.operations = [Blacklight::Rendering::HelperMethod, - Blacklight::Rendering::LinkToFacet, - Blacklight::Rendering::Microdata, - CustomJoin] + Blacklight::Rendering::Pipeline.operations = [ + AutoLink, + Blacklight::Rendering::HelperMethod, + Blacklight::Rendering::LinkToFacet, + Blacklight::Rendering::Microdata, + CustomJoin + ] end diff --git a/config/metadata/note_metadata.yml b/config/metadata/note_metadata.yml index 746096a8d..18b8fc0f9 100644 --- a/config/metadata/note_metadata.yml +++ b/config/metadata/note_metadata.yml @@ -5,6 +5,7 @@ contents_note_tesim: 'Contents note' colophon_tesim: 'Colophon' provenance_tesim: 'Provenance' note_tesim: 'Note' +related_to_ssm: 'Related items' # toc_tesim: 'Table of Contents' (render_table_of_contents_key / value) resp_statement_tesim: 'Statement of Responsibility' citation_source_tesim: 'References' diff --git a/spec/presenters/ursus/note_metadata_presenter_spec.rb b/spec/presenters/ursus/note_metadata_presenter_spec.rb index 04dd9fa6f..5bb4c0b10 100644 --- a/spec/presenters/ursus/note_metadata_presenter_spec.rb +++ b/spec/presenters/ursus/note_metadata_presenter_spec.rb @@ -4,17 +4,18 @@ RSpec.describe Ursus::NoteMetadataPresenter do let(:solr_doc) do { - 'caption_tesim' => 'Caption', 'summary_tesim' => 'Summary', 'description_tesim' => 'Description', - 'provenance_tesim' => 'Provenance', + 'caption_tesim' => 'Caption', 'contents_note_tesim' => 'Contents note', 'colophon_tesim' => 'Colophon', - 'incipit_tesim' => 'Incipit', - 'explicit_tesim' => 'Explicit', + 'provenance_tesim' => 'Provenance', 'note_tesim' => 'Note', + 'related_to_ssm' => 'Related items', 'resp_statement_tesim' => 'Statement of Responsibility', - 'citation_source_tesim' => 'References' + 'citation_source_tesim' => 'References', + 'incipit_tesim' => 'Incipit', + 'explicit_tesim' => 'Explicit' } end let(:solr_doc_missing_items) do @@ -30,10 +31,6 @@ context 'with a solr document containing overview metadata' do describe 'config' do - it 'returns the Caption Key' do - expect(config['caption_tesim'].to_s).to eq('Caption') - end - it 'returns the Summary Key' do expect(config['summary_tesim'].to_s).to eq('Summary') end @@ -42,8 +39,8 @@ expect(config['description_tesim'].to_s).to eq('Description') end - it 'returns the Provenance Key' do - expect(config['provenance_tesim'].to_s).to eq('Provenance') + it 'returns the Caption Key' do + expect(config['caption_tesim'].to_s).to eq('Caption') end it 'returns the Contents note Key' do @@ -54,18 +51,18 @@ expect(config['colophon_tesim'].to_s).to eq('Colophon') end - it 'returns the Incipit Key' do - expect(config['incipit_tesim'].to_s).to eq('Incipit') - end - - it 'returns the Explicit Key' do - expect(config['explicit_tesim'].to_s).to eq('Explicit') + it 'returns the Provenance Key' do + expect(config['provenance_tesim'].to_s).to eq('Provenance') end it 'returns the Note Key' do expect(config['note_tesim'].to_s).to eq('Note') end + it 'returns the Related items Key' do + expect(config['related_to_ssm'].to_s).to eq('Related items') + end + it 'returns the Statement of Responsibility Key' do expect(config['resp_statement_tesim'].to_s).to eq('Statement of Responsibility') end @@ -73,6 +70,14 @@ it 'returns the References Key' do expect(config['citation_source_tesim'].to_s).to eq('References') end + + it 'returns the Incipit Key' do + expect(config['incipit_tesim'].to_s).to eq('Incipit') + end + + it 'returns the Explicit Key' do + expect(config['explicit_tesim'].to_s).to eq('Explicit') + end end describe "#note terms" do @@ -81,7 +86,7 @@ it "returns existing keys" do expect(presenter_object.note_terms).to be_instance_of(Hash) - expect(all).to eq 11 + expect(all).to eq 12 expect(config.length).to eq all end