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



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

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)


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

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)


213
214
215
216
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 213

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)


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

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



328
329
330
331
332
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 328

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:



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

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:



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

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.


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

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)


388
389
390
391
392
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 388

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)


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

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)


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

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.



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

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)


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

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)


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

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)


295
296
297
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 295

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)


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

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



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

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

#search_bar_presenter_classClass

Returns:

  • (Class)


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

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)


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

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)


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

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)


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

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.


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

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)


380
381
382
383
384
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 380

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:



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

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