Skip to content

Commit

Permalink
Merge pull request #320 from scientist-softserv/refactoring-configura…
Browse files Browse the repository at this point in the history
…tions

♻️ Remove conditionals and account for Valkyrie
  • Loading branch information
jeremyf authored Jan 23, 2024
2 parents 2f6efa2 + 1c3ea71 commit df59edf
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 71 deletions.
54 changes: 0 additions & 54 deletions app/indexers/concerns/iiif_print/child_indexer.rb

This file was deleted.

38 changes: 30 additions & 8 deletions app/indexers/concerns/iiif_print/file_set_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,41 @@ module FileSetIndexer
def self.decorate(base)
base.prepend(self)
base.class_attribute :iiif_print_lineage_service, default: IiifPrint::LineageService
decorate_index_document_method(base)
base
end

def generate_solr_document
super.tap do |solr_doc|
# only UV viewable images should have is_page_of, it is only used for iiif search
solr_doc['is_page_of_ssim'] = iiif_print_lineage_service.ancestor_ids_for(object) if object.mime_type&.match(/image/)
# index for full text search
solr_doc['all_text_timv'] = all_text
solr_doc['all_text_tsimv'] = all_text
solr_doc['digest_ssim'] = digest_from_content
##
# Decorate the appropriate indexing document based on the type of indexer; namely whether it
# responds to `#to_solr` or `#generate_solr_document`.
#
# @param base [Class]
# @return [Class]
def self.decorate_index_document_method(base)
method_names = []
method_names << :generate_solr_document if base.instance_methods.include?(:generate_solr_document)
method_names << :to_solr if base.instance_methods.include?(:to_solr)

raise "Unexpected class #{base} provided to #{self}.decorate" if method_names.blank?

method_names.each do |method_name|
# Providing these as wayfinding for searching projects:
#
# def to_solr
# def generate_solr_document
base.define_method(method_name) do |*args|
super(*args).tap do |solr_doc|
# only UV viewable images should have is_page_of, it is only used for iiif search
solr_doc['is_page_of_ssim'] = iiif_print_lineage_service.ancestor_ids_for(object) if object.mime_type&.match(/image/)
# index for full text search
solr_doc['all_text_timv'] = all_text
solr_doc['all_text_tsimv'] = all_text
solr_doc['digest_ssim'] = digest_from_content
end
end
end
end
private_class_method :decorate_index_document_method

private

Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/iiif_print/set_child_flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module SetChildFlag
end

def set_children
ordered_works.each do |child_work|
IiifPrint.object_ordered_works(self).each do |child_work|
child_work.update(is_child: true) unless child_work.is_child
end
end
Expand Down
21 changes: 17 additions & 4 deletions lib/iiif_print.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,23 @@ def self.model_configuration(**kwargs)
Module.new do
extend ActiveSupport::Concern

class_methods do
def iiif_print_config?
true
end
included do
work_type = self # In this case self is the class we're mixing the new module into.

# Ensure that the work_type and corresponding indexer are properly decorated for IiifPrint
indexer = if work_type < Valkyrie::Resource
IiifPrint::PersistenceLayer::ValkyrieAdapter.decorate_with_adapter_logic(work_type: work_type)
elsif work_type < ActiveFedora::Base
IiifPrint::PersistenceLayer::ActiveFedoraAdapter.decorate_with_adapter_logic(work_type: work_type)
else
raise "Unable to mix '.model_configuration' into #{work_type}"
end

# Deriving lineage of objects is a potentially complicated thing. We provide a default
# service but each work_type's indexer can be configured by amending it's
# {.iiif_print_lineage_service}.
indexer.class_attribute(:iiif_print_lineage_service, default: IiifPrint::LineageService) unless indexer.respond_to?(:iiif_print_lineage_service)
work_type::GeneratedResourceSchema.send(:include, IiifPrint::SetChildFlag) if work_type.const_defined?(:GeneratedResourceSchema)
end

# We don't know what you may want in your configuration, but from this gems implementation,
Expand Down
14 changes: 13 additions & 1 deletion lib/iiif_print/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,21 @@ class Engine < ::Rails::Engine
Hyrax::WorkShowPresenter.prepend(IiifPrint::WorkShowPresenterDecorator)
Hyrax::IiifHelper.prepend(IiifPrint::IiifHelperDecorator)

IiifPrint::ChildIndexer.decorate_work_types!
# The ActiveFedora::Base indexer for FileSets
IiifPrint::FileSetIndexer.decorate(Hyrax::FileSetIndexer)

if defined? Hyrax::Indexers::FileSetIndexer
# Newer versions of Hyrax favor `Hyrax::Indexers::FileSetIndexer` and deprecate
# `Hyrax::ValkyrieFileSetIndexer`.
IiifPrint::FileSetIndexer.decorate(Hyrax::Indexers::FileSetIndexer)
elsif defined? Hyrax::ValkyrieFileSetIndexer
# Versions 3.0+ of Hyrax have `Hyrax::ValkyrieFileSetIndexer` so we want to decorate that as
# well. We want to use the elsif construct because later on Hyrax::ValkyrieFileSetIndexer
# inherits from Hyrax::Indexers::FileSetIndexer and only implements:
# `def initialize(*args); super; end`
IiifPrint::FileSetIndexer.decorate(Hyrax::ValkyrieFileSetIndexer)
end

::BlacklightIiifSearch::IiifSearchResponse.prepend(IiifPrint::IiifSearchResponseDecorator)
::BlacklightIiifSearch::IiifSearchAnnotation.prepend(IiifPrint::BlacklightIiifSearch::AnnotationDecorator)
::BlacklightIiifSearch::IiifSearch.prepend(IiifPrint::IiifSearchDecorator)
Expand Down
1 change: 1 addition & 0 deletions lib/iiif_print/lineage_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module IiifPrint
#
# - {.ancestor_ids_for}
# - {.descendent_member_ids_for}
# - {.ancestor_identifier_for}
#
# The ancestor and descendent_file_sets are useful for ensuring we index together related items.
# For example, when I have a work that is a book, and one file set per page of that book, when I
Expand Down
2 changes: 1 addition & 1 deletion spec/iiif_print_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
describe ".model_configuration" do
context "default configuration" do
let(:model) do
Class.new do
Class.new(ActiveFedora::Base) do
include IiifPrint.model_configuration(pdf_split_child_model: Class.new)
end
end
Expand Down
7 changes: 5 additions & 2 deletions spec/support/iiif_print_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class FileSet < ActiveFedora::Base

class MyWork < ActiveFedora::Base
include ::Hyrax::WorkBehavior
include ::Hyrax::BasicMetadata
end

class MyWorkNeedsDerivative < ActiveFedora::Base
Expand All @@ -76,6 +77,8 @@ def members
end

class MyIiifConfiguredWorkWithAllDerivativeServices < ActiveFedora::Base
include ::Hyrax::WorkBehavior
include ::Hyrax::BasicMetadata
include IiifPrint.model_configuration

attr_accessor :title
Expand All @@ -85,6 +88,8 @@ def members
end

class MyIiifConfiguredWork < ActiveFedora::Base
include ::Hyrax::WorkBehavior
include ::Hyrax::BasicMetadata
include IiifPrint.model_configuration(
derivative_service_plugins: [FakeDerivativeService]
)
Expand All @@ -107,7 +112,6 @@ class NewspaperIssue < ActiveFedora::Base
# TODO: merge this in with whatever is needed from misc_shared.rb
class WorkWithIiifPrintConfig < ActiveFedora::Base
include ::Hyrax::WorkBehavior
include IiifPrint::SetChildFlag
include IiifPrint.model_configuration(pdf_split_child_model: WorkWithIiifPrintConfig)
include ::Hyrax::BasicMetadata

Expand All @@ -118,7 +122,6 @@ class WorkWithIiifPrintConfig < ActiveFedora::Base

class WorkWithOutConfig < ActiveFedora::Base
include ::Hyrax::WorkBehavior
include IiifPrint::SetChildFlag
include ::Hyrax::BasicMetadata

validates :title, presence: { message: 'Your work must have a title.' }
Expand Down

0 comments on commit df59edf

Please sign in to comment.