Skip to content

Commit

Permalink
handle special case for Hyrax::Resource
Browse files Browse the repository at this point in the history
Using naming convention, Hyrax::Resource would be indexed by Hyrax::ResourceIndexer.  This class exists but should not be used to index instances of Hyrax::Resource.  Instead the default indexer Valkyrie::Indexer should be used.
  • Loading branch information
elrayle authored and Tom Johnson committed Sep 10, 2020
1 parent 57d3a89 commit b1c3624
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions app/indexers/hyrax/valkyrie_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,6 @@ class ValkyrieIndexer
# @return [Valkyrie::Resource]
attr_reader :resource

##
# @api public
# @param [Valkyrie::Resource] resource
#
# @return [#to_solr]
def self.for(resource:)
indexer_class = "#{resource.class.name}Indexer".safe_constantize || ValkyrieIndexer

indexer_class.new(resource: resource)
end

##
# @api private
# @param [Valkyrie::Resource] resource
Expand All @@ -83,5 +72,30 @@ def to_solr
"updated_at_dtsi": resource.updated_at
}
end

class << self
##
# @api public
# @param resource [Valkyrie::Resource] an instance of a Valkyrie::Resource or an inherited class
# @note This will attempt to return an indexer following a naming convention where the indexer for a resource
# class is expected to be the class name appended with 'Indexer'. It will return default ValkyrieIndexer
# if an indexer following the naming convention is not found or the resource is the reserved Hyrax::Resource
# which should use ValkyrieIndexer instead of the base implementation in Hyrax::ResourceIndexer.
# @example
# resource class: Book
# indexer class: BookIndexer
# @return [Valkyrie::Indexer] an instance of ValkyrieIndexer or an inherited class based on naming convention
#
def for(resource:)
indexer_class = resource.class.name == 'Hyrax::Resource' ? ValkyrieIndexer : indexer_from_classname(resource)
indexer_class.new(resource: resource)
end

private

def indexer_from_classname(resource)
"#{resource.class.name}Indexer".safe_constantize || ValkyrieIndexer
end
end
end
end

0 comments on commit b1c3624

Please sign in to comment.