Skip to content
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

Allow field presenter arguments to get passed through the document presenter #3342

Merged
merged 1 commit into from
Sep 27, 2024
Merged
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
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Metrics/ClassLength:
- "lib/blacklight/search_builder.rb"
- "lib/blacklight/search_state.rb"
- "lib/blacklight/search_state/filter_field.rb"
- "app/presenters/blacklight/document_presenter.rb"

Layout/LineLength:
Max: 200
Expand Down
39 changes: 29 additions & 10 deletions app/presenters/blacklight/document_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,48 @@ class DocumentPresenter
# @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)
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 = field_presenter_options
end

# @return [Hash<String,Configuration::Field>] all the fields for this index view that should be rendered
def fields_to_render
return to_enum(:fields_to_render) unless block_given?
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?

fields.each do |name, field_config|
field_presenter = field_presenter(field_config)
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

def field_presenters
return to_enum(:field_presenters) unless block_given?
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')

fields_to_render.each { |_, _, config| yield config }
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

##
Expand Down Expand Up @@ -122,7 +141,7 @@ def view_config
end

def show_view_config
configuration.view_config(:show)
configuration.view_config(:show, action_name: view_context.action_name)
end

def inspect
Expand Down Expand Up @@ -158,7 +177,7 @@ def field_presenter(field_config, options = {})
end

def field_presenter_options
{}
@field_presenter_options ||= {}
end
end
end
2 changes: 1 addition & 1 deletion app/presenters/blacklight/index_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def label(field_or_string_or_proc, opts = {})
deprecation_deprecate label: 'Use #heading'

def view_config
@view_config ||= configuration.view_config(view_context.document_index_view_type)
@view_config ||= configuration.view_config(view_context.document_index_view_type, action_name: view_context.action_name)
end

private
Expand Down
1 change: 1 addition & 0 deletions spec/presenters/blacklight/document_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

before do
allow(request_context).to receive(:search_state).and_return(search_state)
allow(request_context).to receive(:action_name).and_return(:show)
end

describe '#fields_to_render' do
Expand Down
1 change: 1 addition & 0 deletions spec/presenters/blacklight/index_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

before do
allow(request_context).to receive(:search_state).and_return(search_state)
allow(request_context).to receive(:action_name).and_return(:index)
end

describe '#fields' do
Expand Down
1 change: 1 addition & 0 deletions spec/presenters/blacklight/show_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

before do
allow(request_context).to receive(:search_state).and_return(search_state)
allow(request_context).to receive(:action_name).and_return(:show)
end

describe "link_rel_alternates" do
Expand Down
Loading