app/presenters/blacklight/document_presenter.rb in blacklight-7.40.0 vs app/presenters/blacklight/document_presenter.rb in blacklight-8.0.0.beta1
- old
+ new
@@ -9,52 +9,33 @@
self.thumbnail_presenter = ThumbnailPresenter
# @param [SolrDocument] document
# @param [ActionView::Base] view_context scope for linking and generating urls
# @param [Blacklight::Configuration] configuration
- def initialize(document, view_context, configuration = view_context.blacklight_config, view_config: nil, field_presenter_options: {})
+ def initialize(document, view_context, configuration = view_context.blacklight_config)
@document = document
@view_context = view_context
@configuration = configuration
- @view_config = view_config
- @field_presenter_options = field_presenter_options
end
# @return [Hash<String,Configuration::Field>] all the fields for this index view that should be rendered
- 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?
+ def fields_to_render
+ return to_enum(:fields_to_render) unless block_given?
- Deprecation.warn(self.class, 'In Blacklight 8, Blacklight::DocumentPresenter#fields_to_render accepts additional arguments')
+ fields.each do |name, field_config|
+ field_presenter = field_presenter(field_config)
- 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
- def field_presenters(document_fields = fields, **kwargs)
- unless block_given?
- return to_enum(:field_presenters, document_fields, **kwargs) unless method(:field_presenters).arity.zero?
+ def field_presenters
+ return to_enum(:field_presenters) unless block_given?
- 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
+ fields_to_render.each { |_, _, config| yield config }
end
##
# Get the value of the document's "title" field, or a placeholder
# value (if empty)
@@ -96,14 +77,12 @@
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 || []
+ display_type = fields.lazy.map { |field| field_presenter(display_fields[field] || Configuration::NullDisplayField.new(field)) }.detect(&:any?)&.values
+ display_type || Array(default)
end
##
# Render the field label for a document
#
@@ -135,49 +114,43 @@
def link_rel_alternates(options = {})
LinkAlternatePresenter.new(view_context, document, options).render
end
def view_config
- @view_config ||= show_view_config
+ show_view_config
end
def show_view_config
- configuration.view_config(:show, action_name: view_context.action_name)
+ configuration.view_config(:show)
end
def inspect
fields = "document:#{document.inspect}"
"#<#{self.class.name}:#{object_id} #{fields}>"
end
private
- def render_field?(field_config)
- field_presenter(field_config).render_field?
+ # @return [Hash<String,Configuration::Field>]
+ def fields
+ @fields ||= Array(display_type).inject(display_fields) do |fields, display_type|
+ fields.merge(display_fields(configuration.for_display_type(display_type)))
+ end
end
- deprecation_deprecate render_field?: 'Use FieldPresenter#render_field?'
- def has_value?(field_config)
- field_presenter(field_config).any?
+ def display_fields(config = configuration)
+ config[view_config.document_fields_key || :index_fields]
end
- deprecation_deprecate has_value?: 'Use FieldPresenter#any?'
- def field_values(field_config, options = {})
- field_value(field_config, options)
+ def field_config(field)
+ fields.fetch(field) { Configuration::NullDisplayField.new(field) }
end
- deprecation_deprecate field_values: 'Use #field_value'
- def retrieve_values(field_config)
- field_presenter(field_config).values
- end
- deprecation_deprecate retrieve_values: 'Use FieldPresenter#values'
-
def field_presenter(field_config, options = {})
- presenter_class = field_config.presenter || Blacklight::FieldPresenter
- presenter_class.new(view_context, document, field_config, options.merge(field_presenter_options))
+ field_config.presenter.new(view_context, document, field_config, field_presenter_options.merge(options))
end
def field_presenter_options
- @field_presenter_options ||= {}
+ {}
end
end
end