Skip to content

Commit

Permalink
removing unnecessary code from override
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcam-src committed Dec 3, 2024
1 parent 783fb5e commit 99c82b5
Showing 1 changed file with 2 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,80 +1,7 @@
# frozen_string_literal: true

module Blacklight
class ThumbnailPresenter
attr_reader :document, :view_context, :view_config

# @param [SolrDocument] document
# @param [ActionView::Base] view_context scope for linking and generating urls
# as well as for invoking "thumbnail_method"
# @param [Blacklight::Configuration::ViewConfig] view_config
def initialize(document, view_context, view_config)
@document = document
@view_context = view_context
@view_config = view_config
end

def render(image_options = {})
thumbnail_value(image_options)
end

##
# Does the document have a thumbnail to render?
#
# @return [Boolean]
def exists?
thumbnail_method.present? ||
(thumbnail_field && thumbnail_value_from_document.present?) ||
default_thumbnail.present?
end

##
# Render the thumbnail, if available, for a document and
# link it to the document record.
#
# @param [Hash] image_options to pass to the image tag
# @param [Hash] url_options to pass to #link_to_document
# @return [String]
def thumbnail_tag(image_options = {}, url_options = {})
value = thumbnail_value(image_options)
return value if value.nil? || url_options[:suppress_link]

view_context.link_to_document document, value, url_options
end

private

delegate :thumbnail_field, :thumbnail_method, :default_thumbnail, to: :view_config

# @param [Hash] image_options to pass to the image tag
def thumbnail_value(image_options)
value = if thumbnail_method
view_context.send(thumbnail_method, document, image_options)
elsif thumbnail_field
image_url = thumbnail_value_from_document
view_context.image_tag image_url, image_options if image_url.present?
end

value || default_thumbnail_value(image_options)
end

def default_thumbnail_value(image_options)
return unless default_thumbnail

case default_thumbnail
when Symbol
view_context.send(default_thumbnail, document, image_options)
when Proc
default_thumbnail.call(document, image_options)
else
view_context.image_tag default_thumbnail, image_options
end
end

def thumbnail_value_from_document
Array(thumbnail_field).lazy.map { |field| retrieve_values(field_config(field)).first }.compact_blank.first
end

# [hyc-override] Retrieve the thumbnail of a document's first file_set instead of using the default if it exists
def retrieve_values(field_config)
# Extract the SolrDocument from the document if it's nested
Expand All @@ -100,6 +27,8 @@ def retrieve_values(field_config)
end
end

# Extract the SolrDocument from the document if it's nested
# Prevents errors when the document is a presenter on work show pages
def extract_solr_document(doc)
if doc.is_a?(SolrDocument)
doc
Expand All @@ -118,11 +47,5 @@ def needs_thumbnail_path_update?(document)
# Check if the thumbnail path is the default or missing and file_set_ids are present
thumbnail_path !~ %r{^/downloads/\w+\?file=thumbnail$} && file_set_ids.present?
end

def field_config(field)
return field if field.is_a? Blacklight::Configuration::Field

Configuration::NullField.new(field)
end
end
end

0 comments on commit 99c82b5

Please sign in to comment.