Module: Blacklight::BlacklightHelperBehavior

Includes:
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

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 UrlHelperBehavior

#controller_tracking_method, #link_back_to_catalog, #link_to_document, #link_to_previous_search, #session_tracking_params, #session_tracking_path

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



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

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


72
73
74
75
76
77
78
79
80
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 72

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



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

def document_presenter(document)
  document_presenter_class(document).new(document, self)
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



120
121
122
123
124
125
126
127
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 120

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

#extra_body_classesArray<String>

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

Returns:

  • (Array<String>)

See Also:



63
64
65
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 63

def extra_body_classes
  @extra_body_classes ||= ["blacklight-#{controller.controller_name}", "blacklight-#{[controller.controller_name, controller.action_name].join('-')}"]
end

#opensearch_description_tag(title, href) ⇒ String

Open Search discovery tag for HTML <head> links

Returns:

  • (String)


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

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

#render_body_classString

Render classes for the <body> element

Returns:

  • (String)


55
56
57
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 55

def render_body_class
  extra_body_classes.join " "
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)


107
108
109
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 107

def render_grouped_response? response = @response
  response&.grouped?
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)


46
47
48
49
50
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 46

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

  document_presenter(document).link_rel_alternates(options)
end

#render_page_titleString

Get the page’s HTML title

Returns:

  • (String)


30
31
32
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 30

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

#search_bar_presenter_classClass

Returns:

  • (Class)


130
131
132
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 130

def search_bar_presenter_class
  blacklight_config.view_config(action_name: :index).search_bar_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:



92
93
94
95
96
97
98
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 92

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