Skip to content

Commit

Permalink
Fix bug with setting the thumbnail alt text
Browse files Browse the repository at this point in the history
This commit will fix a bug where if the user sets the thumbnail first
and then later sets the alt text, they would have to have to remove the
thumbnail and re-add it and set the alt text at the same time to get the
alt text to be set.
  • Loading branch information
kirkkwang committed May 8, 2024
1 parent d6dd1d2 commit 0113466
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
10 changes: 10 additions & 0 deletions app/presenters/hyrax/collection_presenter_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ def banner_file
end
end

def thumbnail_record
CollectionBrandingInfo.where(collection_id: id, role: "thumbnail")
.select(:local_path, :alt_text, :target_url).map do |thumbnail|
{ alttext: thumbnail.alt_text,
file: File.split(thumbnail.local_path).last,
file_location: "/#{thumbnail.local_path.split('/')[-4..-1].join('/')}",
linkurl: thumbnail.target_url }
end
end

# Begin Featured Collections Methods
def collection_featurable?
user_can_feature_collection? && solr_document.public?
Expand Down
2 changes: 1 addition & 1 deletion app/services/hyrax/thumbnail_path_service_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module Hyrax
module ThumbnailPathServiceDecorator
def call(object)
return super unless object.collection?
return super unless object.try(:collection?)

collection_thumbnail = CollectionBrandingInfo.where(collection_id: object.id.to_s, role: "thumbnail").first
return collection_thumbnail.local_path.gsub(Rails.public_path.to_s, '') if collection_thumbnail
Expand Down
2 changes: 1 addition & 1 deletion app/views/hyrax/my/collections/_list_collections.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<span class="<%= Hyrax::ModelIcon.css_class_for(::Collection) %> collection-icon-small"></span>
<% else %>
<%# OVERRIDE begin %>
<%= image_tag(collection_presenter.thumbnail_path, alt: block_for(name: 'default_collection_image_text') || "#{collection_presenter.title_or_label} #{t('hyrax.dashboard.my.sr.thumbnail')}") %>
<%= image_tag(collection_presenter.thumbnail_path, alt: collection_presenter.thumbnail_record.first[:alttext] || block_for(name: 'default_collection_image_text') || "#{collection_presenter.title_or_label} #{t('hyrax.dashboard.my.sr.thumbnail')}") %>
<%# OVERRIDE end %>
<% end %>
</div>
Expand Down
20 changes: 14 additions & 6 deletions lib/hyrax/transactions/steps/save_collection_thumbnail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,25 @@ class SaveCollectionThumbnail
# @return [Dry::Monads::Result] `Failure` if the thumbnail info fails to save;
# `Success(input)`, otherwise.
def call(collection_resource, update_thumbnail_file_ids: nil, thumbnail_unchanged_indicator: true, alttext_values: nil)
return Success(collection_resource) if ActiveModel::Type::Boolean.new.cast(thumbnail_unchanged_indicator)
collection_id = collection_resource.id.to_s
process_thumbnail_input(collection_id:, update_thumbnail_file_ids:, alttext_values:)
process_thumbnail_input(collection_id:, update_thumbnail_file_ids:, thumbnail_unchanged_indicator:, alttext_values:)
Success(collection_resource)
end

private

def process_thumbnail_input(collection_id:, update_thumbnail_file_ids:, alttext_values:)
remove_thumbnail(collection_id:)
add_new_thumbnail(collection_id:, uploaded_file_ids: update_thumbnail_file_ids, alttext_values:) if update_thumbnail_file_ids
def process_thumbnail_input(collection_id:, update_thumbnail_file_ids:, thumbnail_unchanged_indicator:, alttext_values:)
if !update_thumbnail_file_ids && !alttext_values
remove_thumbnail(collection_id:)
elsif update_thumbnail_file_ids && thumbnail_unchanged_indicator.nil?
remove_thumbnail(collection_id:)
add_new_thumbnail(collection_id:, uploaded_file_ids: update_thumbnail_file_ids, alttext_values:)
else
CollectionBrandingInfo
.where(collection_id:, role: 'thumbnail')
.first
.update_column(:alt_text, alttext_values.first) # rubocop:disable Rails/SkipsModelValidations
end
end

def remove_thumbnail(collection_id:)
Expand All @@ -43,7 +51,7 @@ def add_new_thumbnail(collection_id:, uploaded_file_ids:, alttext_values:)
collection_id:,
filename: File.split(file.file_url).last,
role: "thumbnail",
alt_txt: alttext_values.first,
alt_txt: alttext_values&.first || "",
target_url: "TODO: link to the collection"
)
thumbnail_info.save file.file_url
Expand Down
24 changes: 24 additions & 0 deletions spec/hyrax/transactions/steps/save_collection_thumbnail_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'rails_helper'
require 'hyrax/transactions'

RSpec.describe Hyrax::Transactions::Steps::SaveCollectionThumbnail do
subject(:step) { described_class.new }
let(:collection) do
FactoryBot.valkyrie_create(:hyrax_collection,
title: "My Resource")
end

context 'update the thumbnail' do
let(:uploaded) { FactoryBot.create(:uploaded_file) }

it 'successfully updates the thumbnail' do
expect(step.call(collection, update_thumbnail_file_ids: [uploaded.id.to_s], thumbnail_unchanged_indicator: nil)).to be_success

expect(CollectionBrandingInfo
.where(collection_id: collection.id.to_s, role: "thumbnail")
.where("local_path LIKE '%#{uploaded.file.filename}'"))
.to exist
end
end
end

0 comments on commit 0113466

Please sign in to comment.