Class: Hyrax::PresenterFactory

Inherits:
Object
  • Object
show all
Defined in:
app/presenters/hyrax/presenter_factory.rb

Overview

TODO:

Extract a SolrDocument finder class that takes a list of pids and returns/yields a ::SolrDocument for each hit in SOLR.

Note:

The given IDs are loaded from SOLR

Responsible building an Array of presenters based on IDs and presenter class given

Direct Known Subclasses

IiifManifestPresenter::Factory

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ids:, presenter_class:, presenter_args:) ⇒ PresenterFactory

Returns a new instance of PresenterFactory.


22
23
24
25
26
27
# File 'app/presenters/hyrax/presenter_factory.rb', line 22

def initialize(ids:, presenter_class:, presenter_args:)
  @ids = ids
  @presenter_class = presenter_class
  # In moving from splat args to named parameters, passing the parameters is a bit off.
  @presenter_args = presenter_args.nil? ? [nil] : presenter_args
end

Instance Attribute Details

#idsObject (readonly)

Returns the value of attribute ids.


20
21
22
# File 'app/presenters/hyrax/presenter_factory.rb', line 20

def ids
  @ids
end

#presenter_argsObject (readonly)

Returns the value of attribute presenter_args.


20
21
22
# File 'app/presenters/hyrax/presenter_factory.rb', line 20

def presenter_args
  @presenter_args
end

#presenter_classObject (readonly)

Returns the value of attribute presenter_class.


20
21
22
# File 'app/presenters/hyrax/presenter_factory.rb', line 20

def presenter_class
  @presenter_class
end

Class Method Details

.build_for(ids:, presenter_class:, presenter_args: []) ⇒ Array

TODO:

Convert to find and yield only the found SOLR documents. There is a coupling of knowledge regarding the building of the presenter class and its parameters.

TODO:

We are looping through SOLR docs three times; Can this be compressed into a single loop

Returns presenters for the documents in order of the ids (as given).

Parameters:

  • ids (Array)

    the list of ids to load

  • presenter_class (Class)

    the class of presenter to make

  • presenter_args (Array) (defaults to: [])

    any other arguments to pass to the presenters

Returns:

  • (Array)

    presenters for the documents in order of the ids (as given)


15
16
17
# File 'app/presenters/hyrax/presenter_factory.rb', line 15

def build_for(ids:, presenter_class:, presenter_args: [])
  new(ids: ids, presenter_class: presenter_class, presenter_args: presenter_args).build
end

Instance Method Details

#buildObject


29
30
31
32
33
34
35
36
# File 'app/presenters/hyrax/presenter_factory.rb', line 29

def build
  return [] if ids.blank?
  docs = load_docs
  ids.map do |id|
    solr_doc = docs.find { |doc| doc.id == id }
    presenter_class.new(solr_doc, *presenter_args) if solr_doc
  end.compact
end