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

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.



33
34
35
36
37
38
# File 'app/presenters/hyrax/presenter_factory.rb', line 33

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.



29
30
31
# File 'app/presenters/hyrax/presenter_factory.rb', line 29

def ids
  @ids
end

#presenter_argsObject (readonly)

Returns the value of attribute presenter_args.



29
30
31
# File 'app/presenters/hyrax/presenter_factory.rb', line 29

def presenter_args
  @presenter_args
end

#presenter_classObject (readonly) Also known as: klass

Returns the value of attribute presenter_class.



29
30
31
# File 'app/presenters/hyrax/presenter_factory.rb', line 29

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)



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

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

.build_presenters(ids, klass, *args) ⇒ Array

Deprecated.

use .build_for instead

Returns presenters for the documents in order of the ids.

Parameters:

  • ids (Array)

    the list of ids to load

  • klass (Class)

    the class of presenter to make

  • args (Array)

    any other arguments to pass to the presenters

Returns:

  • (Array)

    presenters for the documents in order of the ids



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

def build_presenters(ids, klass, *args)
  Deprecation.warn(self, "build_presenters is deprecated and will be removed from Hyrax 3.0 (use .build_for instead)")
  build_for(ids: ids, presenter_class: klass, presenter_args: args)
end

Instance Method Details

#buildObject



40
41
42
43
44
45
46
47
# File 'app/presenters/hyrax/presenter_factory.rb', line 40

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