Module: GeoblacklightAdminHelper

Includes:
Pagy::Frontend
Defined in:
app/helpers/geoblacklight_admin_helper.rb

Overview

GeoblacklightAdminHelper

This module provides helper methods for the GeoBlacklight admin interface. It includes methods for handling JSON data, generating paths, formatting flash messages, and more.

Instance Method Summary collapse

Instance Method Details

#assets_dct_references_optionsString

Generates options for asset DCT references.

Returns:

  • (String)

    the escaped JavaScript string of options



205
206
207
# File 'app/helpers/geoblacklight_admin_helper.rb', line 205

def assets_dct_references_options
  escape_javascript(options_for_select(I18n.t("activemodel.asset_enum_values.document/reference.category").invert.sort.insert(0, ["Choose Reference Type", nil]))).to_s
end

#b1g_institution_codesHash

Provides a mapping of institution codes to institution names.

Returns:

  • (Hash)

    a hash mapping institution codes to names



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/geoblacklight_admin_helper.rb', line 55

def b1g_institution_codes
  {
    "01" => "Indiana University",
    "02" => "University of Illinois Urbana-Champaign",
    "03" => "University of Iowa",
    "04" => "University of Maryland",
    "05" => "University of Minnesota",
    "06" => "Michigan State University",
    "07" => "University of Michigan",
    "08" => "Purdue University",
    "09" => "Pennsylvania State University",
    "10" => "University of Wisconsin-Madison",
    "11" => "The Ohio State University",
    "12" => "University of Chicago",
    "13" => "University of Nebraska-Lincoln",
    "14" => "Rutgers University-New Brunswick",
    "15" => "Northwestern University"
  }
end

#bookmarks_badgeString

Generates an HTML badge for bookmarks.

Returns:

  • (String)

    the HTML string for the bookmarks badge



78
79
80
81
# File 'app/helpers/geoblacklight_admin_helper.rb', line 78

def bookmarks_badge
  bookmarks_classes = ["badge", "badge-dark"]
  "<span class='#{bookmarks_classes.join(" ")}' id='bookmarks-count'>#{current_user.bookmarks.size}</span>"
end

#diff_class(char) ⇒ String

Maps a character to a CSS class for diff highlighting.

Parameters:

  • char (String)

    the character representing a diff operation

Returns:

  • (String)

    the corresponding CSS class



149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/helpers/geoblacklight_admin_helper.rb', line 149

def diff_class(char)
  case char
  when "~"
    "table-warning"
  when "-"
    "table-danger"
  when "+"
    "table-success"
  else
    ""
  end
end

#flash_class(level) ⇒ String

Maps flash message levels to CSS classes.

Parameters:

  • level (String)

    the flash message level

Returns:

  • (String)

    the corresponding CSS class



42
43
44
45
46
47
48
49
50
# File 'app/helpers/geoblacklight_admin_helper.rb', line 42

def flash_class(level)
  alerts = {
    "notice" => "alert alert-info",
    "success" => "alert alert-success",
    "error" => "alert alert-error",
    "alert" => "alert alert-error"
  }
  alerts[level]
end

#flat_hash_key(names) ⇒ String

Generates a key for a flattened hash from an array of names.

Parameters:

  • names (Array)

    the array of names

Returns:

  • (String)

    the generated key



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

def flat_hash_key(names)
  names = Array.new(names)
  name = names.shift.to_s.dup
  names.each do |n|
    name << "[#{n}]"
  end
  name
end

#flatten_hash(hash, ancestor_names = []) ⇒ Hash

Flattens a nested hash into a single-level hash with keys representing the nested structure.

Parameters:

  • hash (Hash)

    the hash to flatten

  • ancestor_names (Array) (defaults to: [])

    the ancestor keys for nested hashes

Returns:

  • (Hash)

    the flattened hash



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/helpers/geoblacklight_admin_helper.rb', line 115

def flatten_hash(hash, ancestor_names = [])
  flat_hash = {}
  hash.each do |k, v|
    names = Array.new(ancestor_names)
    names << k
    if v.is_a?(Hash)
      flat_hash.merge!(flatten_hash(v, names))
    else
      key = flat_hash_key(names)
      key += "[]" if v.is_a?(Array)
      flat_hash[key] = v
    end
  end

  flat_hash
end

Generates a link to the admin import page for a given import.

Parameters:

  • import (Object)

    the import object

Returns:

  • (String)

    the HTML link to the admin import page



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

def link_to_admin_import(import)
  path = admin_documents_path(
    {
      f: {b1g_geom_import_id_ssi: [import]}
    }
  )

  link_to import.name, path
end

Generates a link to the GeoBlacklight import page with optional state.

Parameters:

  • label (String)

    the link label

  • import (Object)

    the import object

  • state (Boolean) (defaults to: false)

    the publication state

Returns:

  • (String)

    the HTML link to the GeoBlacklight import page



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'app/helpers/geoblacklight_admin_helper.rb', line 182

def link_to_gbl_import(label, import, state = false)
  path = if state
    blacklight_path(
      {
        f: {b1g_geom_import_id_ssi: [import]},
        publication_state: state
      }
    )
  else
    blacklight_path(
      {
        f: {b1g_geom_import_id_ssi: [import]},
        publication_state: "*"
      }
    )
  end

  link_to(label, path)
end

#no_json_blanks(value) ⇒ String, ...

Removes blank values from JSON data.

Parameters:

  • value (String, Array)

    the value to check for presence

Returns:

  • (String, Array, nil)

    the original value if present, otherwise nil



18
19
20
21
22
23
24
25
# File 'app/helpers/geoblacklight_admin_helper.rb', line 18

def no_json_blanks(value)
  case value
  when String
    value.presence
  when Array
    value.join.blank? ? nil : value
  end
end

#notifications_badgeString

Generates an HTML badge for notifications.

Returns:

  • (String)

    the HTML string for the notifications badge



86
87
88
89
90
91
# File 'app/helpers/geoblacklight_admin_helper.rb', line 86

def notifications_badge
  notifications_classes = ["badge"]
  notifications_classes << "badge-dark" if current_user.notifications.unread.empty?
  notifications_classes << "badge-danger" if current_user.notifications.unread.size.positive?
  "<span class='#{notifications_classes.join(" ")}' id='notification-count'>#{current_user.notifications.unread.size}</span>"
end

#params_as_hidden_fields(params) ⇒ String

Converts parameters into hidden form fields.

Parameters:

  • params (Hash)

    the parameters to convert

Returns:

  • (String)

    the HTML string of hidden fields



97
98
99
100
101
102
103
104
105
106
107
# File 'app/helpers/geoblacklight_admin_helper.rb', line 97

def params_as_hidden_fields(params)
  hidden_fields = []
  flatten_hash(params).each do |name, value|
    value = Array.wrap(value)
    value.each do |v|
      hidden_fields << hidden_field_tag(name, v.to_s, id: nil)
    end
  end

  safe_join(hidden_fields, "\n")
end

#qa_search_vocab_path(vocab, subauthority = nil) ⇒ String

Generates a search path for the QA (questioning_authority) gem.

Parameters:

  • vocab (String)

    the vocabulary to search

  • subauthority (String, nil) (defaults to: nil)

    the subauthority to search

Returns:

  • (String)

    the generated search path



32
33
34
35
36
# File 'app/helpers/geoblacklight_admin_helper.rb', line 32

def qa_search_vocab_path(vocab, subauthority = nil)
  path = "/authorities/search/#{CGI.escape vocab}"
  path += "/#{CGI.escape subauthority}" if subauthority
  path
end

#thumb_to_render?(document) ⇒ Boolean

Determines if a document’s thumbnail should be rendered.

Parameters:

  • document (Object)

    the document object

Returns:

  • (Boolean)

    true if the thumbnail should be rendered, false otherwise



213
214
215
216
217
218
219
220
221
222
223
# File 'app/helpers/geoblacklight_admin_helper.rb', line 213

def thumb_to_render?(document)
  if document&.thumbnail&.file_url&.present? && document&.thumbnail&.file_derivatives&.present?
    true
  elsif document&.document_assets&.any?
    document.document_assets.any? do |asset|
      asset.file_derivatives&.key?(:thumb_standard_2X)
    end
  else
    false
  end
end

#thumbnail_to_render(document) ⇒ String

Returns the URL of the thumbnail to render for a document.

Parameters:

  • document (Object)

    the document object

Returns:

  • (String)

    the URL of the thumbnail to render



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

def thumbnail_to_render(document)
  if document&.thumbnail&.file_url&.present? && document&.thumbnail&.file_derivatives&.present?
    document.thumbnail.file_url(:thumb_standard_2X)
  elsif document&.document_assets&.any?
    document.document_assets.find do |asset|
      asset.file_derivatives&.key?(:thumb_standard_2X)
    end&.file_url(:thumb_standard_2X)
  end
end