Sha256: 6e95ae8a61ca2eec61b23165f178b03dd98e97aecb44b869e66ec160c10a76e7

Contents?: true

Size: 1.26 KB

Versions: 7

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module Blacklight
  # An abstract class that the view presenters for SolrDocuments descend from
  class DocumentPresenter
    attr_reader :document, :configuration, :view_context

    # @return [Hash<String,Configuration::Field>]  all the fields for this index view that should be rendered
    def fields_to_render
      fields.select do |_name, field_config|
        # rubocop:disable Style/PreferredHashMethods
        render_field?(field_config) && has_value?(field_config)
        # rubocop:enable Style/PreferredHashMethods
      end
    end

    private

    ##
    # Check to see if the given field should be rendered in this context
    # @param [Blacklight::Configuration::Field] field_config
    # @return [Boolean]
    def render_field?(field_config)
      view_context.should_render_field?(field_config, document)
    end

    ##
    # Check if a document has (or, might have, in the case of accessor methods) a value for
    # the given solr field
    # @param [Blacklight::Configuration::Field] field_config
    # @return [Boolean]
    def has_value? field_config
      document.has?(field_config.field) ||
        (document.has_highlight_field? field_config.field if field_config.highlight) ||
        field_config.accessor
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
blacklight-7.3.0 app/presenters/blacklight/document_presenter.rb
blacklight-7.2.0 app/presenters/blacklight/document_presenter.rb
blacklight-7.1.0 app/presenters/blacklight/document_presenter.rb
blacklight-7.1.0.alpha app/presenters/blacklight/document_presenter.rb
blacklight-7.0.1 app/presenters/blacklight/document_presenter.rb
blacklight-7.0.0 app/presenters/blacklight/document_presenter.rb
blacklight-7.0.0.rc2 app/presenters/blacklight/document_presenter.rb