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

Use the DocumentMetadataComponent to render email + sms fields #2803

Merged
merged 7 commits into from
Feb 1, 2023
2 changes: 2 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,9 @@ Rails/HelperInstanceVariable:
Exclude:
- 'app/helpers/blacklight/blacklight_helper_behavior.rb'
- 'app/helpers/blacklight/catalog_helper_behavior.rb'
- 'app/helpers/blacklight/document_helper_behavior.rb'
- 'app/helpers/blacklight/component_helper_behavior.rb'
- 'app/helpers/blacklight/layout_helper_behavior.rb'
- 'app/helpers/blacklight/render_partials_helper_behavior.rb'

# Offense count: 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<dl class="document-metadata dl-invert row">
<% content = capture do %>
<% fields.each do |field| -%>
<%= field %>
<% end -%>
</dl>
<% end %>

<%= @tag.nil? ? content : tag.public_send(@tag, content, class: @classes, **@component_args) %>
10 changes: 8 additions & 2 deletions app/components/blacklight/document_metadata_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ class DocumentMetadataComponent < Blacklight::Component
with_collection_parameter :fields

# @param fields [Enumerable<Blacklight::FieldPresenter>] Document field presenters
def initialize(fields: [], show: false, view_type: nil)
# rubocop:disable Metrics/ParameterLists
def initialize(fields: [], tag: 'dl', classes: %w[document-metadata dl-invert row], show: false, view_type: nil, field_layout: nil, **component_args)
@fields = fields
@tag = tag
@classes = classes
@show = show
@view_type = view_type
@field_layout = field_layout
@component_args = component_args
end
# rubocop:enable Metrics/ParameterLists

def before_render
return unless fields

@fields.each do |field|
with_field(component: field.component, field: field, show: @show, view_type: @view_type)
with_field(component: field.component, field: field, show: @show, view_type: @view_type, layout: @field_layout)
end
end

Expand Down
11 changes: 7 additions & 4 deletions app/components/blacklight/metadata_field_layout_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ class MetadataFieldLayoutComponent < Blacklight::Component
with_collection_parameter :field
renders_one :label
renders_many :values, (lambda do |value: nil, &block|
if block
content_tag :dd, class: "#{@value_class} blacklight-#{@key}", &block
if @value_tag.nil?
block&.call || value
elsif block
content_tag @value_tag, class: "#{@value_class} blacklight-#{@key}", &block
else
content_tag :dd, value, class: "#{@value_class} blacklight-#{@key}"
content_tag @value_tag, value, class: "#{@value_class} blacklight-#{@key}"
end
end)

# @param field [Blacklight::FieldPresenter]
def initialize(field:, label_class: 'col-md-3', value_class: 'col-md-9')
def initialize(field:, value_tag: 'dd', label_class: 'col-md-3', value_class: 'col-md-9')
@field = field
@key = @field.key.parameterize
@label_class = label_class
@value_tag = value_tag
@value_class = value_class
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Blacklight
class MetadataFieldPlainTextLayoutComponent < Blacklight::MetadataFieldLayoutComponent
with_collection_parameter :field

def initialize(field:, **kwargs)
super(field: field, **kwargs, value_tag: nil)
end

# rubocop:disable Rails/OutputSafety
def call
[label.to_s.strip, helpers.strip_tags(CGI.unescape_html(safe_join(values, "\n")).strip)].compact.join(' ').html_safe
end
# rubocop:enable Rails/OutputSafety
end
end
96 changes: 0 additions & 96 deletions app/helpers/blacklight/blacklight_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ module Blacklight::BlacklightHelperBehavior
include Blacklight::LayoutHelperBehavior
include Blacklight::IconHelperBehavior

# @!group Layout helpers

##
# Get the name of this application from an i18n string
# key: blacklight.application_name
Expand All @@ -23,63 +21,6 @@ def application_name
end
end

##
# Get the page's HTML title
#
# @return [String]
def render_page_title
(content_for(:page_title) if content_for?(:page_title)) || @page_title || application_name
end

##
# Create <link rel="alternate"> links from a documents dynamically
# provided export formats.
#
# Returns empty string if no links available.
#
# @param [SolrDocument] document
# @param [Hash] options
# @option options [Boolean] :unique ensures only one link is output for every
# content type, e.g. as required by atom
# @option options [Array<String>] :exclude array of format shortnames to not include in the output
# @return [String]
def render_link_rel_alternates(document = @document, options = {})
return if document.nil?

document_presenter(document).link_rel_alternates(options)
end

##
# Render classes for the <body> element
# @return [String]
def render_body_class
extra_body_classes.join " "
end

##
# List of classes to be applied to the <body> element
# @see render_body_class
# @return [Array<String>]
def extra_body_classes
@extra_body_classes ||= ["blacklight-#{controller.controller_name}", "blacklight-#{[controller.controller_name, controller.action_name].join('-')}"]
end

##
# Get the current "view type" (and ensure it is a valid type)
#
# @param [Hash] query_params the query parameters to check
# @return [Symbol]
def document_index_view_type query_params = params
view_param = query_params[:view]
view_param ||= session[:preferred_view]
if view_param && document_index_views.key?(view_param.to_sym)
view_param.to_sym
else
default_document_index_view_type
end
end

# @!group Search result helpers
##
# Render a partial of an arbitrary format inside a
# template of a different format. (e.g. render an HTML
Expand All @@ -97,45 +38,8 @@ def with_format(format)
nil
end

##
# Should we render a grouped response (because the response
# contains a grouped response instead of the normal response)
#
# Default to false if there's no response object available (sometimes the case
# for tests, but might happen in other circumstances too..)
# @return [Boolean]
def render_grouped_response? response = @response
response&.grouped?
end

##
# Returns a document presenter for the given document
def document_presenter(document)
document_presenter_class(document).new(document, self)
end

##
# Override this method if you want to use a differnet presenter for your documents
# @param [Blacklight::Document] _document optional, here for extension + backwards compatibility only
def document_presenter_class(_document = nil)
case action_name
when 'show', 'citation'
blacklight_config.view_config(:show, action_name: action_name).document_presenter_class
else
blacklight_config.view_config(document_index_view_type, action_name: action_name).document_presenter_class
end
end

# @return [Class]
def search_bar_presenter_class
blacklight_config.view_config(action_name: :index).search_bar_presenter_class
end

# @!group Layout helpers
##
# Open Search discovery tag for HTML <head> links
# @return [String]
def opensearch_description_tag title, href
tag :link, href: href, title: title, type: "application/opensearchdescription+xml", rel: "search"
end
end
81 changes: 27 additions & 54 deletions app/helpers/blacklight/catalog_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module Blacklight::CatalogHelperBehavior
include Blacklight::ConfigurationHelperBehavior
include Blacklight::ComponentHelperBehavior
include Blacklight::DocumentHelperBehavior
include Blacklight::FacetsHelperBehavior
include Blacklight::RenderPartialsHelperBehavior

Expand Down Expand Up @@ -113,40 +114,6 @@ def current_per_page
(@response.rows if @response && @response.rows > 0) || params.fetch(:per_page, blacklight_config.default_per_page).to_i
end

##
# Get the classes to add to a document's div
#
# @param [Blacklight::Document] document
# @return [String]
def render_document_class(document = @document)
types = document_presenter(document).display_type
return if types.blank?

Array(types).compact.map do |t|
"#{document_class_prefix}#{t.try(:parameterize) || t}"
end.join(' ')
end

##
# Return a prefix for the document classes infered from the document
# @see #render_document_class
# @return [String]
def document_class_prefix
'blacklight-'
end

##
# Render the sidebar partial for a document
# This is used as an integration point by downstream apps to add to the
# default sidebar.
# See: https://github.com/geoblacklight/geoblacklight/blob/7d3c31c7af3362879b97e2c1351a2496c728c59c/app/helpers/blacklight_helper.rb#L7
#
# @param [SolrDocument] document
# @return [String]
def render_document_sidebar_partial(document)
render 'show_sidebar', document: document
end

##
# Should we display the sort and per page widget?
#
Expand All @@ -167,26 +134,6 @@ def show_pagination? response = nil
response.limit_value > 0
end

##
# return the Bookmarks on a set of documents (all bookmarks on the page)
# @private
# @return [Enumerable<Bookmark>]
def current_bookmarks
@current_bookmarks ||= begin
documents = @document.presence || @response.documents
current_or_guest_user.bookmarks_for_documents(Array(documents)).to_a
end
end
private :current_bookmarks

##
# Check if the document is in the user's bookmarks
# @param [Blacklight::Document] document
# @return [Boolean]
def bookmarked? document
current_bookmarks.any? { |x| x.document_id == document.id && x.document_type == document.class }
end

# Render an html <title> appropriate string for a selected facet field and values
#
# @see #render_search_to_page_title
Expand Down Expand Up @@ -233,6 +180,32 @@ def render_search_to_page_title(search_state_or_params)
constraints.join(' / ')
end

##
# Should we render a grouped response (because the response
# contains a grouped response instead of the normal response)
#
# Default to false if there's no response object available (sometimes the case
# for tests, but might happen in other circumstances too..)
# @return [Boolean]
def render_grouped_response? response = @response
response&.grouped?
end

##
# Get the current "view type" (and ensure it is a valid type)
#
# @param [Hash] query_params the query parameters to check
# @return [Symbol]
def document_index_view_type query_params = params || {}
view_param = query_params[:view]
view_param ||= session[:preferred_view] if respond_to?(:session)
if view_param && document_index_views.key?(view_param.to_sym)
view_param.to_sym
else
default_document_index_view_type
end
end

private

# @param [String] format
Expand Down
11 changes: 0 additions & 11 deletions app/helpers/blacklight/component_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@

module Blacklight
module ComponentHelperBehavior
##
# Render "document actions" area for navigation header
# (normally renders "Saved Searches", "History", "Bookmarks")
# These things are added by add_nav_action
#
# @param [Hash] options
# @return [String]
def render_nav_actions(options = {}, &block)
render_filtered_partials(blacklight_config.navbar.partials, options, &block)
end

##
# Render "document actions" area for search results view
# (normally renders next to title in the list view)
Expand Down
Loading