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
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
115 116 117 |
# File 'app/presenters/blacklight/document_presenter.rb', line 115 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 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 |
#heading ⇒ String
Get the value of the document’s “title” field, or a placeholder value (if empty)
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_title ⇒ String
Get the document’s “title” to display in the <title> element. (by default, use the #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 |
#inspect ⇒ Object
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 |
#link_rel_alternates(options = {}) ⇒ Object
Create <link rel=“alternate”> links from a documents dynamically provided export formats. Returns empty string if no links available.
135 136 137 |
# File 'app/presenters/blacklight/document_presenter.rb', line 135 def link_rel_alternates( = {}) LinkAlternatePresenter.new(view_context, document, ).render end |
#show_view_config ⇒ Object
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 |
#thumbnail ⇒ Object
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_class ⇒ Object
119 120 121 |
# File 'app/presenters/blacklight/document_presenter.rb', line 119 def thumbnail_presenter_class view_config.thumbnail_presenter || thumbnail_presenter end |
#view_config ⇒ Object
139 140 141 |
# File 'app/presenters/blacklight/document_presenter.rb', line 139 def view_config @view_config ||= show_view_config end |