10000 Allow the `view_config` to be passed into the `document_presenter` helper and `DocumentComponent` constructor by cbeer · Pull Request #3343 · projectblacklight/blacklight · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Allow the view_config to be passed into the document_presenter helper and DocumentComponent constructor #3343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions app/components/blacklight/document_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ 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)

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

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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]
Expand All @@ -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

Expand All @@ -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|
Expand All @@ -137,6 +138,12 @@ def before_render
end
end

def view_config
@view_config ||= presenter&.view_config || Blacklight::Configuration::ViewConfig.new
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there really a case where presenter is nil? Wouldn't we also need to deal with that in the delegate :field_presenters?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. Someone thought so in all the renders_one blocks above, but not in the before_render.

end

delegate :field_presenters, to: :presenter

private

attr_reader :document_counter, :presenter, :view_partials
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/blacklight/document_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

##
Expand Down
36 changes: 36 additions & 0 deletions spec/helpers/blacklight/document_helper_behavior_spec.rb
Original file line number Diff line number Diff line change
@@ -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
0