Module: ApplicationHelper

Includes:
PrefixHelper, RequestsHelper
Included in:
BulkImportController, TopContainersController
Defined in:
frontend/app/helpers/application_helper.rb,
public/app/helpers/application_helper.rb

Constant Summary

Constants included from RequestsHelper

RequestsHelper::VALID_EMAIL_REGEX

Instance Method Summary collapse

Methods included from RequestsHelper

#pass_email_requirements?, #repo_has_valid_email?

Methods included from PrefixHelper

app_prefix, #app_prefix, app_prefix_js, #app_prefix_js

Instance Method Details

#add_new_event_url(record) ⇒ Object



324
325
326
327
328
329
330
# File 'frontend/app/helpers/application_helper.rb', line 324

def add_new_event_url(record)
  if record.jsonmodel_type == "agent"
    url_for(:controller => :events, :action => :new, :agent_uri => record.uri,  :event_type => "${event_type")
  else
    url_for(:controller => :events, :action => :new, :record_uri => record.uri, :record_type => record.jsonmodel_type, :event_type => "${event_type}")
  end
end

#bootstrap_class_for(flash_type) ⇒ Object



5
6
7
# File 'public/app/helpers/application_helper.rb', line 5

def bootstrap_class_for flash_type
  { success: "alert-success", error: "alert-danger", alert: "alert-warning", notice: "alert-info" }[flash_type.to_sym] || flash_type.to_s
end

#button_confirm_action(label, target, opts = {}) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
# File 'frontend/app/helpers/application_helper.rb', line 190

def button_confirm_action(label, target, opts = {})
  btn_opts = {
    :"data-target" => target,
    :method => :post,
    :class => "btn",
    :"data-confirmation" => true,
    :"data-authenticity_token" => form_authenticity_token,
    :type => "button"
  }.merge(opts)

  button_tag(label, btn_opts)
end

#button_delete_action(url, opts = {}) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
# File 'frontend/app/helpers/application_helper.rb', line 215

def button_delete_action(url, opts = {})
  button_confirm_action(opts[:label] || I18n.t("actions.delete"),
                        url,
                        {
                          :class => "btn btn-sm btn-danger delete-record",
                          :"data-title" => I18n.t("actions.delete_confirm_title"),
                          :"data-message" => I18n.t("actions.delete_confirm_message"),
                          :"data-confirm-btn-label" => "#{I18n.t("actions.delete")}",
                          :"data-confirm-btn-class" => "btn-danger"
                        }.merge(opts))
end

#button_delete_multiple_action(target_action) ⇒ Object



228
229
230
231
232
233
234
235
236
237
# File 'frontend/app/helpers/application_helper.rb', line 228

def button_delete_multiple_action(target_action)
  button_delete_action(url_for(:controller => :batch_delete, :action => target_action), {
    :class => "btn btn-sm btn-danger multiselect-enabled",
    :"data-multiselect" => "#tabledSearchResults",
    :"data-title" => I18n.t("actions.delete_multiple_confirm_title"),
    :"data-message" => I18n.t("actions.delete_multiple_confirm_message"),
    :"data-confirm-btn-label" => "#{I18n.t("actions.delete_multiple")}",
    :disabled => "disabled"
  })
end

#button_edit_multiple_action(target_controller, target_action = :batch, opts = {}) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'frontend/app/helpers/application_helper.rb', line 239

def button_edit_multiple_action(target_controller, target_action = :batch, opts = {} )
  label = opts[:label] || I18n.t("actions.edit_batch")
  btn_opts = {
    :"data-target" => url_for(:controller => target_controller, :action => target_action),
    :class => "btn btn-sm btn-default multiselect-enabled edit-batch",
    :method => "post",
    :type => "button",
    :"data-multiselect" => "#tabledSearchResults",
    :"data-confirmation" => true,
    :"data-title" => I18n.t("actions.edit_multiple_confirm_title"),
    :"data-message" => I18n.t("actions.edit_multiple_confirm_message"),
    :"data-confirm-btn-label" => "#{I18n.t("actions.edit_multiple")}",
    :"data-authenticity_token" => form_authenticity_token,
    :disabled => "disabled"
  }.merge(opts)

  button_tag(label, btn_opts)
end

#button_get_selector(label, target, opts = {}) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
# File 'frontend/app/helpers/application_helper.rb', line 203

def button_get_selector(label, target, opts = {})
  btn_opts = {
    :"data-target" => target,
    :method => :post,
    :class => "btn",
    :"data-confirmation" => true,
    :"data-authenticity_token" => form_authenticity_token,
    :type => "button"
  }.merge(opts)
  button_tag(label, btn_opts)
end

#clean_mixed_content(content) ⇒ Object



314
315
316
317
318
# File 'frontend/app/helpers/application_helper.rb', line 314

def clean_mixed_content(content)
  content = content.to_s
  return content if content.blank?
  MixedContentParser::parse(content, url_for(:root), { :wrap_blocks => false } ).to_s.html_safe
end

#current_repoObject



147
148
149
150
151
152
153
154
155
156
157
158
# File 'frontend/app/helpers/application_helper.rb', line 147

def current_repo
  return nil if session[:repo].blank?
  return @current_repo if @current_repo != nil

  @current_repo = false

  MemoryLeak::Resources.get(:repository).each do |repo|
    @current_repo = repo if repo['uri'] === session[:repo]
  end

  @current_repo
end

#current_userObject



166
167
168
# File 'frontend/app/helpers/application_helper.rb', line 166

def current_user
  session[:user]
end

#display_audit_info(hash, opts = {}) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'frontend/app/helpers/application_helper.rb', line 258

def display_audit_info(hash, opts = {})
  fmt = opts[:format] || 'wide'
  ark_url = nil
  if AppConfig[:arks_enabled] && hash['ark_name']
    # watch out! hash might be from a jsonmodel or from a solr result
    # don't blame me, i'm just trying to fit in around here
    ark_url = if hash["ark_name"].is_a?(Array)
                hash["ark_name"].first
              else
                hash["ark_name"]["current"]
              end
  end
  html = "<div class='audit-display-#{fmt}'><small>"
  if hash['create_time'] and hash['user_mtime']
    if fmt == 'wide'
      html << "<strong>#{I18n.t("search_results.created")} #{hash['created_by']}</strong>"
      html << " #{Time.parse(hash['create_time']).getlocal}"
      html << ' | '
      html << "<strong>#{I18n.t("search_results.modified")} #{hash['last_modified_by']}</strong>"
      html << " #{Time.parse(hash['user_mtime']).getlocal}"
      html << ' | '
      html << "<label for=\"uri\"><strong>URI:</strong> </label>"
      html << "<input type=\"text\" id=\"uri\" readonly=\"1\" value=\"#{hash['uri']}\" size=\"#{hash['uri'].length}\" style=\"background: #f1f1f1 !important; border: none !important; font-family: monospace;\"/>"
      if !ark_url.nil?
        html << ' | '
        html << "<label for=\"ark\"><strong>ARK:</strong> </label>"
        html << "<input type=\"text\" id=\"ark\" readonly=\"1\" value=\"#{ark_url}\" size=\"#{ark_url.length}\" style=\"background: #f1f1f1 !important; border: none !important; font-family: monospace;\"/>"
      end
    else
      html << "<dl>"
      html << "<dt>#{I18n.t("search_results.created")} #{hash['created_by']}</dt>"
      html << "<dd>#{Time.parse(hash['create_time']).getlocal}</dd>"
      html << "<dt>#{I18n.t("search_results.modified")} #{hash['last_modified_by']}</dt>"
      html << "<dd>#{Time.parse(hash['user_mtime']).getlocal}</dd>"
      html << "</dl>"
    end
  end
  html << "</small></div><div class='clearfix'></div>"
  html.html_safe
end

#edit_mode?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'frontend/app/helpers/application_helper.rb', line 138

def edit_mode?
  ['edit', 'update'].include?(controller.action_name)
end

#export_csv(search_data) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'frontend/app/helpers/application_helper.rb', line 336

def export_csv(search_data)
  results = search_data["results"]

  headers = results.inject([]) { |h, r| h | r.keys }
  headers.delete("json")

  CSV.generate do |csv|
    csv << headers
    results.each do |result|
      data = []
      headers.each do |h|
        unless result.include?(h)
          data << nil
          next
        end
        v = result[h]
        v = v.join(";") if v.is_a?(Array)
        v = v.to_s
        v.gsub!('\"', '""')
        v.delete!("\n")
        v.delete!(",")
        data << v
      end
      csv << data
    end
  end
end

#flash_messages(opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'public/app/helpers/application_helper.rb', line 9

def flash_messages(opts = {})
    flash.each do |msg_type, message|
      concat((:div, message, class: "alert #{bootstrap_class_for(msg_type)} alert-dismissible", role: 'alert') do
        concat((:button, class: 'close', data: { dismiss: 'alert' }) do
   concat (:span, '&times;'.html_safe, 'aria-hidden' => true)
   concat (:span, 'Close', class: 'sr-only')
 end)
        concat (message.is_a?(Array) ? message.join('<br/>').html_safe : message.html_safe)
      end)
    end
    nil
  end
end

#full_mode?Boolean

Returns:

  • (Boolean)


419
420
421
# File 'frontend/app/helpers/application_helper.rb', line 419

def full_mode?
  user_can?("show_full_agents") || user_can?("administer_system")
end

#get_subject_icon_class(obj) ⇒ Object

ANW-521: given an object, if it is a subject, return the class needed to display the correct icon in the interface



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'frontend/app/helpers/application_helper.rb', line 376

def get_subject_icon_class(obj)
  if obj['_resolved']
    if obj['_resolved']['jsonmodel_type'] &&
       obj['_resolved']['jsonmodel_type'] == "subject"

      term_type = obj['_resolved']['terms'][0]["term_type"] rescue nil

      case term_type
      when "cultural_context"
        return "subject_type_cultural_context"
      when "function"
        return "subject_type_function"
      when "genre_form"
        return "subject_type_genre_form"
      when "geographic"
        return "subject_type_geographic"
      when "occupation"
        return "subject_type_occupation"
      when "style_period"
        return "subject_type_style_period"
      when "technique"
        return "subject_type_technique"
      when "temporal"
        return "subject_type_temporal"
      when "topical"
        return "subject_type_topical"
      when "uniform_title"
        return "subject_type_uniform_title"
      end
    end
  end
end

#has_agent_subrecords?(agent) ⇒ Boolean

Returns:

  • (Boolean)


423
424
425
426
427
428
429
# File 'frontend/app/helpers/application_helper.rb', line 423

def has_agent_subrecords?(agent)
  # agent_person has all agent subrecord types so is ideal for finding any potential subrecord
  JSONModel(:agent_person).properties_by_tag('agent_subrecord').map(&:first).map(&:to_sym).find do |subrecord|
    return unless agent.methods.include?(subrecord)
    agent.send(subrecord).length.positive?
  end
end

#has_permission_for_controller?(session, name) ⇒ Boolean

Returns:

  • (Boolean)


300
301
302
303
304
305
# File 'frontend/app/helpers/application_helper.rb', line 300

def has_permission_for_controller?(session, name)
  controller_class_name = "#{name}_controller".classify
  controller_class = Kernel.const_get(controller_class_name)

  controller_class.can_access?(self, :index)
end

#include_controller_jsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'frontend/app/helpers/application_helper.rb', line 5

def include_controller_js
  scripts = ""

  scripts += javascript_include_tag "#{controller.controller_name}" if File.exist?("#{Rails.root}/app/assets/javascripts/#{controller_name}.js") || File.exist?("#{Rails.root}/app/assets/javascripts/#{controller_name}.js.erb")

  scripts += javascript_include_tag "#{controller.controller_name}.#{controller.action_name}" if File.exist?("#{Rails.root}/app/assets/javascripts/#{controller_name}.#{controller.action_name}.js") || File.exist?("#{Rails.root}/app/assets/javascripts/#{controller_name}.#{controller.action_name}.js.erb")

  if ["new", "create", "edit", "update"].include?(controller.action_name)
    scripts += javascript_include_tag "#{controller.controller_name}.crud" if File.exist?("#{Rails.root}/app/assets/javascripts/#{controller_name}.crud.js") || File.exist?("#{Rails.root}/app/assets/javascripts/#{controller_name}.crud.js.erb")
  end

  if ["batch_create"].include?(controller.action_name)
    scripts += javascript_include_tag "#{controller.controller_name}.batch" if File.exist?("#{Rails.root}/app/assets/javascripts/#{controller_name}.batch.js") || File.exist?("#{Rails.root}/app/assets/javascripts/#{controller_name}.batch.js.erb")
  end

  if ["defaults", "update_defaults"].include?(controller.action_name)
    ctrl_name = controller.controller_name == 'archival_objects' ? 'resources' : controller.controller_name

    scripts += javascript_include_tag "#{ctrl_name}.crud" if File.exist?("#{Rails.root}/app/assets/javascripts/#{ctrl_name}.crud.js") || File.exist?("#{Rails.root}/app/assets/javascripts/#{ctrl_name}.crud.js.erb")
  end


  scripts.html_safe
end

#include_theme_cssObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'frontend/app/helpers/application_helper.rb', line 30

def include_theme_css
  begin
    css = ""
    css += stylesheet_link_tag("themes/#{ArchivesSpace::Application.config.frontend_theme}/bootstrap", :media => "all")
    css += stylesheet_link_tag("themes/#{ArchivesSpace::Application.config.frontend_theme}/application", :media => "all")
    css.html_safe
  rescue
    # On app startup in dev mode, the above call triggers the LESS stylesheets
    # to compile, and there seems to be a problem with two threads doing this
    # concurrently.  If things go badly, just retry.

    Rails.logger.warn("Retrying include_theme_css: #{$!}")

    sleep 1
    retry
  end
end

#inline?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'frontend/app/helpers/application_helper.rb', line 142

def inline?
  params[:inline] === "true"
end

#job_typesObject



161
162
163
# File 'frontend/app/helpers/application_helper.rb', line 161

def job_types
  MemoryLeak::Resources.get(:job_types)
end


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'frontend/app/helpers/application_helper.rb', line 114

def link_to_help(opts = {})
  return if not ArchivesSpaceHelp.enabled?
  return if opts.has_key?(:topic) and not ArchivesSpaceHelp.topic?(opts[:topic])

  href = (opts.has_key? :topic) ? ArchivesSpaceHelp.url_for_topic(opts[:topic]) : ArchivesSpaceHelp.base_url

  label = opts[:label] || I18n.t("help.icon")

  label_text = "<span class='sr-only'> Visit the " + I18n.t("help.help_center") + "</span>"

  title = (opts.has_key? :topic) ? I18n.t("help.topics.#{opts[:topic]}", :default => I18n.t("help.default_tooltip", :default => "")) : I18n.t("help.default_tooltip", :default => "")

  link_to(
          label.html_safe + label_text.html_safe,
          href,
          {
            :target => "_blank",
            :title => title,
            :class => "context-help has-tooltip",
            "data-placement" => "left",
          }.merge(opts[:link_opts] || {})
         )
end

Merge new_params into params and generate a link.

Intended to avoid security issues associated with passing user-generated params as the opts for link_to (which allows them to set the host, controller, etc.)



369
370
371
372
373
# File 'frontend/app/helpers/application_helper.rb', line 369

def link_to_merge_params(label, new_params, html_options = {})
  link_to(label,
          params.except(:controller, :action).to_unsafe_h.merge(new_params),
          html_options)
end

#proxy_localhost?Boolean

Returns:

  • (Boolean)


320
321
322
# File 'frontend/app/helpers/application_helper.rb', line 320

def proxy_localhost?
  AppConfig[:public_proxy_url] =~ /localhost/
end

#render_aspace_partial(args) ⇒ Object

See: ApplicationController#render_aspace_partial



309
310
311
312
# File 'frontend/app/helpers/application_helper.rb', line 309

def render_aspace_partial(args)
  defaults = {:formats => [:html], :handlers => [:erb]}
  return render(defaults.merge(args))
end

#render_token(opts) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'frontend/app/helpers/application_helper.rb', line 88

def render_token(opts)
  popover = "<div class='btn-group'>"
  link_opts = {:class => "btn btn-mini"}
  link_opts.merge!({:target => "_blank"}) if opts[:inside_token_editor] || opts[:inside_linker_browse]
  popover_url = url_for :controller => :resolver, :action => :resolve_readonly
  # TrimPath templates break if Rails escapes ${VALUE} style tokens:
  popover_url += "?uri=#{opts[:uri]}"
  popover += link_to I18n.t("actions.view"), popover_url, link_opts
  popover += "</div>"

  popover_template = "<div class='popover token-popover'><div class='arrow'></div><div class='popover-inner'><div class='popover-content'><p></p></div></div></div>"

  html = "<div class='"
  html += "token " if not opts[:inside_token_editor]
  html += "#{opts[:type]} has-popover' data-trigger='#{opts[:trigger] || "custom"}' data-html='true' data-placement='#{opts[:placement] || "bottom"}' data-content=\"#{CGI.escape_html(popover)}\" data-template=\"#{popover_template}\" tabindex='1'>"

  if opts[:icon_class]
    html += "<span class='icon-token #{opts[:icon_class]}'></span>"
  else
    html += "<span class='icon-token'></span>"
  end
  html += clean_mixed_content(opts[:label])
  html += "</div>"
  html.html_safe
end

#set_title(title) ⇒ Object



49
50
51
# File 'frontend/app/helpers/application_helper.rb', line 49

def set_title(title)
  @title = title
end

#setup_context(options) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'frontend/app/helpers/application_helper.rb', line 53

def setup_context(options)
  breadcrumb_trail = options[:trail] || []

  if options.has_key? :object
    object = options[:object]

    type = options[:type] || object["jsonmodel_type"]
    controller = options[:controller] || type.to_s.pluralize

    title = (options[:title] || object["title"] || object["username"]).to_s

    breadcrumb_trail.push([I18n.t("#{controller.to_s.singularize}._plural"), {:controller => controller, :action => :index}])

    if object.id
      breadcrumb_trail.push([title, {:controller => controller, :action => :show}])
      breadcrumb_trail.last.last[:id] = object.id unless object['username']

      if ["edit", "update"].include? action_name
        breadcrumb_trail.push([I18n.t("actions.edit")])
        set_title("#{I18n.t("#{type}._plural")} | #{title} | #{I18n.t("actions.edit")}")
      else
        set_title("#{I18n.t("#{type}._plural")} | #{title}")
      end
    else # new object
      breadcrumb_trail.push([options[:title] || "#{I18n.t("#{type}.new_title")}"])
      set_title("#{I18n.t("#{controller.to_s.singularize}._plural")} | #{options[:title] || I18n.t("actions.new_prefix")}")
    end
  elsif options.has_key? :title
    set_title(options[:title])
    breadcrumb_trail.push([options[:title]])
  end

  render_aspace_partial(:partial =>"shared/breadcrumb", :layout => false , :locals => { :trail => breadcrumb_trail }).to_s if options[:suppress_breadcrumb] != true
end

#show_external_ids?Boolean

Returns:

  • (Boolean)


332
333
334
# File 'frontend/app/helpers/application_helper.rb', line 332

def show_external_ids?
  AppConfig[:show_external_ids] == true
end

#supported_locales_defaultObject



409
410
411
412
413
# File 'frontend/app/helpers/application_helper.rb', line 409

def supported_locales_default
  {
    default: user_prefs.key?('locale') ? user_prefs['locale'] : I18n.default_locale.to_s
  }
end

#supported_locales_optionsObject



415
416
417
# File 'frontend/app/helpers/application_helper.rb', line 415

def supported_locales_options
  I18n.supported_locales.map { |k, v| [t("enumerations.language_iso639_2.#{v}"), k] }
end

#wrap_with_tooltip(text, i18n_path, classes) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'frontend/app/helpers/application_helper.rb', line 171

def wrap_with_tooltip(text, i18n_path, classes)
  tooltip = I18n.t_raw(i18n_path, :default => '')
  if tooltip.empty?
    return text
  else
    options = {}
    options[:title] = tooltip
    options["data-placement"] = "bottom"
    options["data-html"] = true
    options["data-delay"] = 500
    options["data-trigger"] = "manual"
    options["data-template"] = '<div class="tooltip archivesspace-help"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
    options[:class] = " has-tooltip #{classes}"

    (:span, text, options)
  end
end