Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: APPS-2472-2473 Add autolink for url aware #1129

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
29 changes: 29 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -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|
"<span class='fa fa-external-link'></span>#{('&nbsp;' + value) if show_link}"
end
end
end
20 changes: 10 additions & 10 deletions app/helpers/blacklight_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 += '<a href="' + url + '">' + url + '</a> <br>' }
related_to_text
end
# def render_related_to_markup
# related_to_text = ''
# urls = @document[:related_to_ssm]
# urls.each { |url| related_to_text += '<a href="' + url + '">' + url + '</a> <br>' }
# 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?
Expand Down
20 changes: 20 additions & 0 deletions app/processors/auto_link.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/processors/custom_join.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def render
next_step(values.map { |x| html_escape(x) })
else
joiner = (config.join_with || '<br>'.html_safe)
next_step(safe_join(values, joiner))
next_step(safe_join(values.map(&:html_safe), joiner))
end
end

Expand Down
7 changes: 0 additions & 7 deletions app/views/catalog/work_record--ursus/_note_metadata.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,10 @@
<dd class="blacklight-<%= field_name.parameterize %> metadata-block__label-value metadata-block__label-value--ursus">
<%= raw doc_presenter.field_value field %>
</dd>

</dt>
<!-- LOOP END -->
<% end %>

<% if @document[:related_to_ssm] %>
<dt class="metadata-block__label-key">Related Item(s)</dt>

<dd class="metadata-block__label-value document__list-metadata-value--ursus"><%= render_related_to_link %></dd>
<% end %>

<!-- TABLE OF CONTENTS -->
<dt class='metadata-block__label-key'><%= toc_key %></dt>
<dd class='metadata-block__label-value'><%= raw toc_value %></dd>
Expand Down
11 changes: 7 additions & 4 deletions config/initializers/blacklight.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions config/metadata/note_metadata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
41 changes: 23 additions & 18 deletions spec/presenters/ursus/note_metadata_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -54,25 +51,33 @@
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

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
Expand All @@ -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

Expand Down
Loading