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



422
423
424
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 422

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)


137
138
139
140
141
142
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 137

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)


226
227
228
229
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 226

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)


273
274
275
276
277
278
279
280
281
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 273

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

#document_presenter(document) ⇒ Object

Returns a document presenter for the given document



341
342
343
344
345
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 341

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



378
379
380
381
382
383
384
385
386
387
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 378

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:



240
241
242
243
244
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 240

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.


363
364
365
366
367
368
369
370
371
372
373
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 363

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)


401
402
403
404
405
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 401

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)


416
417
418
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 416

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)


426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 426

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.



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 318

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)


258
259
260
261
262
263
264
265
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 258

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)


206
207
208
209
210
211
212
213
214
215
216
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 206

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)


308
309
310
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 308

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)


179
180
181
182
183
184
185
186
187
188
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 179

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
84
85
86
87
88
89
90
91
92
93
94
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 81

def render_search_bar
  if search_bar_presenter_class == Blacklight::SearchBarPresenter && partial_from_blacklight?(Blacklight::SearchBarPresenter.partial)
    component_class = blacklight_config&.view_config(document_index_view_type)&.search_bar_component || Blacklight::SearchBarComponent
    render component_class.new(
      url: search_action_url,
      advanced_search_url: search_action_url(action: 'advanced_search'),
      params: search_state.params_for_search.except(:qt),
      search_fields: Deprecation.silence(Blacklight::ConfigurationHelperBehavior) { search_fields },
      autocomplete_path: search_action_path(action: :suggest)
    )
  else
    search_bar_presenter.render
  end
end

#search_bar_presenterBlacklight::SearchBarPresenter



100
101
102
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 100

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

#search_bar_presenter_classClass

Returns:

  • (Class)


408
409
410
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 408

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)


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

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)


125
126
127
128
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 125

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)


151
152
153
154
155
156
157
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 151

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.


349
350
351
352
353
354
355
356
357
358
359
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 349

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)


393
394
395
396
397
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 393

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:



293
294
295
296
297
298
299
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 293

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