diff --git a/app/components/blacklight/document_component.rb b/app/components/blacklight/document_component.rb index 5f8f8e6e9a..4fff1636f8 100644 --- a/app/components/blacklight/document_component.rb +++ b/app/components/blacklight/document_component.rb @@ -34,7 +34,7 @@ class DocumentComponent < Blacklight::Component # The document title with some reasonable default behavior renders_one :title, (lambda do |*args, component: nil, **kwargs| - component ||= @presenter&.view_config&.title_component || Blacklight::DocumentTitleComponent + component ||= view_config.title_component || Blacklight::DocumentTitleComponent component.new(*args, counter: @counter, document: @document, presenter: @presenter, as: @title_component, actions: !@show, link_to_document: !@show, document_component: self, **kwargs) end) @@ -42,7 +42,7 @@ class DocumentComponent < Blacklight::Component renders_one :embed, (lambda do |static_content = nil, *args, component: nil, **kwargs| next static_content if static_content.present? - component ||= @presenter.view_config&.embed_component + component ||= view_config.embed_component next unless component @@ -53,7 +53,7 @@ class DocumentComponent < Blacklight::Component renders_one :metadata, (lambda do |static_content = nil, *args, component: nil, fields: nil, **kwargs| next static_content if static_content.present? - component ||= @presenter&.view_config&.metadata_component || Blacklight::DocumentMetadataComponent + component ||= view_config.metadata_component || Blacklight::DocumentMetadataComponent component.new(*args, fields: fields || @presenter&.field_presenters || [], **kwargs) end) @@ -64,7 +64,7 @@ class DocumentComponent < Blacklight::Component renders_one :thumbnail, (lambda do |image_options_or_static_content = {}, *args, component: nil, **kwargs| next image_options_or_static_content if image_options_or_static_content.is_a? String - component ||= @presenter&.view_config&.thumbnail_component || Blacklight::Document::ThumbnailComponent + component ||= view_config.thumbnail_component || Blacklight::Document::ThumbnailComponent component.new(*args, document: @document, presenter: @presenter, counter: @counter, image_options: image_options_or_static_content, **kwargs) end) @@ -91,7 +91,7 @@ class DocumentComponent < Blacklight::Component def initialize(document: nil, presenter: nil, partials: nil, id: nil, classes: [], component: :article, title_component: nil, counter: nil, document_counter: nil, counter_offset: 0, - show: false, **args) + show: false, view_config: nil, **args) Blacklight.deprecation.warn('the `presenter` argument to DocumentComponent#initialize is deprecated; pass the `presenter` in as document instead') if presenter @presenter = presenter || document || args[self.class.collection_parameter] @@ -108,6 +108,7 @@ def initialize(document: nil, presenter: nil, partials: nil, @counter ||= 1 + @document_counter + counter_offset if @document_counter.present? @show = show + @view_config = view_config end # rubocop:enable Metrics/ParameterLists @@ -124,7 +125,7 @@ def classes def before_render set_slot(:title, nil) unless title set_slot(:thumbnail, nil) unless thumbnail || show? - set_slot(:metadata, nil, fields: presenter.field_presenters, show: @show) unless metadata + set_slot(:metadata, nil, fields: field_presenters, show: @show) unless metadata set_slot(:embed, nil) unless embed if view_partials.present? view_partials.each do |view_partial| @@ -137,6 +138,12 @@ def before_render end end + def view_config + @view_config ||= presenter&.view_config || Blacklight::Configuration::ViewConfig.new + end + + delegate :field_presenters, to: :presenter + private attr_reader :document_counter, :presenter, :view_partials diff --git a/app/helpers/blacklight/document_helper_behavior.rb b/app/helpers/blacklight/document_helper_behavior.rb index f4355a8939..b5e6f7bf70 100644 --- a/app/helpers/blacklight/document_helper_behavior.rb +++ b/app/helpers/blacklight/document_helper_behavior.rb @@ -58,8 +58,8 @@ def bookmarked? document ## # Returns a document presenter for the given document - def document_presenter(document) - document_presenter_class(document).new(document, self) + def document_presenter(document, view_config: nil, **kwargs) + (view_config&.document_presenter_class || document_presenter_class(document)).new(document, self, view_config: view_config, **kwargs) end ## diff --git a/spec/helpers/blacklight/document_helper_behavior_spec.rb b/spec/helpers/blacklight/document_helper_behavior_spec.rb new file mode 100644 index 0000000000..419accdf15 --- /dev/null +++ b/spec/helpers/blacklight/document_helper_behavior_spec.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +RSpec.describe Blacklight::DocumentHelperBehavior do + before do + allow(helper).to receive(:blacklight_config).and_return(blacklight_config) + end + + let(:blacklight_config) { Blacklight::Configuration.new } + + describe '#document_presenter' do + subject { helper.document_presenter(document) } + + let(:document) { SolrDocument.new(id: '123') } + + it { is_expected.to be_a Blacklight::DocumentPresenter } + + context 'in a show context' do + before do + blacklight_config.show.document_presenter_class = Blacklight::ShowPresenter + + allow(helper).to receive(:action_name).and_return('show') + end + + it { is_expected.to be_a Blacklight::ShowPresenter } + end + + context 'with a provided view config' do + subject { helper.document_presenter(document, view_config: view_config) } + + let(:view_config) { Blacklight::Configuration::ViewConfig.new(document_presenter_class: stub_class) } + let(:stub_class) { stub_const('MyDocumentPresenter', Class.new(Blacklight::DocumentPresenter)) } + + it { is_expected.to be_a stub_class } + end + end +end