Skip to content

Commit

Permalink
[i886] - update logic to fix collection thumbnail bug
Browse files Browse the repository at this point in the history
The default work thumbnail was displaying even though the default collection thumbnail was set.

Issue:
- scientist-softserv/adventist_knapsack#886
  • Loading branch information
ShanaLMoore committed Nov 13, 2024
1 parent 8cf3a6f commit 50043e7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
15 changes: 7 additions & 8 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ def group_navigation_presenter
# - fallback to Hyrax's default image
def collection_thumbnail(document, _image_options = {}, url_options = {})
view_class = url_options[:class]
# The correct thumbnail SHOULD be indexed on the object
return image_tag(document['thumbnail_path_ss'], class: view_class, alt: alttext_for(document)) if document['thumbnail_path_ss'].present?
# Use collection branding thumbnail if it exists
if document['thumbnail_path_ss'].present? && !document['thumbnail_path_ss'].include?('default_work_images')
return image_tag(document['thumbnail_path_ss'], class: view_class, alt: alttext_for(document))
end

# If nothing is indexed, we just fall back to site default
return image_tag(Site.instance.default_collection_image&.url, alt: alttext_for(document), class: view_class) if Site.instance.default_collection_image.present?
# If no branding thumbnail, use the site's default collection image
return image_tag(Site.instance.default_collection_image.url, alt: alttext_for(document), class: view_class) if Site.instance.default_collection_image.present?

# fall back to Hyrax default if no site default
# Fallback to Hyrax's default image if no site default
tag.span("", class: [Hyrax::ModelIcon.css_class_for(::Collection), view_class],
alt: alttext_for(document))
end
Expand All @@ -41,7 +43,6 @@ def alttext_for(collection)

def hint_for(term:, record_class: nil)
hint = locale_for(type: 'hints', term:, record_class:)

return hint unless missing_translation(hint)
end

Expand All @@ -50,7 +51,6 @@ def locale_for(type:, term:, record_class:)
record_class = record_class.to_s.downcase
work_or_collection = record_class == Hyrax.config.collection_model.downcase ? 'collection' : 'defaults'
locale = t("hyrax.#{record_class}.#{type}.#{term}")

if missing_translation(locale)
(t("simple_form.#{type}.#{work_or_collection}.#{term}")).try(:html_safe)
else
Expand All @@ -66,7 +66,6 @@ def missing_translation(value, _options = {})

def markdown(text)
return text unless Flipflop.treat_some_user_inputs_as_markdown?

# Consider extracting these options to a Hyku::Application
# configuration/class attribute.
options = %i[
Expand Down
7 changes: 6 additions & 1 deletion app/services/hyrax/thumbnail_path_service_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ module ThumbnailPathServiceDecorator
def call(object)
return super unless object.try(:collection?)

# Check if a collection-specific branding thumbnail exists
collection_thumbnail = CollectionBrandingInfo.where(collection_id: object.id.to_s, role: "thumbnail").first
return collection_thumbnail.local_path.gsub(Hyrax.config.branding_path.to_s, '/branding') if collection_thumbnail

return default_image if object.try(:thumbnail_id).blank?
# Fallback to site-wide default collection image if no branding thumbnail
return default_collection_image if object.thumbnail_id.blank?

# Retrieve thumbnail if it exists, otherwise use the default collection image
thumb = fetch_thumbnail(object)
return default_collection_image unless thumb

# Process thumbnail for audio or default image types
return call(thumb) unless thumb.file_set?
if audio?(thumb)
audio_image
Expand Down
64 changes: 61 additions & 3 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true

RSpec.describe ApplicationHelper, type: :helper do
describe "#markdown" do
let(:header) { '# header' }
Expand All @@ -8,7 +7,6 @@
context 'when treat_some_user_inputs_as_markdown is true' do
it 'renders markdown into html' do
allow(Flipflop).to receive(:treat_some_user_inputs_as_markdown?).and_return(true)

expect(helper.markdown(header)).to eq("<h1>header</h1>\n")
expect(helper.markdown(bold)).to eq("<p><em>bold</em></p>\n")
end
Expand All @@ -17,7 +15,6 @@
context 'when treat_some_user_inputs_as_markdown is false' do
it 'does not render markdown into html' do
allow(Flipflop).to receive(:treat_some_user_inputs_as_markdown?).and_return(false)

expect(helper.markdown(header)).to eq('# header')
expect(helper.markdown(bold)).to eq('*bold*')
end
Expand All @@ -31,4 +28,65 @@
end
end
end

describe '#collection_thumbnail' do
let(:document) { instance_double('SolrDocument', id: '123', title_or_label: 'Test Collection') }
let(:url_options) { { class: 'view-class' } }

context 'when collection has a branding thumbnail' do
before do
allow(document).to receive(:[]).with('thumbnail_path_ss').and_return('/path/to/thumbnail.jpg')
end

it 'returns the branding thumbnail image tag' do
expect(helper.collection_thumbnail(document, {}, url_options)).to include('src="/path/to/thumbnail.jpg"')
expect(helper.collection_thumbnail(document, {}, url_options)).to include('class="view-class"')
end
end

context 'when collection has no branding thumbnail but site has default image' do
let(:site_instance) { instance_double('Site') }
let(:default_image) { instance_double('DefaultImage', url: '/default/image.jpg') }

before do
allow(document).to receive(:[]).with('thumbnail_path_ss').and_return(nil)
allow(Site).to receive(:instance).and_return(site_instance)
allow(site_instance).to receive(:default_collection_image).and_return(default_image)
allow(site_instance).to receive(:default_collection_image).and_return(default_image)
end

it 'returns the site default collection image tag' do
expect(helper.collection_thumbnail(document, {}, url_options)).to include('src="/default/image.jpg"')
expect(helper.collection_thumbnail(document, {}, url_options)).to include('class="view-class"')
end
end

context 'when collection has no branding thumbnail and no site default image' do
before do
allow(document).to receive(:[]).with('thumbnail_path_ss').and_return(nil)
allow(Site).to receive(:instance).and_return(instance_double('Site', default_collection_image: nil))
allow(Hyrax::ModelIcon).to receive(:css_class_for).with(::Collection).and_return('collection-icon-class')
end

it 'returns the Hyrax default icon span' do
result = helper.collection_thumbnail(document, {}, url_options)
expect(result).to include('class="collection-icon-class view-class"')
expect(result).to include('<span')
end
end

context 'when thumbnail path includes default_work_images' do
before do
allow(document).to receive(:[]).with('thumbnail_path_ss').and_return('/default_work_images/thumb.jpg')
allow(Site).to receive(:instance).and_return(instance_double('Site', default_collection_image: nil))
allow(Hyrax::ModelIcon).to receive(:css_class_for).with(::Collection).and_return('collection-icon-class')
end

it 'falls back to Hyrax default icon' do
result = helper.collection_thumbnail(document, {}, url_options)
expect(result).to include('class="collection-icon-class view-class"')
expect(result).to include('<span')
end
end
end
end

0 comments on commit 50043e7

Please sign in to comment.