Module: Blacklight::BlacklightHelperBehavior

Extended by:
Deprecation
Includes:
RenderPartialsHelper, BlacklightConfigurationHelper, BlacklightUrlHelper, FacetsHelper, HashAsHiddenFieldsHelper, RenderConstraintsHelper
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

Instance Method Summary collapse

Methods included from FacetsHelperBehavior

#facet_display_value, #facet_field_id, #facet_field_in_params?, #facet_in_params?, #facet_params, #facet_partial_name, #has_facet_values?, #path_for_facet, #render_facet_count, #render_facet_item, #render_facet_limit, #render_facet_limit_list, #render_facet_partials, #render_facet_value, #render_selected_facet_value, #should_collapse_facet?, #should_render_facet?

Methods included from Facet

#facet_by_field_name, #facet_field_names, #facet_paginator, #facets_from_request

Methods included from RenderPartialsHelper

#document_index_path_templates, #document_partial_name, #document_partial_path_templates, #render_document_index, #render_document_index_with_view, #render_document_partial, #render_document_partials, #render_grouped_document_index, #type_field_to_partial_name

Methods included from RenderConstraintsHelperBehavior

#query_has_constraints?, #remove_constraint_url, #render_constraint_element, #render_constraints, #render_constraints_filters, #render_constraints_query, #render_filter_element

Methods included from HashAsHiddenFieldsHelperBehavior

#flat_hash_key, #flatten_hash, #render_hash_as_hidden_fields

Methods included from ConfigurationHelperBehavior

#active_sort_fields, #constraint_query_label, #default_document_index_view_type, #default_per_page, #default_search_field?, #default_sort_field, #document_index_view_controls, #document_index_views, #document_show_field_label, #document_show_fields, #document_show_link_field, #facet_field_label, #field_label, #has_alternative_views?, #index_field_label, #index_fields, #label_for_search_field, #per_page_options_for_select, #search_field_options_for_select, #search_fields, #should_render_field?, #sort_field_label, #sort_fields, #spell_check_max, #view_label

Methods included from UrlHelperBehavior

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

Methods included from DeprecatedUrlHelperBehavior

#add_facet_params, #params_for_search, #remove_facet_params, #reset_search_params, #sanitize_search_params

Instance Method Details

#application_nameString

Get the name of this application, from either:

- the Rails configuration
- an i18n string (key: blacklight.application_name; preferred)

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
  if Rails.application.config.respond_to? :application_name
    Deprecation.warn(self, "BlacklightHelper#application_name will no longer delegate to config.application_name in version 7.0. Set the i18n for blacklight.application_name instead")
    return Rails.application.config.application_name
  end

  t('blacklight.application_name')
end

#document_has_value?(document, field_config) ⇒ Boolean

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

Parameters:

Returns:

  • (Boolean)


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

def document_has_value? document, field_config
  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

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

Parameters:

Returns:

  • (String)


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

def document_heading document=nil
  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)


300
301
302
303
304
305
306
307
308
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 300

def document_index_view_type query_params=params
  view_param = query_params[:view]
  view_param ||= session[:preferred_view]
  if view_param and document_index_views.keys.include? view_param.to_sym
    view_param.to_sym
  else
    default_document_index_view_type
  end
end

#document_show_html_title(document = nil) ⇒ String

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

Parameters:

Returns:

  • (String)

See Also:



253
254
255
256
257
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 253

def document_show_html_title document=nil
  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:



70
71
72
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 70

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

#get_field_values(document, _field, field_config, options = {}) ⇒ Object

Get the value for a document's field, and prepare to render it.

  • highlight_field

  • accessor

  • solr field

Rendering:

- helper_method
- link_to_search

Parameters:



290
291
292
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 290

def get_field_values document, _field, field_config, options = {}
  presenter(document).field_values field_config, options
end

#index_presenter(document) ⇒ Object



353
354
355
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 353

def index_presenter(document)
  index_presenter_class(document).new(document, self)
end

#index_presenter_class(_document) ⇒ Object



368
369
370
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 368

def index_presenter_class(_document)
  blacklight_config.index.document_presenter_class
end

#opensearch_description_tag(title, href) ⇒ Object

Open Search discovery tag for HTML <head> links



374
375
376
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 374

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

#presenter(document) ⇒ Object

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



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

def presenter(document)
  case action_name
  when 'show', 'citation'
    show_presenter(document)
  when 'index'
    index_presenter(document)
  else
    Deprecation.warn(Blacklight::BlacklightHelperBehavior, "Unable to determine presenter type for #{action_name} on #{controller_name}, falling back on deprecated Blacklight::DocumentPresenter")
    presenter_class.new(document, self)
  end
end

#presenter_classObject



357
358
359
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 357

def presenter_class
  blacklight_config.document_presenter_class
end

#render_body_classString

Render classes for the <body> element

Returns:

  • (String)


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

def render_body_class
  extra_body_classes.join " "
end

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

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

Overloads:

  • #render_document_heading(document, options) ⇒ Object

    Parameters:

    Options Hash (options):

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

    Parameters:

    • options (Hash)

    Options Hash (options):

    • :tag (Symbol)


268
269
270
271
272
273
274
275
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 268

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

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

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

Render the show field label for a document

Overloads:

  • #render_document_show_field_label(options) ⇒ Object

    Use the default, document-agnostic configuration

    Parameters:

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

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

    Parameters:



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

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

  field = options[:field]

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

#render_document_show_field_value(options) ⇒ Object #render_document_show_field_value(document, options) ⇒ Object #render_document_show_field_value(document, field, options) ⇒ Object

Deprecated.

use ShowPresenter#field_value

Render the index field label for a document

Overloads:

  • #render_document_show_field_value(options) ⇒ Object

    Use the default, document-agnostic configuration

    Parameters:

    • opts (Hash)
  • #render_document_show_field_value(document, options) ⇒ Object

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

    Parameters:

  • #render_document_show_field_value(document, field, options) ⇒ Object

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

    Parameters:



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

def render_document_show_field_value *args
  render_field_value(*args)
end

#render_field_value(*args) ⇒ Object

Deprecated.

use IndexPresenter#field_value



175
176
177
178
179
180
181
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 175

def render_field_value(*args)
  options = args.extract_options!
  document = args.shift || options[:document]

  field = args.shift || options[:field]
  presenter(document).field_value field, options.except(:document, :field)
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)

Returns:

  • (Boolean)


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

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

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

Render the index field label for a document

Overloads:

  • #render_index_field_label(options) ⇒ Object

    Use the default, document-agnostic configuration

    Parameters:

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

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

    Parameters:



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

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

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

#render_index_field_value(options) ⇒ Object #render_index_field_value(document, options) ⇒ Object #render_index_field_value(document, field, options) ⇒ Object

Deprecated.

use IndexPresenter#field_value

Render the index field label for a document

Overloads:

  • #render_index_field_value(options) ⇒ Object

    Use the default, document-agnostic configuration

    Parameters:

    • opts (Hash)
  • #render_index_field_value(document, options) ⇒ Object

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

    Parameters:

  • #render_index_field_value(document, field, options) ⇒ Object

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

    Parameters:



169
170
171
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 169

def render_index_field_value *args
  render_field_value(*args)
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



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

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

#render_opensearch_response_metadataString

Render OpenSearch headers for this search

Returns:

  • (String)


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

def 
  render :partial => 'catalog/opensearch_response_metadata'
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)


77
78
79
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 77

def render_search_bar
  render :partial=>'catalog/search_form'
end

#should_render_index_field?(document, field_config) ⇒ Boolean

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

Parameters:

Returns:

  • (Boolean)


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

def should_render_index_field? document, field_config
  should_render_field?(field_config, document) && document_has_value?(document, field_config)
end

#should_render_show_field?(document, field_config) ⇒ Boolean

Determine whether to render a given field in the show view

Parameters:

Returns:

  • (Boolean)


97
98
99
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 97

def should_render_show_field? document, field_config
  should_render_field?(field_config, document) && document_has_value?(document, field_config)
end

#should_show_spellcheck_suggestions?(response) ⇒ Boolean

Determine whether to display spellcheck suggestions

Parameters:

Returns:

  • (Boolean)


118
119
120
121
122
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 118

def should_show_spellcheck_suggestions? response
  response.total <= spell_check_max &&
    !response.spelling.nil? &&
    response.spelling.words.any?
end

#show_presenter(document) ⇒ Object



349
350
351
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 349

def show_presenter(document)
  show_presenter_class(document).new(document, self)
end

#show_presenter_class(_document) ⇒ Object

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



364
365
366
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 364

def show_presenter_class(_document)
  blacklight_config.show.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:



319
320
321
322
323
324
325
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 319

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