Class: Blacklight::DocumentPresenter

Inherits:
Object
  • Object
show all
Defined in:
app/presenters/blacklight/document_presenter.rb

Overview

An abstract class that the view presenters for SolrDocuments descend from

Direct Known Subclasses

IndexPresenter, ShowPresenter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, view_context, configuration = view_context.blacklight_config, view_config: nil, field_presenter_options: {}) ⇒ DocumentPresenter

Returns a new instance of DocumentPresenter.

Parameters:

  • document (SolrDocument)
  • view_context (ActionView::Base)

    scope for linking and generating urls

  • configuration (Blacklight::Configuration) (defaults to: view_context.blacklight_config)


14
15
16
17
18
19
20
# File 'app/presenters/blacklight/document_presenter.rb', line 14

def initialize(document, view_context, configuration = view_context.blacklight_config, view_config: nil, field_presenter_options: {})
  @document = document
  @view_context = view_context
  @configuration = configuration
  @view_config = view_config
  @field_presenter_options = field_presenter_options
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



6
7
8
# File 'app/presenters/blacklight/document_presenter.rb', line 6

def configuration
  @configuration
end

#documentObject (readonly)

Returns the value of attribute document.



6
7
8
# File 'app/presenters/blacklight/document_presenter.rb', line 6

def document
  @document
end

#view_contextObject (readonly)

Returns the value of attribute view_context.



6
7
8
# File 'app/presenters/blacklight/document_presenter.rb', line 6

def view_context
  @view_context
end

Instance Method Details

#display_type(base_name = nil, default: nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/presenters/blacklight/document_presenter.rb', line 72

def display_type(base_name = nil, default: nil)
  fields = []
  fields += Array.wrap(view_config[:"#{base_name}_display_type_field"]) if base_name && view_config.key?(:"#{base_name}_display_type_field")
  fields += Array.wrap(view_config.display_type_field)

  if fields.empty? && show_view_config != view_config
    fields += Array.wrap(show_view_config[:"#{base_name}_display_type_field"]) if base_name && show_view_config.key?(:"#{base_name}_display_type_field")
    fields += Array.wrap(show_view_config.display_type_field)
  end

  fields += ['format'] if fields.empty? # backwards compatibility with the old default value for display_type_field

  display_type = fields.lazy.map { |field| field_presenter(display_fields[field] || Configuration::NullDisplayField.new(field)) }.detect(&:any?)&.values
  display_type || Array(default)
end

#field_presenters(document_fields = fields, **kwargs) ⇒ Object



35
36
37
38
39
# File 'app/presenters/blacklight/document_presenter.rb', line 35

def field_presenters(document_fields = fields, **kwargs)
  return to_enum(:field_presenters, document_fields, **kwargs) unless block_given?

  fields_to_render(document_fields, **kwargs).each { |_, _, config| yield config }
end

#field_value(field_config, options = {}) ⇒ Object

Render the field label for a document

Allow an extention point where information in the document may drive the value of the field

Parameters:

Options Hash (options):

  • :value (String)


96
97
98
# File 'app/presenters/blacklight/document_presenter.rb', line 96

def field_value field_config, options = {}
  field_presenter(field_config, options).render
end

#fields_to_render(document_fields = fields, **kwargs) ⇒ Hash<String,Configuration::Field>

Returns all the fields for this index view that should be rendered.

Returns:

  • (Hash<String,Configuration::Field>)

    all the fields for this index view that should be rendered



23
24
25
26
27
28
29
30
31
32
33
# File 'app/presenters/blacklight/document_presenter.rb', line 23

def fields_to_render(document_fields = fields, **kwargs)
  return to_enum(:fields_to_render, document_fields, **kwargs) unless block_given?

  document_fields.each do |name, field_config|
    field_presenter = field_presenter(field_config, kwargs)

    next unless field_presenter.render_field? && field_presenter.any?

    yield name, field_config, field_presenter
  end
end

#headingString

Get the value of the document’s “title” field, or a placeholder value (if empty)

Returns:

  • (String)


46
47
48
49
50
51
52
# File 'app/presenters/blacklight/document_presenter.rb', line 46

def heading
  return field_value(view_config.title_field) if view_config.title_field.is_a? Blacklight::Configuration::Field

  fields = Array.wrap(view_config.title_field) + [configuration.document_model.unique_key]
  f = fields.lazy.map { |field| field_config(field) }.detect { |field_config| field_presenter(field_config).any? }
  f ? field_value(f, except_operations: [Rendering::HelperMethod]) : ""
end

#html_titleString

Get the document’s “title” to display in the <title> element. (by default, use the #document_heading)

Returns:

  • (String)

See Also:

  • #document_heading


60
61
62
63
64
65
66
67
68
69
70
# File 'app/presenters/blacklight/document_presenter.rb', line 60

def html_title
  return field_value(view_config.html_title_field) if view_config.html_title_field.is_a? Blacklight::Configuration::Field

  if view_config.html_title_field
    fields = Array.wrap(view_config.html_title_field) + [configuration.document_model.unique_key]
    f = fields.lazy.map { |field| field_config(field) }.detect { |field_config| field_presenter(field_config).any? }
    field_value(f)
  else
    heading
  end
end

#inspectObject



128
129
130
131
# File 'app/presenters/blacklight/document_presenter.rb', line 128

def inspect
  fields = "document:#{document.inspect}"
  "#<#{self.class.name}:#{object_id} #{fields}>"
end

Create <link rel=“alternate”> links from a documents dynamically provided export formats. Returns empty string if no links available.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :unique (Boolean)

    ensures only one link is output for every content type, e.g. as required by atom

  • :exclude (Array<String>)

    array of format shortnames to not include in the output



116
117
118
# File 'app/presenters/blacklight/document_presenter.rb', line 116

def link_rel_alternates(options = {})
  LinkAlternatePresenter.new(view_context, document, options).render
end

#show_view_configObject



124
125
126
# File 'app/presenters/blacklight/document_presenter.rb', line 124

def show_view_config
  configuration.view_config(:show, action_name: view_context.action_name)
end

#thumbnailObject



104
105
106
# File 'app/presenters/blacklight/document_presenter.rb', line 104

def thumbnail
  @thumbnail ||= thumbnail_presenter_class.new(document, view_context, view_config)
end

#thumbnail_presenter_classObject



100
101
102
# File 'app/presenters/blacklight/document_presenter.rb', line 100

def thumbnail_presenter_class
  view_config.thumbnail_presenter || thumbnail_presenter
end

#view_configObject



120
121
122
# File 'app/presenters/blacklight/document_presenter.rb', line 120

def view_config
  @view_config ||= show_view_config
end