Class: Blacklight::DocumentPresenter
- Inherits:
-
Object
- Object
- Blacklight::DocumentPresenter
- Defined in:
- app/presenters/blacklight/document_presenter.rb
Overview
An abstract class that the view presenters for SolrDocuments descend from
Direct Known Subclasses
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#view_context ⇒ Object
readonly
Returns the value of attribute view_context.
Instance Method Summary collapse
- #display_type(base_name = nil, default: nil) ⇒ Object
- #field_presenters(document_fields = fields, **kwargs) ⇒ Object
-
#field_value(field_config, options = {}) ⇒ Object
Render the field label for a document.
-
#fields_to_render(document_fields = fields, **kwargs) ⇒ Hash<String,Configuration::Field>
All the fields for this index view that should be rendered.
-
#heading ⇒ String
Get the value of the document’s “title” field, or a placeholder value (if empty).
-
#html_title ⇒ String
Get the document’s “title” to display in the <title> element.
-
#initialize(document, view_context, configuration = view_context.blacklight_config, view_config: nil, field_presenter_options: {}) ⇒ DocumentPresenter
constructor
A new instance of DocumentPresenter.
- #inspect ⇒ Object
-
#link_rel_alternates(options = {}) ⇒ Object
Create <link rel=“alternate”> links from a documents dynamically provided export formats.
- #show_view_config ⇒ Object
- #thumbnail ⇒ Object
- #thumbnail_presenter_class ⇒ Object
- #view_config ⇒ Object
Constructor Details
#initialize(document, view_context, configuration = view_context.blacklight_config, view_config: nil, field_presenter_options: {}) ⇒ DocumentPresenter
Returns a new instance of DocumentPresenter.
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 = end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
6 7 8 |
# File 'app/presenters/blacklight/document_presenter.rb', line 6 def configuration @configuration end |
#document ⇒ Object (readonly)
Returns the value of attribute document.
6 7 8 |
# File 'app/presenters/blacklight/document_presenter.rb', line 6 def document @document end |
#view_context ⇒ Object (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
96 97 98 |
# File 'app/presenters/blacklight/document_presenter.rb', line 96 def field_value field_config, = {} field_presenter(field_config, ).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.
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 |
#heading ⇒ String
Get the value of the document’s “title” field, or a placeholder value (if empty)
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_title ⇒ String
Get the document’s “title” to display in the <title> element. (by default, use the #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 |
#inspect ⇒ Object
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 |
#link_rel_alternates(options = {}) ⇒ Object
Create <link rel=“alternate”> links from a documents dynamically provided export formats. Returns empty string if no links available.
116 117 118 |
# File 'app/presenters/blacklight/document_presenter.rb', line 116 def link_rel_alternates( = {}) LinkAlternatePresenter.new(view_context, document, ).render end |
#show_view_config ⇒ Object
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 |
#thumbnail ⇒ Object
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_class ⇒ Object
100 101 102 |
# File 'app/presenters/blacklight/document_presenter.rb', line 100 def thumbnail_presenter_class view_config.thumbnail_presenter || thumbnail_presenter end |
#view_config ⇒ Object
120 121 122 |
# File 'app/presenters/blacklight/document_presenter.rb', line 120 def view_config @view_config ||= show_view_config end |