From 6f435769f38bff96a5f248a1308fa2ebb052c124 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Fri, 23 Jun 2017 20:07:07 -0500 Subject: [PATCH] Changes to the collection indexer to make it aware of IIIF Previously when you selected a collection thumbnail it did not know to use a IIIF url --- .rubocop.yml | 3 +- app/indexers/collection_indexer.rb | 15 ++++++++ app/indexers/file_set_indexer.rb | 2 +- app/indexers/image_indexer.rb | 2 +- app/indexers/work_indexer.rb | 2 +- app/models/collection.rb | 2 +- app/services/concerns/iiif_thumbnail_paths.rb | 29 ++++++++++++++ .../iiif_collection_thumbnail_path_service.rb | 8 ++++ app/services/iiif_thumbnail_path_service.rb | 30 --------------- .../iiif_work_thumbnail_path_service.rb | 3 ++ spec/indexers/collection_indexer_spec.rb | 9 +++++ spec/indexers/file_set_indexer_spec.rb | 2 +- spec/indexers/work_indexer_spec.rb | 2 +- spec/models/collection_spec.rb | 6 +++ ..._collection_thumbnail_path_service_spec.rb | 38 +++++++++++++++++++ ... iiif_work_thumbnail_path_service_spec.rb} | 2 +- 16 files changed, 117 insertions(+), 38 deletions(-) create mode 100644 app/indexers/collection_indexer.rb create mode 100644 app/services/concerns/iiif_thumbnail_paths.rb create mode 100644 app/services/iiif_collection_thumbnail_path_service.rb delete mode 100644 app/services/iiif_thumbnail_path_service.rb create mode 100644 app/services/iiif_work_thumbnail_path_service.rb create mode 100644 spec/indexers/collection_indexer_spec.rb create mode 100644 spec/services/iiif_collection_thumbnail_path_service_spec.rb rename spec/services/{iiif_thumbnail_path_service_spec.rb => iiif_work_thumbnail_path_service_spec.rb} (96%) diff --git a/.rubocop.yml b/.rubocop.yml index 541df979a..d8c16eb42 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -36,7 +36,8 @@ Metrics/LineLength: Exclude: - 'app/controllers/catalog_controller.rb' - 'spec/controllers/curation_concerns/generic_works_controller_spec.rb' - - 'spec/services/iiif_thumbnail_path_service_spec.rb' + - 'spec/services/iiif_collection_thumbnail_path_service_spec.rb' + - 'spec/services/iiif_work_thumbnail_path_service_spec.rb' - 'spec/routing/proprietor/accounts_routing_spec.rb' Style/IndentationConsistency: diff --git a/app/indexers/collection_indexer.rb b/app/indexers/collection_indexer.rb new file mode 100644 index 000000000..0ddab5cfc --- /dev/null +++ b/app/indexers/collection_indexer.rb @@ -0,0 +1,15 @@ +class CollectionIndexer < Hyrax::CollectionIndexer + # This indexes the default metadata. You can remove it if you want to + # provide your own metadata and indexing. + include Hyrax::IndexesBasicMetadata + + # Use thumbnails served by RIIIF + self.thumbnail_path_service = IIIFCollectionThumbnailPathService + + # Uncomment this block if you want to add custom indexing behavior: + # def generate_solr_document + # super.tap do |solr_doc| + # solr_doc['my_custom_field_ssim'] = object.my_custom_property + # end + # end +end diff --git a/app/indexers/file_set_indexer.rb b/app/indexers/file_set_indexer.rb index f83d0fe2b..11539fd4e 100644 --- a/app/indexers/file_set_indexer.rb +++ b/app/indexers/file_set_indexer.rb @@ -1,3 +1,3 @@ class FileSetIndexer < Hyrax::FileSetIndexer - self.thumbnail_path_service = IIIFThumbnailPathService + self.thumbnail_path_service = IIIFWorkThumbnailPathService end diff --git a/app/indexers/image_indexer.rb b/app/indexers/image_indexer.rb index d663b0898..a8adba1d6 100644 --- a/app/indexers/image_indexer.rb +++ b/app/indexers/image_indexer.rb @@ -10,7 +10,7 @@ class ImageIndexer < Hyrax::WorkIndexer include Hyrax::IndexesLinkedMetadata # Use thumbnails served by RIIIF - self.thumbnail_path_service = IIIFThumbnailPathService + self.thumbnail_path_service = IIIFWorkThumbnailPathService # Uncomment this block if you want to add custom indexing behavior: # def generate_solr_document diff --git a/app/indexers/work_indexer.rb b/app/indexers/work_indexer.rb index 823331835..8ea575d95 100644 --- a/app/indexers/work_indexer.rb +++ b/app/indexers/work_indexer.rb @@ -8,7 +8,7 @@ class WorkIndexer < Hyrax::WorkIndexer include Hyrax::IndexesLinkedMetadata # Use thumbnails served by RIIIF - self.thumbnail_path_service = IIIFThumbnailPathService + self.thumbnail_path_service = IIIFWorkThumbnailPathService # Uncomment this block if you want to add custom indexing behavior: # def generate_solr_document diff --git a/app/models/collection.rb b/app/models/collection.rb index 87e711661..8bccb7aba 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -3,5 +3,5 @@ class Collection < ActiveFedora::Base include ::Hyrax::CollectionBehavior # You can replace these metadata if they're not suitable include Hyrax::BasicMetadata - self.indexer = Hyrax::CollectionWithBasicMetadataIndexer + self.indexer = CollectionIndexer end diff --git a/app/services/concerns/iiif_thumbnail_paths.rb b/app/services/concerns/iiif_thumbnail_paths.rb new file mode 100644 index 000000000..e33ccc353 --- /dev/null +++ b/app/services/concerns/iiif_thumbnail_paths.rb @@ -0,0 +1,29 @@ +module IIIFThumbnailPaths + extend ActiveSupport::Concern + + class_methods do + # @param [FileSet] file_set + # @param [String] size ('!150,300') an IIIF image size defaults to an image no + # wider than 150px and no taller than 300px + # @return the IIIF url for the thumbnail if it's an image, otherwise gives + # the thumbnail download path + def thumbnail_path(file_set, size = '!150,300'.freeze) + return super(file_set) unless file_set.image? + iiif_thumbnail_path(file_set, size) + end + + # @private + def iiif_thumbnail_path(file_set, size) + file = file_set.original_file + return unless file + Riiif::Engine.routes.url_helpers.image_path( + file.id, + size: size + ) + end + + def thumbnail?(_thumbnail) + true + end + end +end diff --git a/app/services/iiif_collection_thumbnail_path_service.rb b/app/services/iiif_collection_thumbnail_path_service.rb new file mode 100644 index 000000000..b5634f59e --- /dev/null +++ b/app/services/iiif_collection_thumbnail_path_service.rb @@ -0,0 +1,8 @@ +class IIIFCollectionThumbnailPathService < Hyrax::CollectionThumbnailPathService + include IIIFThumbnailPaths + + # The image to use if no thumbnail has been selected + def self.default_image + ActionController::Base.helpers.image_path 'collection.png' + end +end diff --git a/app/services/iiif_thumbnail_path_service.rb b/app/services/iiif_thumbnail_path_service.rb deleted file mode 100644 index 5e848f213..000000000 --- a/app/services/iiif_thumbnail_path_service.rb +++ /dev/null @@ -1,30 +0,0 @@ -class IIIFThumbnailPathService < Hyrax::WorkThumbnailPathService - class << self - protected - - # @param [FileSet] file_set - # @param [String] size ('!150,300') an IIIF image size defaults to an image no - # wider than 150px and no taller than 300px - # @return the IIIF url for the thumbnail if it's an image, otherwise gives - # the thumbnail download path - def thumbnail_path(file_set, size = '!150,300'.freeze) - return super(file_set) unless file_set.image? - iiif_thumbnail_path(file_set, size) - end - - # @private - def iiif_thumbnail_path(file_set, size) - file = file_set.original_file - return unless file - Riiif::Engine.routes.url_helpers.image_path( - file.id, - size: size - ) - end - - def thumbnail?(_thumbnail) - true - end - end - private_class_method :iiif_thumbnail_path -end diff --git a/app/services/iiif_work_thumbnail_path_service.rb b/app/services/iiif_work_thumbnail_path_service.rb new file mode 100644 index 000000000..149f0a2e3 --- /dev/null +++ b/app/services/iiif_work_thumbnail_path_service.rb @@ -0,0 +1,3 @@ +class IIIFWorkThumbnailPathService < Hyrax::WorkThumbnailPathService + include IIIFThumbnailPaths +end diff --git a/spec/indexers/collection_indexer_spec.rb b/spec/indexers/collection_indexer_spec.rb new file mode 100644 index 000000000..c79feb50b --- /dev/null +++ b/spec/indexers/collection_indexer_spec.rb @@ -0,0 +1,9 @@ +require 'spec_helper' + +RSpec.describe CollectionIndexer do + describe ".thumbnail_path_service" do + subject { described_class.thumbnail_path_service } + + it { is_expected.to eq IIIFCollectionThumbnailPathService } + end +end diff --git a/spec/indexers/file_set_indexer_spec.rb b/spec/indexers/file_set_indexer_spec.rb index 57725a829..472414638 100644 --- a/spec/indexers/file_set_indexer_spec.rb +++ b/spec/indexers/file_set_indexer_spec.rb @@ -1,6 +1,6 @@ RSpec.describe FileSetIndexer do describe 'thumbnail_path_service' do subject { described_class.thumbnail_path_service } - it { is_expected.to eq IIIFThumbnailPathService } + it { is_expected.to eq IIIFWorkThumbnailPathService } end end diff --git a/spec/indexers/work_indexer_spec.rb b/spec/indexers/work_indexer_spec.rb index fd9363237..90fa5414b 100644 --- a/spec/indexers/work_indexer_spec.rb +++ b/spec/indexers/work_indexer_spec.rb @@ -1,7 +1,7 @@ RSpec.describe WorkIndexer do describe 'thumbnail_path_service' do subject { described_class.thumbnail_path_service } - it { is_expected.to eq IIIFThumbnailPathService } + it { is_expected.to eq IIIFWorkThumbnailPathService } end describe 'rdf_service' do diff --git a/spec/models/collection_spec.rb b/spec/models/collection_spec.rb index 32f182d5b..6c58fd71a 100644 --- a/spec/models/collection_spec.rb +++ b/spec/models/collection_spec.rb @@ -4,4 +4,10 @@ it "is a hyrax collection" do expect(described_class.ancestors).to include Hyrax::CollectionBehavior end + + describe ".indexer" do + subject { described_class.indexer } + + it { is_expected.to eq CollectionIndexer } + end end diff --git a/spec/services/iiif_collection_thumbnail_path_service_spec.rb b/spec/services/iiif_collection_thumbnail_path_service_spec.rb new file mode 100644 index 000000000..9a3d733c4 --- /dev/null +++ b/spec/services/iiif_collection_thumbnail_path_service_spec.rb @@ -0,0 +1,38 @@ +RSpec.describe IIIFCollectionThumbnailPathService do + let(:file_set) { FileSet.new } + let(:file) do + double(id: 's1/78/4k/72/s1784k724/files/6185235a-79b2-4c29-8c24-4d6ad9b11470', + mime_type: 'image/jpeg') + end + + before do + allow(ActiveFedora::Base).to receive(:find).with('s1784k724').and_return(file_set) + allow(file_set).to receive_messages(original_file: file, id: 's1784k724') + # https://github.com/projecthydra/active_fedora/issues/1251 + allow(file_set).to receive(:persisted?).and_return(true) + end + + context "on a collection" do + subject { described_class.call(collection) } + + context "with a thumbnail" do + let(:collection) { build(:collection, thumbnail_id: 's1784k724') } + + it { is_expected.to eq '/images/s1%2F78%2F4k%2F72%2Fs1784k724%2Ffiles%2F6185235a-79b2-4c29-8c24-4d6ad9b11470/full/!150,300/0/default.jpg' } + end + + context "without a thumbnail" do + let(:collection) { build(:collection) } + + it { is_expected.to start_with '/assets/collection-' } + end + end + + context "on a file set" do + subject { described_class.call(file_set) } + + context "with an image" do + it { is_expected.to eq '/images/s1%2F78%2F4k%2F72%2Fs1784k724%2Ffiles%2F6185235a-79b2-4c29-8c24-4d6ad9b11470/full/!150,300/0/default.jpg' } + end + end +end diff --git a/spec/services/iiif_thumbnail_path_service_spec.rb b/spec/services/iiif_work_thumbnail_path_service_spec.rb similarity index 96% rename from spec/services/iiif_thumbnail_path_service_spec.rb rename to spec/services/iiif_work_thumbnail_path_service_spec.rb index cb3461898..3c2df0c6b 100644 --- a/spec/services/iiif_thumbnail_path_service_spec.rb +++ b/spec/services/iiif_work_thumbnail_path_service_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe IIIFThumbnailPathService do +RSpec.describe IIIFWorkThumbnailPathService do let(:file_set) { FileSet.new } let(:file) do double(id: 's1/78/4k/72/s1784k724/files/6185235a-79b2-4c29-8c24-4d6ad9b11470',