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
422 423 424 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 422 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
137 138 139 140 141 142 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 137 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)
226 227 228 229 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 226 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)
273 274 275 276 277 278 279 280 281 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 273 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 |
#document_presenter(document) ⇒ Object
Returns a document presenter for the given document
341 342 343 344 345 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 341 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
378 379 380 381 382 383 384 385 386 387 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 378 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)
240 241 242 243 244 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 240 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
363 364 365 366 367 368 369 370 371 372 373 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 363 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
401 402 403 404 405 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 401 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
416 417 418 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 416 def opensearch_description_tag title, href tag :link, href: href, title: title, type: "application/opensearchdescription+xml", rel: "search" end |
#partial_from_blacklight?(partial) ⇒ Boolean
426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 426 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.
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 318 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
258 259 260 261 262 263 264 265 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 258 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
206 207 208 209 210 211 212 213 214 215 216 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 206 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..)
308 309 310 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 308 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
179 180 181 182 183 184 185 186 187 188 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 179 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 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 81 def if == Blacklight::SearchBarPresenter && partial_from_blacklight?(Blacklight::SearchBarPresenter.partial) component_class = blacklight_config&.view_config(document_index_view_type)&. || Blacklight::SearchBarComponent render component_class.new( url: search_action_url, advanced_search_url: search_action_url(action: 'advanced_search'), params: search_state.params_for_search.except(:qt), search_fields: Deprecation.silence(Blacklight::ConfigurationHelperBehavior) { search_fields }, autocomplete_path: search_action_path(action: :suggest) ) else .render end end |
#search_bar_presenter ⇒ Blacklight::SearchBarPresenter
100 101 102 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 100 def @search_bar ||= .new(controller, blacklight_config) end |
#search_bar_presenter_class ⇒ Class
408 409 410 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 408 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.
113 114 115 116 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 113 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
125 126 127 128 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 125 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
151 152 153 154 155 156 157 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 151 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
349 350 351 352 353 354 355 356 357 358 359 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 349 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
393 394 395 396 397 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 393 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)
293 294 295 296 297 298 299 |
# File 'app/helpers/blacklight/blacklight_helper_behavior.rb', line 293 def with_format(format) old_formats = formats self.formats = [format] yield self.formats = old_formats nil end |