Module: Blacklight::CatalogHelperBehavior

Includes:
ComponentHelperBehavior, ConfigurationHelperBehavior, FacetsHelperBehavior, RenderPartialsHelperBehavior
Included in:
CatalogHelper
Defined in:
app/helpers/blacklight/catalog_helper_behavior.rb

Overview

Helper methods for catalog-like controllers

Instance Method Summary collapse

Methods included from RenderPartialsHelperBehavior

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

Methods included from FacetsHelperBehavior

#facet_field_presenter

Methods included from ComponentHelperBehavior

#document_actions, #render_index_doc_actions, #render_nav_actions, #render_results_collection_tools, #show_doc_actions?

Methods included from ConfigurationHelperBehavior

#active_sort_fields, #default_document_index_view_type, #default_search_field?, #default_sort_field, #document_index_view_controls, #document_index_views, #facet_field_label, #field_label, #label_for_search_field, #per_page_options_for_select, #should_render_field?, #sort_field_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)


20
21
22
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 20

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)


186
187
188
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 186

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

#current_per_pageInteger

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

Returns:

  • (Integer)


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

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



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

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

#document_class_prefixString

Return a prefix for the document classes infered from the document

Returns:

  • (String)

See Also:



134
135
136
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 134

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)


83
84
85
86
87
88
89
90
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 83

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

  unless render_grouped_response?
    idx + 1 + offset
  end
end

Parameters:

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

Options Hash (options):

  • :route_set (Object)

    the route scope to use when constructing the link

Returns:

  • (String)


27
28
29
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 27

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)


41
42
43
44
45
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
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 41

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)


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

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_sidebar_partial(document) ⇒ 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)


146
147
148
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 146

def render_document_sidebar_partial(document)
  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:

  • params (ActionController::Parameters)

Returns:

  • (String)


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 210

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:



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

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

Parameters:

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

Options Hash (options):

  • :route_set (Object)

    the route scope to use when constructing the link

Returns:

  • (String)


13
14
15
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 13

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)


96
97
98
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 96

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

#show_pagination?(response = nil) ⇒ Boolean

Should we display the pagination controls?

Parameters:

Returns:

  • (Boolean)


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

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)


155
156
157
158
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 155

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