-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding Schema.org tags for better discovery #688
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
792a227
basic structure for schema tags
claudiawulee 68cc675
updated tags
claudiawulee 2395641
Started playing wiht helpers
hectorcorrea 3d02a5e
authors helper
claudiawulee a6c758a
Refactored keywords helper
hectorcorrea 4d49493
authors and licenses helper
claudiawulee f488d2e
included some quotations
claudiawulee 3c396ed
licenses, partial view, tests
claudiawulee 081ba85
tests
claudiawulee 3799742
added hyperlink
claudiawulee b6813f6
rubocop fixes
claudiawulee b887668
rubocop fixes
claudiawulee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
# rubocop:disable Rails/OutputSafety | ||
module SchemaOrgHelper | ||
def keywords_helper(subjects) | ||
keywords_json = subjects.map do |subject| | ||
'"' + html_escape(subject) + '"' | ||
end | ||
|
||
html = "[" + keywords_json.join(",") + "]" | ||
html.html_safe | ||
end | ||
|
||
def authors_helper(authors) | ||
authors_json = authors.each.map do |author| | ||
json_str = "\n\t\t\t{\n\t\t\t" + '"name": ' + '"' + author.value + '"' | ||
if author.affiliation_name.present? | ||
json_str += ",\n\t\t\t" + '"affiliation": ' + '"' + author.affiliation_name + '"' | ||
end | ||
if author.orcid.present? | ||
json_str += ",\n\t\t\t" + '"identifier": ' + '"' + author.orcid + '"' | ||
end | ||
json_str += "\n\t\t\t}" | ||
json_str | ||
end | ||
html = "[" + authors_json.join(",") + "]" | ||
html.html_safe | ||
end | ||
|
||
def license_helper(licenses) | ||
if licenses.count == 0 | ||
"" | ||
else | ||
html = '"license": {' | ||
html += "\n\t\t\t" + '"@type": ' + '"Dataset"' + ",\n" \ | ||
"\t\t\t" + '"text": ' + '"' + licenses[0]['identifier'] + '"' + ",\n" \ | ||
"\t\t\t" + '"url": ' + '"' + licenses[0]['uri'] + '"' | ||
html += "\n\t\t\t}," | ||
html.html_safe | ||
end | ||
end | ||
end | ||
# rubocop:enable Rails/OutputSafety |
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,28 @@ | ||
<% if !@document.nil?%> | ||
<!-- Renders Schema.org tags --> | ||
<script type ="application/ld+json"> | ||
{ "@context": "http://schema.org", | ||
"@type": "Dataset", | ||
"identifier": "<%= @document.doi_url %>", | ||
"name": "<%= @document.title %>", | ||
"author": <%= authors_helper(@document.authors_ordered) %>, | ||
"version": "1", | ||
"description": "<%= @document.description %>", | ||
"keywords": <%= keywords_helper(@document.subject) %>, | ||
"citation": | ||
[ | ||
"<a href="<%= @document.doi_url%>" target="_blank"> "<%= @document.citation.to_s('apa')%>" </a>" | ||
], | ||
"schemaVersion": "https://schema.org/version/28.0", | ||
<%= license_helper(@document.rights_enhanced) %> | ||
"provider": | ||
{ | ||
"@type": "Organization", | ||
"name": "Princeton University" | ||
} | ||
} | ||
</script> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is also an additional ticket to add dates once we index them (see #695) |
||
<% 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,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
RSpec.describe SchemaOrgHelper, type: :helper do | ||
let(:author_with_orcid) do | ||
{ | ||
"value" => "some_name", | ||
"identifier" => { | ||
"scheme" => "ORCID", | ||
"value" => "0000-0000-1111-1111" | ||
}, | ||
"affiliations" => [ | ||
{ "value" => "some_affiliation" } | ||
] | ||
} | ||
end | ||
|
||
let(:author_without_orcid) do | ||
{ | ||
"value" => "some_name" | ||
} | ||
end | ||
|
||
let(:license) do | ||
{ | ||
"identifier" => "some_identifier", | ||
"uri" => "some_uri" | ||
} | ||
end | ||
|
||
describe "#render_sidebar_related_identifiers" do | ||
it "renders keywords" do | ||
expect(helper.keywords_helper(['a', 'b'])).to eq '["a","b"]' | ||
end | ||
|
||
it "does not render keywords" do | ||
expect(helper.keywords_helper([])).to eq '[]' | ||
end | ||
|
||
it "renders authors with orcid and affiliation" do | ||
author = [Author.new(author_with_orcid)] | ||
# Expected output with orcid and affiliation | ||
expected_output = "[\n\t\t\t{\n\t\t\t\"name\": \"some_name\",\n\t\t\t\"affiliation\": \"some_affiliation\",\n\t\t\t\"identifier\": \"0000-0000-1111-1111\"\n\t\t\t}]" | ||
|
||
# Call the helper function with the authors array and verify the result | ||
expect(helper.authors_helper(author)).to eq expected_output | ||
end | ||
|
||
it "renders authors without orcid nor affiliation" do | ||
author = [Author.new(author_without_orcid)] | ||
# Expected output without orcid and affiliation | ||
expected_output = "[\n\t\t\t{\n\t\t\t\"name\": \"some_name\"\n\t\t\t}]" | ||
|
||
# Call the helper function with the authors array and verify the result | ||
expect(helper.authors_helper(author)).to eq expected_output | ||
end | ||
|
||
it "render one license" do | ||
expected_output = "\"license\": {\n\t\t\t\"@type\": \"Dataset\",\n\t\t\t\"text\": \"some_identifier\",\n\t\t\t\"url\": \"some_uri\"\n\t\t\t}," | ||
expect(helper.license_helper([license])).to eq expected_output | ||
end | ||
|
||
it "renders no license" do | ||
expect(helper.license_helper([])).to eq "" | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hard-coded to 1 for, but will get a real value once this has been implemented: #694