diff --git a/app/presenters/hyrax/collection_presenter_decorator.rb b/app/presenters/hyrax/collection_presenter_decorator.rb index 7b3efc050..6ad55ce56 100644 --- a/app/presenters/hyrax/collection_presenter_decorator.rb +++ b/app/presenters/hyrax/collection_presenter_decorator.rb @@ -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? diff --git a/app/services/hyrax/thumbnail_path_service_decorator.rb b/app/services/hyrax/thumbnail_path_service_decorator.rb index 7209aba6b..2f44abbff 100644 --- a/app/services/hyrax/thumbnail_path_service_decorator.rb +++ b/app/services/hyrax/thumbnail_path_service_decorator.rb @@ -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 diff --git a/app/views/hyrax/my/collections/_list_collections.html.erb b/app/views/hyrax/my/collections/_list_collections.html.erb index 109c3208e..58c0bb3e3 100644 --- a/app/views/hyrax/my/collections/_list_collections.html.erb +++ b/app/views/hyrax/my/collections/_list_collections.html.erb @@ -28,7 +28,7 @@ <% 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 %> diff --git a/lib/hyrax/transactions/steps/save_collection_thumbnail.rb b/lib/hyrax/transactions/steps/save_collection_thumbnail.rb index 777865cb7..14036efc2 100644 --- a/lib/hyrax/transactions/steps/save_collection_thumbnail.rb +++ b/lib/hyrax/transactions/steps/save_collection_thumbnail.rb @@ -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:) @@ -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 diff --git a/spec/hyrax/transactions/steps/save_collection_thumbnail_spec.rb b/spec/hyrax/transactions/steps/save_collection_thumbnail_spec.rb new file mode 100644 index 000000000..47e8a6f21 --- /dev/null +++ b/spec/hyrax/transactions/steps/save_collection_thumbnail_spec.rb @@ -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