Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def document
if !@slug_checker[:correct]
render inline: "Document not found", status: :not_found if !@slug_checker[:redirect]
redirect_to document_path(id, @document.slug), status: :moved_permanently if @slug_checker[:redirect]
else
# Since I want to list linked documents, I'm querying them right away
@linked_documents = PrismicService.get_documents(@document.linked_documents.map(&:id), api, ref) if [email protected]_documents.blank?
end
end

Expand Down
13 changes: 13 additions & 0 deletions app/models/prismic_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ def get_document(id, api, ref)
documents.length == 0 ? nil : documents.first
end

# Gets a list of documents, from an array of their IDs, in the right order
# Returns an array of Document objects
# This is more performant than querying for each document, as it does only one query
def get_documents(ids, api, ref)
ids_as_string = ids.map{|id| "\"#{id}\""}.join(', ')
documents = api.form("everything")
.query("[[:d = any(document.id, ["+ids_as_string+"])]]")
.submit(ref)
# Reordering the documents in the original order of IDs
documents_by_id = documents.results.index_by{|document| document.id}
ids.map{|id| documents_by_id[id] }
end

# Checks if the slug is the right one for the document.
# You can change this depending on your URL strategy.
def slug_checker(document, slug)
Expand Down
13 changes: 13 additions & 0 deletions app/views/application/document.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@
%>
<%= @document.as_html_safe(link_resolver(maybe_ref)) %>
</article>


<% if @linked_documents %>
<hr>
<aside>
<h2>Linked documents:</h2>
<ul>
<% @linked_documents.each do |linked_document| %>
<li><%= link_to linked_document.first_title || "Untitled document", link_resolver(maybe_ref).link_to(linked_document) %></li>
<% end %>
</ul>
</aside>
<% end %>