Module: Spotlight::ApplicationHelper

Includes:
CropHelper, CrudLinkHelpers, ExhibitThemeHelper, LanguagesHelper, MetaHelper, TitleHelper
Defined in:
app/helpers/spotlight/application_helper.rb

Overview

General spotlight application helpers

Instance Method Summary collapse

Methods included from LanguagesHelper

#add_exhibit_language_dropdown_options, #default_language?, #locale_selecter_dropown_options

Methods included from CropHelper

#form_prefix, #iiif_cropper_tag, #iiif_cropper_tags, #iiif_upload_tag, #input_prefix

Methods included from MetaHelper

#add_browse_meta_content, #add_document_meta_content, #add_exhibit_meta_content, #add_page_meta_content, #browse_opengraph_content, #browse_twitter_card_content, #build_tags, #card, #description, #document_opengraph_content, #document_twitter_card_content, #exhibit_opengraph_content, #exhibit_twitter_card_content, #meta_image, #page_opengraph_content, #page_twitter_card_content

Methods included from TitleHelper

#accessibility_page_title, #configuration_page_title, #curation_page_title, #page_title, #set_html_page_title

Methods included from ExhibitThemeHelper

#current_exhibit_theme, #exhibit_stylesheet_link_tag

Methods included from CrudLinkHelpers

#action_label, #cancel_link, #create_link, #delete_link, #edit_link, #exhibit_create_link, #exhibit_delete_link, #exhibit_edit_link, #exhibit_view_link, #nav_link, #view_link

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Can search for named routes directly in the main app, omitting the “main_app.” prefix



62
63
64
65
66
67
68
# File 'app/helpers/spotlight/application_helper.rb', line 62

def method_missing(method, *args, &)
  if main_app_url_helper?(method)
    main_app.send(method, *args)
  else
    super
  end
end

Instance Method Details

#additional_locale_routing_scopesObject



56
57
58
# File 'app/helpers/spotlight/application_helper.rb', line 56

def additional_locale_routing_scopes
  [spotlight, main_app]
end

#application_nameObject

Give the application name a chance to include the exhibit title



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/spotlight/application_helper.rb', line 16

def application_name
  name = site_title
  name ||= super

  if current_exhibit
    t :'spotlight.application_name',
      exhibit: current_exhibit.title,
      application_name: name,
      default: t('spotlight.application_name', locale: I18n.default_locale, exhibit: current_exhibit.title, application_name: name)
  else
    name
  end
end

#available_view_fieldsObject



151
152
153
# File 'app/helpers/spotlight/application_helper.rb', line 151

def available_view_fields
  current_exhibit.blacklight_configuration.default_blacklight_config.view.to_h.reject { |_k, v| v.if == false }
end

#blacklight_view_config_for_search_block(block) ⇒ Object

Return a copy of the blacklight configuration that only includes views conifgured by our block



107
108
109
110
111
112
113
114
# File 'app/helpers/spotlight/application_helper.rb', line 107

def blacklight_view_config_for_search_block(block)
  return {} if block.view.blank?

  # Reject any views that aren't configured to display for this block
  blacklight_config.view.select do |view, _|
    block.view.include? view.to_s
  end
end

#block_document_index_view_type(block) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'app/helpers/spotlight/application_helper.rb', line 116

def block_document_index_view_type(block)
  views = blacklight_view_config_for_search_block(block)

  selected_view = if views.key? document_index_view_type
                    document_index_view_type
                  else
                    views.keys.first
                  end

  selected_view || default_document_index_view_type
end

#content?(field) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'app/helpers/spotlight/application_helper.rb', line 34

def content?(field)
  return content_for?(field) unless Rails.configuration.action_view.annotate_rendered_view_with_filenames && content_for(field).present?

  stripped_content = content_for(field).gsub(/<!-- BEGIN .+? -->/, '').gsub(/<!-- END .+? -->/, '').strip
  stripped_content.present?
end

#current_page_for_locale(locale) ⇒ Object

Returns the url for the current page in the new locale. This may be overridden in downstream applications where our naive use of ‘url_for` is insufficient to generate the expected routes



44
45
46
47
48
49
50
51
52
53
54
# File 'app/helpers/spotlight/application_helper.rb', line 44

def current_page_for_locale(locale)
  initial_exception = nil

  ([self] + additional_locale_routing_scopes).each do |scope|
    return scope.public_send(:url_for, params.to_unsafe_h.merge(locale:))
  rescue ActionController::UrlGenerationError => e
    initial_exception ||= e
  end

  raise initial_exception
end

#document_action_path(action_opts, url_opts = nil) ⇒ Object

Override Blacklight’s #document_action_path helper to add the current exhibit context



77
78
79
80
81
82
83
84
# File 'app/helpers/spotlight/application_helper.rb', line 77

def document_action_path(action_opts, url_opts = nil)
  if current_exhibit
    model_name = current_exhibit.blacklight_config.document_model.model_name
    spotlight.send(action_opts.path || "#{action_opts.key}_exhibit_#{model_name.collection}_path", url_opts)
  else
    super
  end
end

#render_constraints(localized_params = nil, local_search_state = nil) ⇒ Object



166
167
168
169
170
171
172
173
# File 'app/helpers/spotlight/application_helper.rb', line 166

def render_constraints(localized_params = nil, local_search_state = nil)
  return super if defined?(super)

  local_search_state ||= convert_to_search_state(localized_params || search_state)
  constraints_component = blacklight_config&.view_config(document_index_view_type)&.constraints_component
  constraints_component ||= Blacklight::ConstraintsComponent
  render(constraints_component.new(search_state: local_search_state))
end

#render_document_class(document = @document) ⇒ Object

Override Blacklight’s #render_document_class to inject a private class



98
99
100
101
102
103
# File 'app/helpers/spotlight/application_helper.rb', line 98

def render_document_class(document = @document)
  [
    super,
    ("#{document_class_prefix}private" if document.private?(current_exhibit))
  ].join(' ')
end

#render_search_barObject



155
156
157
158
159
160
161
162
163
164
# File 'app/helpers/spotlight/application_helper.rb', line 155

def render_search_bar
  return super if defined?(super)

  render((blacklight_config&.view_config(document_index_view_type)&.search_bar_component || Blacklight::SearchBarComponent).new(
           url: search_action_url,
           advanced_search_url: search_action_url(action: 'advanced_search'),
           params: search_state.params_for_search.except(:qt),
           autocomplete_path: suggest_index_catalog_path
         ))
end

#respond_to_missing?(method, *args) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/helpers/spotlight/application_helper.rb', line 70

def respond_to_missing?(method, *args)
  main_app_url_helper?(method) || super
end

#select_deselect_action(id) ⇒ Object

Create checkbox for selecting/deselecting metadata fields for a given view in metadata configuration



136
137
138
139
140
141
142
143
144
# File 'app/helpers/spotlight/application_helper.rb', line 136

def select_deselect_action(id)
  check_box_tag(
    id,
    class: 'metadata-select',
    data: {
      behavior: 'metadata-select'
    }
  )
end

#selected_search_block_views(block) ⇒ Object

Return the list of views that are configured to display for a block



129
130
131
132
133
# File 'app/helpers/spotlight/application_helper.rb', line 129

def selected_search_block_views(block)
  block.as_json[:data].select do |_key, value|
    value == 'on'
  end.keys.map(&:to_s)
end

#site_titleObject



30
31
32
# File 'app/helpers/spotlight/application_helper.rb', line 30

def site_title
  current_site.title.presence
end

#uploaded_field_label(config) ⇒ Object



146
147
148
149
# File 'app/helpers/spotlight/application_helper.rb', line 146

def uploaded_field_label(config)
  solr_field = Array(config.solr_field || config.field_name).first.to_s
  blacklight_config.index_fields[solr_field]&.label || config.label || t(".#{solr_field}")
end

#url_to_tag_facet(tag) ⇒ Object

Helper to turn tag data into facets



88
89
90
91
92
93
94
# File 'app/helpers/spotlight/application_helper.rb', line 88

def url_to_tag_facet(tag)
  if current_exhibit
    search_action_url(search_state.reset.filter(:exhibit_tags).add(tag).params)
  else
    search_action_url(q: tag)
  end
end