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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/presenters/blacklight/document_presenter.rb', line 89

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(field_config(field)) }.detect(&:any?)&.values
  display_type ||= Array(default) if default

  display_type || []
end

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/presenters/blacklight/document_presenter.rb', line 41

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

    Deprecation.warn(self.class, 'In Blacklight 8, Blacklight::DocumentPresenter#field_presenters accepts additional arguments')

    return to_enum(:field_presenters)
  end

  if method(:fields_to_render).arity.zero?
    Deprecation.warn(self.class, 'In Blacklight 8, Blacklight::DocumentPresenter#fields_to_render accept additional arguments')
    fields_to_render.each { |_, _, config| yield config }
  else
    fields_to_render(document_fields, **kwargs).each { |_, _, config| yield config }
  end
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)


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

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
34
35
36
37
38
39
# File 'app/presenters/blacklight/document_presenter.rb', line 23

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

    Deprecation.warn(self.class, 'In Blacklight 8, Blacklight::DocumentPresenter#fields_to_render accepts additional arguments')

    return to_enum(:fields_to_render)
  end

  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)


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

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


77
78
79
80
81
82
83
84
85
86
87
# File 'app/presenters/blacklight/document_presenter.rb', line 77

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



147
148
149
150
# File 'app/presenters/blacklight/document_presenter.rb', line 147

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



135
136
137
# File 'app/presenters/blacklight/document_presenter.rb', line 135

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

#show_view_configObject



143
144
145
# File 'app/presenters/blacklight/document_presenter.rb', line 143

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

#thumbnailObject



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

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

#thumbnail_presenter_classObject



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

def thumbnail_presenter_class
  view_config.thumbnail_presenter || thumbnail_presenter
end

#view_configObject



139
140
141
# File 'app/presenters/blacklight/document_presenter.rb', line 139

def view_config
  @view_config ||= show_view_config
end