Skip to content

Commit

Permalink
Merge pull request #207 from scientist-softserv/i206-questioning-auth…
Browse files Browse the repository at this point in the history
…orities

I206 questioning authorities
  • Loading branch information
kirkkwang authored Mar 30, 2023
2 parents 683c06b + 9674b9d commit 2c20ca1
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 21 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build-lint-test-action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ jobs:
needs: build
uses: scientist-softserv/actions/.github/workflows/[email protected]
with:
setup_db_cmd: rake engine_cart:generate
setup_db_cmd: bundle exec rake prepare_and_run_tests
lint:
needs: build
uses: scientist-softserv/actions/.github/workflows/[email protected]

6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ end
Dir.glob('tasks/*.rake').each { |r| import r }
Dir.glob('lib/tasks/*.rake').each { |r| import r }

# Adding the copy_authorities here so it runs the same in CI
desc "Generate the engine_cart, copy authorities, and run tests"
task prepare_and_run_tests: ['engine_cart:generate', 'engine_cart:copy_authorities'] do
puts "Running CI tests"
end

task default: :ci
42 changes: 28 additions & 14 deletions lib/iiif_print/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,33 @@ def scrub(value)

def cast_to_value(field_name:, options:)
if options&.[](:render_as) == :faceted
values_for(field_name: field_name).map do |value|
search_field = field_name.to_s + "_sim"
path = Rails.application.routes.url_helpers.search_catalog_path(
"f[#{search_field}][]": value, locale: I18n.locale
)
path += '&include_child_works=true' if work["is_child_bsi"] == true
"<a href='#{File.join(@base_url, path)}'>#{value}</a>"
end
faceted_values_for(field_name: field_name)
elsif options&.[](:render_as) == :rights_statement || options&.[](:render_as) == :license
authority_values_for(field_name: field_name)
else
make_link(values_for(field_name: field_name))
end
end

def faceted_values_for(field_name:)
values_for(field_name: field_name).map do |value|
search_field = field_name.to_s + "_sim"
path = Rails.application.routes.url_helpers.search_catalog_path(
"f[#{search_field}][]": value, locale: I18n.locale
)
path += '&include_child_works=true' if work["is_child_bsi"] == true
"<a href='#{File.join(@base_url, path)}'>#{value}</a>"
end
end

def authority_values_for(field_name:)
authority = Qa::Authorities::Local.subauthority_for(field_name.to_s.pluralize)
values_for(field_name: field_name).map do |value|
id, term = authority.find(value).values_at('id', 'term')
"<a href='#{id}'>#{term}</a>"
end
end

def values_for(field_name:)
field_name = field_name.try(:name) || field_name
# TODO: we are assuming tesim or dtsi (for dates), might want to account for other suffixes in the future
Expand All @@ -95,22 +109,22 @@ def viewable_collections
Hyrax::CollectionMemberService.run(SolrDocument.find(work.id), current_ability)
end

# @note This method turns link looking strings into links
# @note This method turns link looking strings into links and assumes https if not protocol was given
def make_link(texts)
texts.map do |t|
t.to_s.gsub(MAKE_LINK_REGEX) do |url|
"<a href='#{url}' target='_blank'>#{url}</a>"
protocol = url.start_with?('www.') ? 'https://' : ''
"<a href='#{protocol}#{url}' target='_blank'>#{url}</a>"
end
end
end

MAKE_LINK_REGEX = %r{
\b
(
(?: [a-z][\w-]+:
(?: /{1,3} | [a-z0-9%] ) |
www\d{0,3}[.] |
[a-z0-9.\-]+[.][a-z]{2,4}/
(?:
(?:https?://) |
(?:www\.)
)
(?:
[^\s()<>]+ | \(([^\s()<>]+|(\([^\s()<>]+\)))*\)
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/authorities/licenses.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
terms:
- id: https://creativecommons.org/licenses/by-sa/4.0/
term: Creative Commons BY-SA Attribution-ShareAlike 4.0 International
active: true
4 changes: 4 additions & 0 deletions spec/fixtures/authorities/rights_statements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
terms:
- id: http://rightsstatements.org/vocab/InC-OW-EU/1.0/
term: "In Copyright - EU Orphan Work"
active: true
60 changes: 59 additions & 1 deletion spec/iiif_print/metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,42 @@
let(:attributes) { { "creator_tesim" => ["McAuthor, Arthur"] } }

it "adds a link to the faceted search" do
expect(manifest_metadata). to eq [
expect(manifest_metadata).to eq [
{ "label" => "Creator",
"value" =>
["<a href='#{base_url}/catalog?f%5Bcreator_sim%5D%5B%5D=McAuthor%2C+Arthur&locale=en'>McAuthor, Arthur</a>"] }
]
end
end

context "with an authority option" do
context "rights statement" do
let(:metadata_fields) { { rights_statement: { render_as: :rights_statement } } }
let(:attributes) { { "rights_statement_tesim" => ["http://rightsstatements.org/vocab/InC-OW-EU/1.0/"] } }

it "renders a link and displays a term" do
expect(manifest_metadata).to eq [
{ "label" => "Rights statement",
"value" => ["<a href='http://rightsstatements.org/vocab/InC-OW-EU/1.0/'>In Copyright - EU Orphan Work</a>"] }
]
end
end

context "license" do
let(:metadata_fields) { { license: { render_as: :license } } }
let(:attributes) { { "license_tesim" => ["https://creativecommons.org/licenses/by-sa/4.0/"] } }

it "renders a link and displays a term" do
expect(manifest_metadata).to eq [
{ "label" => "License",
"value" => [
"<a href='https://creativecommons.org/licenses/by-sa/4.0/'>Creative Commons BY-SA Attribution-ShareAlike 4.0 International</a>"
] }
]
end
end
end

context "when the work is apart of a collection" do
let(:metadata_fields) { { collection: {} } }
let(:collection_attributes) { { "id" => "321cba", "title_tesim" => ["My Cool Collection"] } }
Expand Down Expand Up @@ -152,6 +180,36 @@
end
end

context "with an authority option" do
context "rights statement" do
let(:metadata_fields) { { rights_statement: { render_as: :rights_statement } } }
let(:attributes) { { "rights_statement_tesim" => ["http://rightsstatements.org/vocab/InC-OW-EU/1.0/"] } }

it "renders a link and displays a term" do
expect(manifest_metadata).to eq [
{ "label" => { "en" => ["Rights statement"] },
"value" => { "none" => [
"<a href='http://rightsstatements.org/vocab/InC-OW-EU/1.0/'>In Copyright - EU Orphan Work</a>"
] } }
]
end
end

context "license" do
let(:metadata_fields) { { license: { render_as: :license } } }
let(:attributes) { { "license_tesim" => ["https://creativecommons.org/licenses/by-sa/4.0/"] } }

it "renders a link and displays a term" do
expect(manifest_metadata).to eq [
{ "label" => { "en" => ["License"] },
"value" => { "none" => [
"<a href='https://creativecommons.org/licenses/by-sa/4.0/'>Creative Commons BY-SA Attribution-ShareAlike 4.0 International</a>"
] } }
]
end
end
end

context "when the work is apart of a collection" do
let(:metadata_fields) { { collection: {} } }
let(:collection_attributes) { { "id" => "321cba", "title_tesim" => ["My Cool Collection"] } }
Expand Down
11 changes: 11 additions & 0 deletions tasks/copy_authorities_to_test_app.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace :engine_cart do
desc "Copy custom rights_statement and license questioning authority YAML files into the test app"
task copy_authorities: :generate do
test_app_path = ENV['ENGINE_CART_DESTINATION'] || File.expand_path(".internal_test_app", File.dirname(__FILE__))
src_dir = File.expand_path("../spec/fixtures/authorities", __dir__)
dest_dir = File.join(test_app_path, "config/authorities")

FileUtils.mkdir_p(dest_dir)
FileUtils.cp_r("#{src_dir}/.", dest_dir)
end
end
8 changes: 4 additions & 4 deletions tasks/iiif_print_dev.rake
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ task :spec_with_app_load do
end

if ENV.fetch('IN_DOCKER', false)
desc 'Generate the engine_cart and spin up test servers and run specs'
task ci: %w[rubocop engine_cart:generate] do
desc 'Generate the engine_cart, copy authorities and spin up test servers and run specs'
task ci: %w[rubocop engine_cart:generate engine_cart:copy_authorities] do
puts 'running continuous integration'
Rake::Task['spec'].invoke
end
else
desc 'Generate the engine_cart and spin up test servers and run specs'
task ci: %w[rubocop engine_cart:generate] do
desc 'Generate the engine_cart, copy authorities and spin up test servers and run specs'
task ci: %w[rubocop engine_cart:generate engine_cart:copy_authorities] do
puts 'running continuous integration'
Rake::Task['spec_with_app_load'].invoke
end
Expand Down

0 comments on commit 2c20ca1

Please sign in to comment.