Module: Blacklight::CatalogHelperBehavior
- Includes:
- ComponentHelperBehavior, ConfigurationHelperBehavior, DocumentHelperBehavior, 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
- #atom_feed_link_tag(options = {}) ⇒ String
-
#current_sort_field ⇒ Blacklight::Configuration::SortField
Look up the current sort field, or provide the default if none is set.
-
#document_counter_with_offset(idx, offset = nil) ⇒ Integer
Get the offset counter for a document.
-
#document_index_view_type(query_params = params || {}) ⇒ Symbol
Get the current “view type” (and ensure it is a valid type).
- #json_api_link_tag(options = {}) ⇒ String
-
#page_entries_info(collection, entry_name: nil) ⇒ String
Override the Kaminari page_entries_info helper with our own, blacklight-aware implementation.
-
#render_grouped_response?(response = @response) ⇒ Boolean
Should we render a grouped response (because the response contains a grouped response instead of the normal response).
-
#render_search_to_page_title(search_state_or_params) ⇒ String
Render an html <title> appropriate string for a set of search parameters.
-
#render_search_to_page_title_filter(facet, values) ⇒ String
Render an html <title> appropriate string for a selected facet field and values.
- #rss_feed_link_tag(options = {}) ⇒ String
-
#search_field_label(params) ⇒ String
Look up search field user-displayable label based on params and blacklight_configuration.
-
#show_pagination?(response = nil) ⇒ Boolean
Should we display the pagination controls?.
-
#show_sort_and_per_page?(response = nil) ⇒ Boolean
Should we display the sort and per page widget?.
Methods included from RenderPartialsHelperBehavior
#document_index_path_templates, #render_document_index, #render_document_index_with_view, #render_document_partial, #render_xml_partials
Methods included from FacetsHelperBehavior
Methods included from DocumentHelperBehavior
#bookmarked?, #document_class_prefix, #document_presenter, #document_presenter_class, #render_document_class
Methods included from ComponentHelperBehavior
#document_actions, #filter_partials, #render_index_doc_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, #should_render_field?, #sort_field_label
Instance Method Details
#atom_feed_link_tag(options = {}) ⇒ String
21 22 23 |
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 21 def atom_feed_link_tag( = {}) auto_discovery_link_tag(:atom, feed_link_url('atom', ), title: t('blacklight.search.atom_feed')) end |
#current_sort_field ⇒ Blacklight::Configuration::SortField
Look up the current sort field, or provide the default if none is set
101 102 103 |
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 101 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_counter_with_offset(idx, offset = nil) ⇒ Integer
Get the offset counter for a document
80 81 82 83 84 85 86 87 |
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 80 def document_counter_with_offset idx, offset = nil offset ||= @response.start if @response offset ||= 0 unless render_grouped_response? idx + 1 + offset end end |
#document_index_view_type(query_params = params || {}) ⇒ Symbol
Get the current “view type” (and ensure it is a valid type)
191 192 193 194 195 196 197 198 199 |
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 191 def document_index_view_type query_params = params || {} view_param = query_params[:view] view_param ||= session[:preferred_view] if respond_to?(:session) if view_param && document_index_views.key?(view_param.to_sym) view_param.to_sym else default_document_index_view_type end end |
#json_api_link_tag(options = {}) ⇒ String
28 29 30 |
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 28 def json_api_link_tag( = {}) auto_discovery_link_tag(:json, feed_link_url('json', ), 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.
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 |
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 42 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 = [collection.offset_value + end_num, collection.total_count].min 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_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..)
182 183 184 |
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 182 def render_grouped_response? response = @response response&.grouped? end |
#render_search_to_page_title(search_state_or_params) ⇒ String
Render an html <title> appropriate string for a set of search parameters
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 149 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
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 131 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 do |value| label = facet_item_presenter(facet_config, value, facet).label label = (label) if label.html_safe? label end.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 |
#rss_feed_link_tag(options = {}) ⇒ String
14 15 16 |
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 14 def rss_feed_link_tag( = {}) auto_discovery_link_tag(:rss, feed_link_url('rss', ), 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.
93 94 95 |
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 93 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?
120 121 122 123 |
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 120 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?
110 111 112 113 |
# File 'app/helpers/blacklight/catalog_helper_behavior.rb', line 110 def show_sort_and_per_page? response = nil response ||= @response !response.empty? end |