Module: DocumentHelper

Defined in:
app/helpers/document_helper.rb

Overview

DocumentHelper

This module provides helper methods for handling document-related functionalities such as generating publication state badges, creating localized links, and handling pagination links.

Instance Method Summary collapse

Instance Method Details

Constructs a link to a document’s page in the Blacklight catalog.

Parameters:

  • document (Object)

    the document object

Returns:

  • (String)

    the URL to the document’s Blacklight catalog page



89
90
91
# File 'app/helpers/document_helper.rb', line 89

def blacklight_link(document)
  "#{BLACKLIGHT_URL}/catalog/#{document.friendlier_id}"
end

Processes a link from an API response, determining whether to add or remove a facet.

Parameters:

  • link (Hash)

    a hash containing links for adding or removing facets

Returns:

  • (Hash)

    a hash with action and link keys



51
52
53
54
55
56
57
58
59
# File 'app/helpers/document_helper.rb', line 51

def link_from_api(link)
  # Append facet - Full URI returned
  uri = URI.parse(link["links"]["self"])
  {action: "add", link: "/admin/documents?#{uri.query}"}
rescue
  # Remove facet - Only path and query returned
  uri = URI.parse(link["links"]["remove"])
  {action: "remove", link: "/admin/documents?#{uri.query}"}
end

Localizes a given link by parsing its URI and appending it to a base path.

Parameters:

  • link (String)

    the link to be localized

Returns:

  • (String)

    the localized link



34
35
36
37
# File 'app/helpers/document_helper.rb', line 34

def localize_link(link)
  uri = URI.parse(link)
  "/admin/documents?#{uri.query}"
end

Generates a link to the next page in a paginated list.

Parameters:

  • links (Hash)

    a hash containing pagination links

Returns:

  • (String)

    HTML link element for the next page



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

def next_link(links)
  if links["next"]
    link_to "Next", localize_link(links["next"]), {class: "btn btn-outline-primary btn-sm"}
  else
    link_to "Next", "javascript:;", {class: "btn btn-outline-primary btn-sm disabled", "aria-disabled": true}
  end
end

Generates a link to the previous page in a paginated list.

Parameters:

  • links (Hash)

    a hash containing pagination links

Returns:

  • (String)

    HTML link element for the previous page



65
66
67
68
69
70
71
# File 'app/helpers/document_helper.rb', line 65

def previous_link(links)
  if links["prev"]
    link_to "Previous", localize_link(links["prev"]), {class: "btn btn-outline-primary btn-sm"}
  else
    link_to "Previous", "javascript:;", {class: "btn btn-outline-primary btn-sm disabled", "aria-disabled": true}
  end
end

#publication_state_badge(document) ⇒ String

Generates a badge for the publication state of a document.

Parameters:

  • document (Object)

    the document object containing the publication state

Returns:

  • (String)

    HTML link with a span element representing the publication state



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/document_helper.rb', line 13

def publication_state_badge(document)
  case document.publication_state
  when "draft"
    link_to("#document_publication_state") do
      tag.span("Draft", class: "badge badge-secondary")
    end
  when "published"
    link_to("#document_publication_state") do
      tag.span("Published", class: "badge badge-success")
    end
  when "unpublished"
    link_to("#document_publication_state") do
      tag.span("Unpublished", class: "badge badge-danger")
    end
  end
end

Creates a sort link with a label and localized URL.

Parameters:

  • link (Hash)

    a hash containing link attributes and self link

Returns:

  • (String)

    HTML link element for sorting



43
44
45
# File 'app/helpers/document_helper.rb', line 43

def sort_link(link)
  link_to link["attributes"]["label"], localize_link(link["links"]["self"]), {class: "dropdown-item"}
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



97
98
99
# File 'app/helpers/document_helper.rb', line 97

def thumb_to_render?(document)
  document&.thumbnail&.file_url&.present? && document&.thumbnail&.file_derivatives&.present?
end