-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to OCLC Discovery API Citation service
- Loading branch information
Showing
36 changed files
with
669 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<% citations.each do |style, citation| %> | ||
<div class="mb-4"> | ||
<% unless style == 'NULL' %> | ||
<h4><%= t("searchworks.citations.styles.#{style}") %></h4> | ||
<% end %> | ||
<% Array(citation).each do |cite| %> | ||
<div class="mb-2"> | ||
<%= cite %> | ||
</div> | ||
<% end %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Citations | ||
class CitationComponent < ViewComponent::Base | ||
attr_reader :citations | ||
|
||
def initialize(citations:) | ||
@citations = citations | ||
super() | ||
end | ||
|
||
def render? | ||
citations.present? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render Citations::CitationComponent.new(citations: grouped_citations) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module Citations | ||
class GroupedCitationComponent < ViewComponent::Base | ||
attr_reader :citations | ||
|
||
PREFERRED_CITATION_KEY = 'preferred' | ||
|
||
# @param [Array<Hash>] citations in the form of [{ citation_style => citation_text }] | ||
def initialize(citations:) | ||
@citations = citations | ||
super() | ||
end | ||
|
||
# @return [Hash] A hash of citations grouped by style in the form of { citation_style => [citation_text] } | ||
def grouped_citations | ||
citation_styles.index_with { |style| citations.pluck(style).compact } | ||
end | ||
|
||
def render? | ||
citations.present? | ||
end | ||
|
||
private | ||
|
||
def citation_styles | ||
keys = citations.map(&:keys).flatten.uniq | ||
# It doesn't make sense to display the NULL citation | ||
# when grouping citations by style so remove it from the list | ||
keys.delete('NULL') | ||
|
||
# If the preferred citation is present, move it to the front of the list | ||
# so that it always displays first | ||
return keys unless keys.include?(PREFERRED_CITATION_KEY) | ||
|
||
keys.delete(PREFERRED_CITATION_KEY) | ||
keys.unshift(PREFERRED_CITATION_KEY) | ||
end | ||
end | ||
end |
13 changes: 13 additions & 0 deletions
13
app/components/citations/multiple_citations_component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<div class="tab-content"> | ||
<div role="tabpanel" class="tab-pane active" id="all" aria-labelledby="by-title-button"> | ||
<% @documents.each do |document| %> | ||
<h3 class="mt-4 mb-3"><%= helpers.document_presenter(document).heading %></h3> | ||
<%= render Citations::CitationComponent.new(citations: citations(document)) %> | ||
<% end %> | ||
</div> | ||
<div role="tabpanel" class="tab-pane" id="biblio" aria-labelledby="by-format-button"> | ||
<div class="my-3"> | ||
<%= render Citations::GroupedCitationComponent.new(citations: @documents.map { |doc| citations(doc) }) %> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module Citations | ||
class MultipleCitationsComponent < ViewComponent::Base | ||
attr_reader :documents, :oclc_citations | ||
|
||
# @param [Array<SolrDocument>] documents to generate citations for | ||
# @param [Hash] oclc_citations in the form of { oclc_number => { citation_style => citation_text } } | ||
# for lookup of pre-fetched OCLC citations | ||
def initialize(documents:, oclc_citations:) | ||
@documents = documents | ||
@oclc_citations = oclc_citations | ||
super() | ||
end | ||
|
||
# @return [Hash] A hash of citations for each document in the form of { citation_style => [citation_text] } | ||
def citations(document) | ||
citation_hash = {} | ||
|
||
citation_hash.merge!(document.mods_citations) | ||
citation_hash.merge!(document.eds_citations) | ||
citation_hash.merge!(oclc_citation(document)) | ||
|
||
citation_hash.presence || Citation::NULL_CITATION | ||
end | ||
|
||
private | ||
|
||
def oclc_citation(document) | ||
return {} if document.oclc_number.blank? | ||
|
||
oclc_citations.fetch(document.oclc_number, {}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
### | ||
# Returns an EDS citation formatted for use by SearchWorks | ||
module Citations | ||
class EdsCitation | ||
CITATION_STYLES = %w[apa chicago harvard mla turabian].freeze | ||
|
||
attr_reader :eds_citation_styles | ||
|
||
# @param eds_citation_styles [Array<Hash>] An array of EDS citations | ||
def initialize(eds_citation_styles:) | ||
@eds_citation_styles = eds_citation_styles | ||
end | ||
|
||
# @return [Hash] A hash with citation styles as keys and citation text as values. | ||
def all_citations | ||
matching_styles.index_with do |id| | ||
eds_citation_styles.select { |style| style.fetch('id', nil) == id }.pick('data')&.html_safe # rubocop:disable Rails/OutputSafety | ||
end.compact | ||
end | ||
|
||
private | ||
|
||
def matching_styles | ||
eds_citation_styles.pluck('id').select { |id| CITATION_STYLES.include?(id) } | ||
end | ||
end | ||
end |
Oops, something went wrong.