Module: Blacklight::CatalogHelperBehavior

Overview

Helper methods for catalog-like controllers

Instance Method Summary collapse

Methods included from SuggestHelperBehavior

#autocomplete_enabled?

Methods included from SearchHistoryConstraintsHelperBehavior

#render_filter_name, #render_filter_value, #render_search_to_s, #render_search_to_s_element, #render_search_to_s_filters, #render_search_to_s_q

Methods included from RenderPartialsHelperBehavior

#document_index_path_templates, #document_partial_name, #render_document_index, #render_document_index_with_view, #render_document_partial, #render_document_partials, #render_grouped_document_index, #render_xml_partials

Methods included from RenderConstraintsHelperBehavior

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

Methods included from FacetsHelperBehavior

#facet_display_value, #facet_field_id, #facet_field_in_params?, #facet_field_presenter, #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_paginator, #facets_from_request

Methods included from ComponentHelperBehavior

#document_action_label, #document_action_path, #document_actions, #render_index_doc_actions, #render_nav_actions, #render_results_collection_tools, #render_show_doc_actions, #render_show_doc_actions_method_from_blacklight?, #show_doc_actions?

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, #spell_check_max, #view_label

Instance Method Details

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :route_set (Object)

    the route scope to use when constructing the link

Returns:

  • (String)


25
26
27
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 25

def atom_feed_link_tag(options = {})
  auto_discovery_link_tag(:atom, feed_link_url('atom', options), title: t('blacklight.search.atom_feed'))
end

#bookmarked?(document) ⇒ Boolean

Check if the document is in the user's bookmarks

Parameters:

Returns:

  • (Boolean)


300
301
302
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 300

def bookmarked? document
  current_bookmarks.any? { |x| x.document_id == document.id && x.document_type == document.class }
end

#current_bookmarks(documents_or_response = nil) ⇒ Enumerable<Bookmark>

return the Bookmarks on a set of documents

Parameters:

Returns:



282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 282

def current_bookmarks documents_or_response = nil
  documents = if documents_or_response.respond_to? :documents
                Deprecation.warn(Blacklight::CatalogHelperBehavior, "Passing a response to #current_bookmarks is deprecated; pass response.documents instead")
                documents_or_response.documents
              else
                documents_or_response
              end

  documents ||= [@document] if @document.present?
  documents ||= @response.documents

  @current_bookmarks ||= current_or_guest_user.bookmarks_for_documents(documents).to_a
end

#current_per_pageInteger

Look up the current per page value, or the default if none if set

Returns:

  • (Integer)


131
132
133
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 131

def current_per_page
  (@response.rows if @response && @response.rows > 0) || params.fetch(:per_page, blacklight_config.default_per_page).to_i
end

#current_sort_fieldBlacklight::Configuration::SortField

Look up the current sort field, or provide the default if none is set



123
124
125
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 123

def current_sort_field
  (blacklight_config.sort_fields.values.find { |f| f.sort == @response.sort } if @response && @response.sort.present?) || blacklight_config.sort_fields[params[:sort]] || default_sort_field
end

#default_view_type_group_icon_classes(view) ⇒ String

Deprecated.

Get the default view type classes for a view in the results view picker

Parameters:

  • view (String)

Returns:

  • (String)


273
274
275
276
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 273

def default_view_type_group_icon_classes view
  Deprecation.warn(Blacklight::CatalogHelperBehavior, 'This method has been deprecated, use blacklight_icons helper instead')
  "glyphicon-#{view.to_s.parameterize} view-icon-#{view.to_s.parameterize}"
end

#document_class_prefixString

Return a prefix for the document classes infered from the document

Returns:

  • (String)

See Also:



153
154
155
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 153

def document_class_prefix
  'blacklight-'
end

#document_counter_with_offset(idx, offset = nil) ⇒ Integer

Get the offset counter for a document

Parameters:

  • idx (Integer)

    document index

  • offset (Integer) (defaults to: nil)

    additional offset to incremenet the counter by

Returns:

  • (Integer)


88
89
90
91
92
93
94
95
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 88

def document_counter_with_offset idx, offset = nil
  offset ||= @response.start if @response
  offset ||= 0

  unless render_grouped_response?
    idx + 1 + offset
  end
end

#has_thumbnail?(document) ⇒ Boolean

Deprecated.

Does the document have a thumbnail to render?

Parameters:

Returns:

  • (Boolean)


224
225
226
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 224

def has_thumbnail? document
  document_presenter(document).thumbnail.exists?
end

#item_page_entry_infoString

Deprecated.

Like #page_entries_info above, but for an individual item show page. Displays “showing X of Y items” message.

Returns:

  • (String)

See Also:



104
105
106
107
108
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 104

def 
  t('blacklight.search.entry_pagination_info.other', current: number_with_delimiter(search_session['counter']),
                                                     total: number_with_delimiter(search_session['total']),
                                                     count: search_session['total'].to_i).html_safe
end

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :route_set (Object)

    the route scope to use when constructing the link

Returns:

  • (String)


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

def json_api_link_tag(options = {})
  auto_discovery_link_tag(:json, feed_link_url('json', options), type: 'application/json')
end

#page_entries_info(collection, entry_name: nil) ⇒ String

Override the Kaminari page_entries_info helper with our own, blacklight-aware implementation. Why do we have to do this?

- We need custom counting information for grouped results
- We need to provide number_with_delimiter strings to i18n keys

If we didn't have to do either one of these, we could get away with removing this entirely.

Parameters:

  • collection (RSolr::Resource)

    (or other Kaminari-compatible objects)

Returns:

  • (String)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 46

def page_entries_info(collection, entry_name: nil)
  entry_name = if entry_name
                 entry_name.pluralize(collection.size, I18n.locale)
               else
                 collection.entry_name(count: collection.size).to_s
               end

  # grouped response objects need special handling
  end_num = if collection.respond_to?(:groups) && render_grouped_response?(collection)
              collection.groups.length
            else
              collection.limit_value
            end

  end_num = if collection.offset_value + end_num <= collection.total_count
              collection.offset_value + end_num
            else
              collection.total_count
            end

  case collection.total_count
    when 0
      t('blacklight.search.pagination_info.no_items_found', entry_name: entry_name).html_safe
    when 1
      t('blacklight.search.pagination_info.single_item_found', entry_name: entry_name).html_safe
    else
      t('blacklight.search.pagination_info.pages', entry_name: entry_name,
                                                   current_page: collection.current_page,
                                                   num_pages: collection.total_pages,
                                                   start_num: number_with_delimiter(collection.offset_value + 1),
                                                   end_num: number_with_delimiter(end_num),
                                                   total_num: number_with_delimiter(collection.total_count),
                                                   count: collection.total_pages).html_safe
  end
end

#render_document_class(document = @document) ⇒ String

Get the classes to add to a document's div

Parameters:

Returns:

  • (String)


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

def render_document_class(document = @document)
  types = document_presenter(document).display_type
  return if types.blank?

  Array(types).compact.map do |t|
    "#{document_class_prefix}#{t.try(:parameterize) || t}"
  end.join(' ')
end

#render_document_main_content_partial(_document = @document) ⇒ String

Render the main content partial for a document This is widely used as by downstream apps when they override their show view. See github.com/search?q=render_document_main_content_partial&type=Code

Parameters:

Returns:

  • (String)


181
182
183
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 181

def render_document_main_content_partial(_document = @document)
  render partial: 'show_main_content'
end

#render_document_sidebar_partial(document = nil) ⇒ String

Render the sidebar partial for a document This is used as an integration point by downstream apps to add to the default sidebar. See: github.com/geoblacklight/geoblacklight/blob/7d3c31c7af3362879b97e2c1351a2496c728c59c/app/helpers/blacklight_helper.rb#L7

Parameters:

Returns:

  • (String)


165
166
167
168
169
170
171
172
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 165

def render_document_sidebar_partial(document = nil)
  unless document
    Deprecation.warn(self, 'render_document_sidebar_partial expects one argument ' \
      '(@document) and you passed none. This behavior will be removed in version 8')
    document = @document
  end
  render 'show_sidebar', document: document
end

#render_search_to_page_title(search_state_or_params) ⇒ String

Render an html <title> appropriate string for a set of search parameters

Parameters:

  • params2 (ActionController::Parameters)

Returns:

  • (String)


324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 324

def render_search_to_page_title(search_state_or_params)
  search_state = if search_state_or_params.is_a? Blacklight::SearchState
                   search_state_or_params
                 else
                   controller.search_state_class.new(params, blacklight_config, self)
                 end

  constraints = []

  if search_state.query_param.present?
    q_label = label_for_search_field(search_state.search_field.key) unless search_state.search_field&.key.blank? || default_search_field?(search_state.search_field.key)

    constraints += if q_label.present?
                     [t('blacklight.search.page_title.constraint', label: q_label, value: search_state.query_param)]
                   else
                     [search_state.query_param]
                   end
  end

  if search_state.filters.any?
    constraints += search_state.filters.collect { |filter| render_search_to_page_title_filter(filter.key, filter.values) }
  end

  constraints.join(' / ')
end

#render_search_to_page_title_filter(facet, values) ⇒ String

Render an html <title> appropriate string for a selected facet field and values

Parameters:

  • facet (Symbol)

    the facet field

  • values (Array<String>)

    the selected facet values

Returns:

  • (String)

See Also:



310
311
312
313
314
315
316
317
318
319
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 310

def render_search_to_page_title_filter(facet, values)
  facet_config = facet_configuration_for_field(facet)
  filter_label = facet_field_label(facet_config.key)
  filter_value = if values.size < 3
                   values.map { |value| facet_item_presenter(facet_config, value, facet).label }.to_sentence
                 else
                   t('blacklight.search.page_title.many_constraint_values', values: values.size)
                 end
  t('blacklight.search.page_title.constraint', label: filter_label, value: filter_value)
end

#render_thumbnail_tag(document, image_options = {}, url_options = {}) ⇒ String

Deprecated.

Render the thumbnail, if available, for a document and link it to the document record.

Parameters:

  • document (SolrDocument)
  • image_options (Hash) (defaults to: {})

    to pass to the image tag

  • url_options (Hash) (defaults to: {})

    to pass to #link_to_document

Returns:

  • (String)


238
239
240
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 238

def render_thumbnail_tag document, image_options = {}, url_options = {}
  document_presenter(document).thumbnail.thumbnail_tag(image_options, url_options)
end

#render_view_type_group_icon(view) ⇒ String

Deprecated.

Render the view type icon for the results view picker

Parameters:

  • view (String)

Returns:

  • (String)


262
263
264
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 262

def render_view_type_group_icon view
  blacklight_icon(view)
end

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :route_set (Object)

    the route scope to use when constructing the link

Returns:

  • (String)


18
19
20
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 18

def rss_feed_link_tag(options = {})
  auto_discovery_link_tag(:rss, feed_link_url('rss', options), title: t('blacklight.search.rss_feed'))
end

#search_field_label(params) ⇒ String

Look up search field user-displayable label based on params and blacklight_configuration.

Returns:

  • (String)


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

def search_field_label(params)
  h(label_for_search_field(params[:search_field]))
end

#should_autofocus_on_search_box?Boolean

Deprecated.

If no search parameters have been given, we should auto-focus the user's cursor into the searchbox

Returns:

  • (Boolean)


211
212
213
214
215
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 211

def should_autofocus_on_search_box?
  controller.is_a?(Blacklight::Catalog) &&
    action_name == "index" &&
    !has_search_parameters?
end

#show_pagination?(response = nil) ⇒ Boolean

Should we display the pagination controls?

Parameters:

Returns:

  • (Boolean)


200
201
202
203
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 200

def show_pagination? response = nil
  response ||= @response
  response.limit_value > 0
end

#show_sort_and_per_page?(response = nil) ⇒ Boolean

Should we display the sort and per page widget?

Parameters:

Returns:

  • (Boolean)


190
191
192
193
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 190

def show_sort_and_per_page? response = nil
  response ||= @response
  !response.empty?
end

#thumbnail_url(document) ⇒ String

Deprecated.

Get the URL to a document's thumbnail image

Parameters:

Returns:

  • (String)


249
250
251
252
253
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 249

def thumbnail_url document
  if document.has? blacklight_config.view_config(document_index_view_type).thumbnail_field
    document.first(blacklight_config.view_config(document_index_view_type).thumbnail_field)
  end
end