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
-
#blacklight_link(document) ⇒ String
Constructs a link to a document’s page in the Blacklight catalog.
-
#link_from_api(link) ⇒ Hash
Processes a link from an API response, determining whether to add or remove a facet.
-
#localize_link(link) ⇒ String
Localizes a given link by parsing its URI and appending it to a base path.
-
#next_link(links) ⇒ String
Generates a link to the next page in a paginated list.
-
#previous_link(links) ⇒ String
Generates a link to the previous page in a paginated list.
-
#publication_state_badge(document) ⇒ String
Generates a badge for the publication state of a document.
-
#sort_link(link) ⇒ String
Creates a sort link with a label and localized URL.
-
#thumb_to_render?(document) ⇒ Boolean
Determines if a document’s thumbnail should be rendered.
Instance Method Details
#blacklight_link(document) ⇒ String
Constructs a link to a document’s page in the Blacklight catalog.
89 90 91 |
# File 'app/helpers/document_helper.rb', line 89 def blacklight_link(document) "#{BLACKLIGHT_URL}/catalog/#{document.friendlier_id}" end |
#link_from_api(link) ⇒ Hash
Processes a link from an API response, determining whether to add or remove a facet.
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 |
#localize_link(link) ⇒ String
Localizes a given link by parsing its URI and appending it to a base path.
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 |
#next_link(links) ⇒ String
Generates a link to the next page in a paginated list.
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 |
#previous_link(links) ⇒ String
Generates a link to the previous page in a paginated list.
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.
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 |
#sort_link(link) ⇒ String
Creates a sort link with a label and localized URL.
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.
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 |