Module: Blacklight::BlacklightHelperBehavior

Extended by:
Deprecation
Includes:
HashAsHiddenFieldsHelperBehavior, IconHelperBehavior, LayoutHelperBehavior, UrlHelperBehavior
Included in:
BlacklightHelper
Defined in:
app/helpers/blacklight/blacklight_helper_behavior.rb

Overview

Methods added to this helper will be available to all templates in the hosting application

Layout helpers collapse

Presenter extension helpers collapse

Document helpers collapse

Search result helpers collapse

Methods included from IconHelperBehavior

#blacklight_icon

Methods included from LayoutHelperBehavior

#container_classes, #html_tag_attributes, #main_content_classes, #show_content_classes, #show_sidebar_classes, #sidebar_classes

Methods included from HashAsHiddenFieldsHelperBehavior

#render_hash_as_hidden_fields

Methods included from UrlHelperBehavior

#add_group_facet_params_and_redirect, #bookmarks_export_url, #controller_tracking_method, #link_back_to_catalog, #link_to_document, #link_to_next_document, #link_to_previous_document, #link_to_previous_search, #link_to_query, #session_tracking_path, #start_over_path, #url_for_document

Class Method Details

.blacklight_pathObject



411
412
413
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 411

def self.blacklight_path
  @blacklight_path ||= Gem.loaded_specs["blacklight"].full_gem_path
end

Instance Method Details

#application_nameString

Get the name of this application from an i18n string key: blacklight.application_name Try first in the current locale, then the default locale

Returns:

  • (String)

    the application name



19
20
21
22
23
24
25
26
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 19

def application_name
  # It's important that we don't use ActionView::Helpers::CacheHelper#cache here
  # because it returns nil.
  Rails.cache.fetch 'blacklight/application_name' do
    t('blacklight.application_name',
      default: t('blacklight.application_name', locale: I18n.default_locale))
  end
end

#document_has_value?(document, field_config) ⇒ Boolean

Deprecated.

Check if a document has (or, might have, in the case of accessor methods) a value for the given solr

Parameters:

Returns:

  • (Boolean)


126
127
128
129
130
131
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 126

def document_has_value? document, field_config
  Deprecation.warn self, "document_has_value? is deprecated and will be removed in Blacklight 8. Use DocumentPresenter#has_value? instead."
  document.has?(field_config.field) ||
    (document.has_highlight_field? field_config.field if field_config.highlight) ||
    field_config.accessor
end

#document_heading(document = nil) ⇒ String

Deprecated.

Get the value of the document's “title” field, or a placeholder value (if empty)

Parameters:

Returns:

  • (String)


215
216
217
218
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 215

def document_heading document = nil
  document ||= @document
  document_presenter(document).heading
end

#document_index_view_type(query_params = params) ⇒ Symbol

Get the current “view type” (and ensure it is a valid type)

Parameters:

  • query_params (Hash) (defaults to: params)

    the query parameters to check

Returns:

  • (Symbol)


262
263
264
265
266
267
268
269
270
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 262

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

#document_presenter(document) ⇒ Object

Returns a document presenter for the given document



330
331
332
333
334
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 330

def document_presenter(document)
  Deprecation.silence(Blacklight::BlacklightHelperBehavior) do
    presenter(document)
  end
end

#document_presenter_class(document = nil) ⇒ Object

Override this method if you want to use a differnet presenter for your documents

Parameters:

  • document (Blacklight::Document) (defaults to: nil)

    optional, here for extension + backwards compatibility only



367
368
369
370
371
372
373
374
375
376
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 367

def document_presenter_class(document = nil)
  Deprecation.silence(Blacklight::BlacklightHelperBehavior) do
    case action_name
    when 'show', 'citation'
      show_presenter_class(document)
    else
      index_presenter_class(document)
    end
  end
end

#document_show_html_title(document = nil) ⇒ String

Deprecated.

Get the document's “title” to display in the <title> element. (by default, use the #document_heading)

Parameters:

Returns:

  • (String)

See Also:



229
230
231
232
233
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 229

def document_show_html_title document = nil
  document ||= @document

  document_presenter(document).html_title
end

#extra_body_classesArray<String>

List of classes to be applied to the <body> element

Returns:

  • (Array<String>)

See Also:



74
75
76
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 74

def extra_body_classes
  @extra_body_classes ||= ['blacklight-' + controller.controller_name, 'blacklight-' + [controller.controller_name, controller.action_name].join('-')]
end

#index_presenter(document) ⇒ Blacklight::IndexPresenter

Deprecated.


352
353
354
355
356
357
358
359
360
361
362
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 352

def index_presenter(document)
  Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#index_presenter is deprecated; use #document_presenter instead')

  if method(:index_presenter_class).owner != Blacklight::BlacklightHelperBehavior
    Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#index_presenter_class has been overridden; please override #document_presenter_class instead')
  end

  Deprecation.silence(Blacklight::BlacklightHelperBehavior) do
    index_presenter_class(document).new(document, self)
  end
end

#index_presenter_class(_document) ⇒ Class

Deprecated.

Returns:

  • (Class)


390
391
392
393
394
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 390

def index_presenter_class(_document)
  Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#index_presenter_class is deprecated; use #document_presenter_class instead')

  blacklight_config.view_config(document_index_view_type, action_name: action_name).document_presenter_class
end

#opensearch_description_tag(title, href) ⇒ String

Open Search discovery tag for HTML <head> links

Returns:

  • (String)


405
406
407
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 405

def opensearch_description_tag title, href
  tag :link, href: href, title: title, type: "application/opensearchdescription+xml", rel: "search"
end

#partial_from_blacklight?(partial) ⇒ Boolean

Returns:

  • (Boolean)


415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 415

def partial_from_blacklight?(partial)
  path = if Rails::VERSION::MAJOR >= 6
           name = partial.split('/').last
           prefix = partial.split('/').first if partial.include?('/')
           logger&.debug "Looking for document index partial #{partial}"
           prefixes = lookup_context.prefixes + [prefix, ""].compact
           lookup_context.find_all(name, prefixes, true).first&.identifier
         else
           lookup_context.find_all(partial, lookup_context.prefixes + [""], true).first&.identifier
         end

  path&.starts_with?(Blacklight::BlacklightHelperBehavior.blacklight_path)
end

#presenter(document) ⇒ Blacklight::DocumentPresenter

Deprecated.

Returns a document presenter for the given document TODO: Move this to the controller. It can just pass a presenter or set of presenters.



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 307

def presenter(document)
  Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#presenter is deprecated; use #document_presenter instead')

  # As long as the presenter methods haven't been overridden, we can use the new behavior
  if method(:show_presenter).owner == Blacklight::BlacklightHelperBehavior &&
     method(:index_presenter).owner == Blacklight::BlacklightHelperBehavior
    return document_presenter_class(document).new(document, self)
  end

  Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#show_presenter and/or #index_presenter have been overridden; please override #document_presenter instead')

  Deprecation.silence(Blacklight::BlacklightHelperBehavior) do
    case action_name
    when 'show', 'citation'
      show_presenter(document)
    else
      index_presenter(document)
    end
  end
end

#render_body_classString

Render classes for the <body> element

Returns:

  • (String)


66
67
68
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 66

def render_body_class
  extra_body_classes.join " "
end

#render_document_heading(document, options) ⇒ String #render_document_heading(options) ⇒ String

Deprecated.

Render the document “heading” (title) in a content tag

Overloads:

  • #render_document_heading(document, options) ⇒ String

    Parameters:

    Options Hash (options):

    • :tag (Symbol)
  • #render_document_heading(options) ⇒ String

    Parameters:

    • options (Hash)

    Options Hash (options):

    • :tag (Symbol)

Returns:

  • (String)


247
248
249
250
251
252
253
254
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 247

def render_document_heading(*args)
  options = args.extract_options!
  document = args.first
  tag = options.fetch(:tag, :h4)
  document ||= @document

  (tag, document_presenter(document).heading, itemprop: "name")
end

#render_document_show_field_label(options) ⇒ String #render_document_show_field_label(document, options) ⇒ String

Deprecated.

Render the show field label for a document

Overloads:

  • #render_document_show_field_label(options) ⇒ String

    Use the default, document-agnostic configuration

    Parameters:

    • opts (Hash)
  • #render_document_show_field_label(document, options) ⇒ String

    Allow an extention point where information in the document may drive the value of the field

    Parameters:

Returns:

  • (String)


195
196
197
198
199
200
201
202
203
204
205
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 195

def render_document_show_field_label *args
  options = args.extract_options!
  document = args.first

  field = options[:field]
  label = Deprecation.silence(Blacklight::ConfigurationHelperBehavior) do
    options[:label] || document_show_field_label(document, field)
  end

  t(:'blacklight.search.show.label', label: label)
end

#render_grouped_response?(response = @response) ⇒ Boolean

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..)

Returns:

  • (Boolean)


297
298
299
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 297

def render_grouped_response? response = @response
  response&.grouped?
end

#render_index_field_label(options) ⇒ String #render_index_field_label(document, options) ⇒ String

Deprecated.

Render the index field label for a document

Translations for index field labels should go under blacklight.search.fields They are picked up from there by a value “%label” in blacklight.search.index.label

Overloads:

  • #render_index_field_label(options) ⇒ String

    Use the default, document-agnostic configuration

    Parameters:

    • opts (Hash)
  • #render_index_field_label(document, options) ⇒ String

    Allow an extention point where information in the document may drive the value of the field

    Parameters:

Returns:

  • (String)


168
169
170
171
172
173
174
175
176
177
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 168

def render_index_field_label *args
  options = args.extract_options!
  document = args.first

  field = options[:field]
  label = Deprecation.silence(Blacklight::ConfigurationHelperBehavior) do
    options[:label] || index_field_label(document, field)
  end
  html_escape t(:"blacklight.search.index.#{document_index_view_type}.label", default: :'blacklight.search.index.label', label: label)
end

Create <link rel=“alternate”> links from a documents dynamically provided export formats.

Returns empty string if no links available.

Parameters:

  • document (SolrDocument) (defaults to: @document)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :unique (Boolean)

    ensures only one link is output for every content type, e.g. as required by atom

  • :exclude (Array<String>)

    array of format shortnames to not include in the output

Returns:

  • (String)


48
49
50
51
52
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 48

def render_link_rel_alternates(document = @document, options = {})
  return if document.nil?

  document_presenter(document).link_rel_alternates(options)
end

#render_opensearch_response_metadataString

Deprecated.

Render OpenSearch headers for this search

Returns:

  • (String)


58
59
60
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 58

def 
  render partial: 'catalog/opensearch_response_metadata', locals: { response: @response }
end

#render_page_titleString

Get the page's HTML title

Returns:

  • (String)


32
33
34
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 32

def render_page_title
  (content_for(:page_title) if content_for?(:page_title)) || @page_title || application_name
end

#render_search_barString

Render the search navbar

Returns:

  • (String)


81
82
83
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 81

def render_search_bar
  search_bar_presenter.render
end

#search_bar_presenterBlacklight::SearchBarPresenter



89
90
91
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 89

def search_bar_presenter
  @search_bar ||= search_bar_presenter_class.new(controller, blacklight_config)
end

#search_bar_presenter_classClass

Returns:

  • (Class)


397
398
399
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 397

def search_bar_presenter_class
  blacklight_config.view_config(action_name: :index).search_bar_presenter_class
end

#should_render_index_field?(document, field_config) ⇒ Boolean

Deprecated.

Determine whether to render a given field in the index view.

Parameters:

Returns:

  • (Boolean)


102
103
104
105
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 102

def should_render_index_field? document, field_config
  Deprecation.warn self, "should_render_index_field? is deprecated and will be removed in Blacklight 8. Use IndexPresenter#render_field? instead."
  should_render_field?(field_config, document) && document_has_value?(document, field_config)
end

#should_render_show_field?(document, field_config) ⇒ Boolean

Deprecated.

Determine whether to render a given field in the show view

Parameters:

Returns:

  • (Boolean)


114
115
116
117
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 114

def should_render_show_field? document, field_config
  Deprecation.warn self, "should_render_show_field? is deprecated and will be removed in Blacklight 8. Use ShowPresenter#render_field? instead."
  should_render_field?(field_config, document) && document_has_value?(document, field_config)
end

#should_show_spellcheck_suggestions?(response) ⇒ Boolean

Deprecated.

Determine whether to display spellcheck suggestions

Parameters:

Returns:

  • (Boolean)


140
141
142
143
144
145
146
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 140

def should_show_spellcheck_suggestions? response
  Deprecation.silence(Blacklight::ConfigurationHelperBehavior) do
    # The spelling response field may be missing from non solr repositories.
    response.total <= spell_check_max &&
      (response.spelling&.words&.any? || response.spelling&.collation&.present? || false)
  end
end

#show_presenter(document) ⇒ Blacklight::ShowPresenter

Deprecated.


338
339
340
341
342
343
344
345
346
347
348
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 338

def show_presenter(document)
  Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#show_presenter is deprecated; use #document_presenter instead')

  if method(:show_presenter_class).owner != Blacklight::BlacklightHelperBehavior
    Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#show_presenter_class has been overridden; please override #document_presenter_class instead')
  end

  Deprecation.silence(Blacklight::BlacklightHelperBehavior) do
    show_presenter_class(document).new(document, self)
  end
end

#show_presenter_class(_document) ⇒ Class

Deprecated.

Override this method if you want to use a different presenter class

Returns:

  • (Class)


382
383
384
385
386
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 382

def show_presenter_class(_document)
  Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#show_presenter_class is deprecated; use #document_presenter_class instead')

  blacklight_config.view_config(:show, action_name: action_name).document_presenter_class
end

#with_format(format) { ... } ⇒ Object

Render a partial of an arbitrary format inside a template of a different format. (e.g. render an HTML partial from an XML template) code taken from: stackoverflow.com/questions/339130/how-do-i-render-a-partial-of-a-different-format-in-rails (zgchurch)

Parameters:

  • format (String)

    suffix

Yields:



282
283
284
285
286
287
288
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 282

def with_format(format)
  old_formats = formats
  self.formats = [format]
  yield
  self.formats = old_formats
  nil
end