Module: Blacklight::BlacklightHelperBehavior
- Extended by:
- Deprecation
- Includes:
- HashAsHiddenFieldsHelperBehavior, 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
- .blacklight_path ⇒ Object
-
#application_name ⇒ String
Get the name of this application from an i18n string key: blacklight.application_name Try first in the current locale, then the default locale.
-
#extra_body_classes ⇒ Array<String>
List of classes to be applied to the <body> element.
-
#opensearch_description_tag(title, href) ⇒ String
Open Search discovery tag for HTML <head> links.
- #partial_from_blacklight?(partial) ⇒ Boolean
-
#render_body_class ⇒ String
Render classes for the <body> element.
-
#render_link_rel_alternates(document = @document, options = {}) ⇒ String
Create <link rel=“alternate”> links from a documents dynamically provided export formats.
- #render_opensearch_response_metadata ⇒ String deprecated Deprecated.
-
#render_page_title ⇒ String
Get the page's HTML title.
-
#render_search_bar ⇒ String
Render the search navbar.
Presenter extension helpers collapse
-
#document_presenter(document) ⇒ Object
Returns a document presenter for the given document.
-
#document_presenter_class(document = nil) ⇒ Object
Override this method if you want to use a differnet presenter for your documents.
- #index_presenter(document) ⇒ Blacklight::IndexPresenter deprecated Deprecated.
- #index_presenter_class(_document) ⇒ Class deprecated Deprecated.
- #presenter(document) ⇒ Blacklight::DocumentPresenter deprecated Deprecated.
- #search_bar_presenter ⇒ Blacklight::SearchBarPresenter
- #search_bar_presenter_class ⇒ Class
- #show_presenter(document) ⇒ Blacklight::ShowPresenter deprecated Deprecated.
- #show_presenter_class(_document) ⇒ Class deprecated Deprecated.
Document helpers collapse
- #document_has_value?(document, field_config) ⇒ Boolean deprecated Deprecated.
- #document_heading(document = nil) ⇒ String deprecated Deprecated.
-
#document_index_view_type(query_params = params) ⇒ Symbol
Get the current “view type” (and ensure it is a valid type).
- #document_show_html_title(document = nil) ⇒ String deprecated Deprecated.
- #render_document_heading(*args) ⇒ String deprecated Deprecated.
- #render_document_show_field_label(*args) ⇒ String deprecated Deprecated.
- #render_index_field_label(*args) ⇒ String deprecated Deprecated.
- #should_render_index_field?(document, field_config) ⇒ Boolean deprecated Deprecated.
- #should_render_show_field?(document, field_config) ⇒ Boolean deprecated Deprecated.
Search result helpers collapse
-
#render_grouped_response?(response = @response) ⇒ Boolean
Should we render a grouped response (because the response contains a grouped response instead of the normal response).
- #should_show_spellcheck_suggestions?(response) ⇒ Boolean deprecated Deprecated.
-
#with_format(format) { ... } ⇒ Object
Render a partial of an arbitrary format inside a template of a different format.
Methods included from IconHelperBehavior
Methods included from LayoutHelperBehavior
#container_classes, #html_tag_attributes, #main_content_classes, #show_content_classes, #show_sidebar_classes, #sidebar_classes
Methods included from HashAsHiddenFieldsHelperBehavior
Methods included from UrlHelperBehavior
#add_group_facet_params_and_redirect, #bookmarks_export_url, #controller_tracking_method, #link_back_to_catalog, #link_to_document, #link_to_next_document, #link_to_previous_document, #link_to_previous_search, #link_to_query, #session_tracking_path, #start_over_path, #url_for_document
Class Method Details
.blacklight_path ⇒ Object
411 412 413 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 411 def self.blacklight_path @blacklight_path ||= Gem.loaded_specs["blacklight"].full_gem_path end |
Instance Method Details
#application_name ⇒ String
Get the name of this application from an i18n string key: blacklight.application_name Try first in the current locale, then the default locale
19 20 21 22 23 24 25 26 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 19 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_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
126 127 128 129 130 131 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 126 def document_has_value? document, field_config Deprecation.warn self, "document_has_value? is deprecated and will be removed in Blacklight 8. Use DocumentPresenter#has_value? instead." 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)
215 216 217 218 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 215 def document_heading document = nil document ||= @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)
262 263 264 265 266 267 268 269 270 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 262 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
330 331 332 333 334 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 330 def document_presenter(document) Deprecation.silence(Blacklight::BlacklightHelperBehavior) do presenter(document) end end |
#document_presenter_class(document = nil) ⇒ Object
Override this method if you want to use a differnet presenter for your documents
367 368 369 370 371 372 373 374 375 376 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 367 def document_presenter_class(document = nil) Deprecation.silence(Blacklight::BlacklightHelperBehavior) do case action_name when 'show', 'citation' show_presenter_class(document) else index_presenter_class(document) end 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)
229 230 231 232 233 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 229 def document_show_html_title document = nil document ||= @document document_presenter(document).html_title end |
#extra_body_classes ⇒ Array<String>
List of classes to be applied to the <body> element
74 75 76 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 74 def extra_body_classes @extra_body_classes ||= ['blacklight-' + controller.controller_name, 'blacklight-' + [controller.controller_name, controller.action_name].join('-')] end |
#index_presenter(document) ⇒ Blacklight::IndexPresenter
352 353 354 355 356 357 358 359 360 361 362 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 352 def index_presenter(document) Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#index_presenter is deprecated; use #document_presenter instead') if method(:index_presenter_class).owner != Blacklight::BlacklightHelperBehavior Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#index_presenter_class has been overridden; please override #document_presenter_class instead') end Deprecation.silence(Blacklight::BlacklightHelperBehavior) do index_presenter_class(document).new(document, self) end end |
#index_presenter_class(_document) ⇒ Class
390 391 392 393 394 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 390 def index_presenter_class(_document) Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#index_presenter_class is deprecated; use #document_presenter_class instead') blacklight_config.view_config(document_index_view_type, action_name: action_name).document_presenter_class end |
#opensearch_description_tag(title, href) ⇒ String
Open Search discovery tag for HTML <head> links
405 406 407 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 405 def opensearch_description_tag title, href tag :link, href: href, title: title, type: "application/opensearchdescription+xml", rel: "search" end |
#partial_from_blacklight?(partial) ⇒ Boolean
415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 415 def partial_from_blacklight?(partial) path = if Rails::VERSION::MAJOR >= 6 name = partial.split('/').last prefix = partial.split('/').first if partial.include?('/') logger&.debug "Looking for document index partial #{partial}" prefixes = lookup_context.prefixes + [prefix, ""].compact lookup_context.find_all(name, prefixes, true).first&.identifier else lookup_context.find_all(partial, lookup_context.prefixes + [""], true).first&.identifier end path&.starts_with?(Blacklight::BlacklightHelperBehavior.blacklight_path) end |
#presenter(document) ⇒ Blacklight::DocumentPresenter
Returns a document presenter for the given document TODO: Move this to the controller. It can just pass a presenter or set of presenters.
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 307 def presenter(document) Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#presenter is deprecated; use #document_presenter instead') # As long as the presenter methods haven't been overridden, we can use the new behavior if method(:show_presenter).owner == Blacklight::BlacklightHelperBehavior && method(:index_presenter).owner == Blacklight::BlacklightHelperBehavior return document_presenter_class(document).new(document, self) end Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#show_presenter and/or #index_presenter have been overridden; please override #document_presenter instead') Deprecation.silence(Blacklight::BlacklightHelperBehavior) do case action_name when 'show', 'citation' show_presenter(document) else index_presenter(document) end end end |
#render_body_class ⇒ String
Render classes for the <body> element
66 67 68 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 66 def render_body_class extra_body_classes.join " " end |
#render_document_heading(document, options) ⇒ String #render_document_heading(options) ⇒ String
Render the document “heading” (title) in a content tag
247 248 249 250 251 252 253 254 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 247 def render_document_heading(*args) = args. document = args.first tag = .fetch(:tag, :h4) document ||= @document content_tag(tag, document_presenter(document).heading, itemprop: "name") end |
#render_document_show_field_label(options) ⇒ String #render_document_show_field_label(document, options) ⇒ String
Render the show field label for a document
195 196 197 198 199 200 201 202 203 204 205 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 195 def render_document_show_field_label *args = args. document = args.first field = [:field] label = Deprecation.silence(Blacklight::ConfigurationHelperBehavior) do [:label] || document_show_field_label(document, field) end t(:'blacklight.search.show.label', label: label) 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..)
297 298 299 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 297 def render_grouped_response? response = @response response&.grouped? end |
#render_index_field_label(options) ⇒ String #render_index_field_label(document, options) ⇒ String
Render the index field label for a document
Translations for index field labels should go under blacklight.search.fields They are picked up from there by a value “%label” in blacklight.search.index.label
168 169 170 171 172 173 174 175 176 177 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 168 def render_index_field_label *args = args. document = args.first field = [:field] label = Deprecation.silence(Blacklight::ConfigurationHelperBehavior) do [:label] || index_field_label(document, field) end html_escape t(:"blacklight.search.index.#{document_index_view_type}.label", default: :'blacklight.search.index.label', label: label) end |
#render_link_rel_alternates(document = @document, options = {}) ⇒ String
Create <link rel=“alternate”> links from a documents dynamically provided export formats.
Returns empty string if no links available.
48 49 50 51 52 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 48 def render_link_rel_alternates(document = @document, = {}) return if document.nil? document_presenter(document).link_rel_alternates() end |
#render_opensearch_response_metadata ⇒ String
Render OpenSearch headers for this search
58 59 60 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 58 def render partial: 'catalog/opensearch_response_metadata', locals: { response: @response } end |
#render_page_title ⇒ String
Get the page's HTML title
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_bar ⇒ String
Render the search navbar
81 82 83 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 81 def .render end |
#search_bar_presenter ⇒ Blacklight::SearchBarPresenter
89 90 91 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 89 def @search_bar ||= .new(controller, blacklight_config) end |
#search_bar_presenter_class ⇒ Class
397 398 399 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 397 def blacklight_config.view_config(action_name: :index). end |
#should_render_index_field?(document, field_config) ⇒ Boolean
Determine whether to render a given field in the index view.
102 103 104 105 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 102 def should_render_index_field? document, field_config Deprecation.warn self, "should_render_index_field? is deprecated and will be removed in Blacklight 8. Use IndexPresenter#render_field? instead." 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
114 115 116 117 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 114 def should_render_show_field? document, field_config Deprecation.warn self, "should_render_show_field? is deprecated and will be removed in Blacklight 8. Use ShowPresenter#render_field? instead." 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
140 141 142 143 144 145 146 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 140 def should_show_spellcheck_suggestions? response Deprecation.silence(Blacklight::ConfigurationHelperBehavior) do # The spelling response field may be missing from non solr repositories. response.total <= spell_check_max && (response.spelling&.words&.any? || response.spelling&.collation&.present? || false) end end |
#show_presenter(document) ⇒ Blacklight::ShowPresenter
338 339 340 341 342 343 344 345 346 347 348 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 338 def show_presenter(document) Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#show_presenter is deprecated; use #document_presenter instead') if method(:show_presenter_class).owner != Blacklight::BlacklightHelperBehavior Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#show_presenter_class has been overridden; please override #document_presenter_class instead') end Deprecation.silence(Blacklight::BlacklightHelperBehavior) do show_presenter_class(document).new(document, self) end end |
#show_presenter_class(_document) ⇒ Class
Override this method if you want to use a different presenter class
382 383 384 385 386 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 382 def show_presenter_class(_document) Deprecation.warn(Blacklight::BlacklightHelperBehavior, '#show_presenter_class is deprecated; use #document_presenter_class instead') blacklight_config.view_config(:show, action_name: action_name).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)
282 283 284 285 286 287 288 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 282 def with_format(format) old_formats = formats self.formats = [format] yield self.formats = old_formats nil end |