Skip to content

Commit

Permalink
add a rights_statement renderer to render the statement text rather t…
Browse files Browse the repository at this point in the history
…han the URL
  • Loading branch information
Julie Allinson committed Oct 11, 2017
1 parent ec97667 commit cf947a5
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Hyrax
module Renderers
# This is used by PresentsAttributes to show licenses
# e.g.: presenter.attribute_to_html(:rights_statement, render_as: :rights_statement)
class RightsStatementAttributeRenderer < AttributeRenderer
private

##
# Special treatment for license/rights. A URL from the Hyrax gem's config/hyrax.rb is stored in the descMetadata of the
# curation_concern. If that URL is valid in form, then it is used as a link. If it is not valid, it is used as plain text.
def attribute_value_to_html(value)
begin
parsed_uri = URI.parse(value)
rescue
nil
end
if parsed_uri.nil?
ERB::Util.h(value)
else
%(<a href=#{ERB::Util.h(value)} target="_blank">#{Hyrax.config.rights_statement_service_class.new.label(value)}</a>)
end
end
end
end
end
2 changes: 1 addition & 1 deletion app/views/hyrax/base/_attribute_rows.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
<%= presenter.attribute_to_html(:related_url, render_as: :external_link) %>
<%= presenter.attribute_to_html(:resource_type, render_as: :faceted) %>
<%= presenter.attribute_to_html(:source) %>
<%= presenter.attribute_to_html(:rights_statement) %>
<%= presenter.attribute_to_html(:rights_statement, render_as: :rights_statement) %>
11 changes: 11 additions & 0 deletions lib/hyrax/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ def license_service_class
@license_service_class ||= Hyrax::LicenseService
end

# A configuration point for changing the behavior of the rights statement service.
#
# @!attribute [w] license_service_class
# A configuration point for changing the behavior of the license service.
#
# @see Hyrax::RightsStatementService for implementation details
attr_writer :rights_statement_service_class
def rights_statement_service_class
@rights_statement_service_class ||= Hyrax::RightsStatementService
end

attr_writer :banner_image
def banner_image
# This image can be used for free and without attribution. See here for source and license: https://github.com/samvera/hyrax/issues/1551#issuecomment-326624909
Expand Down
4 changes: 4 additions & 0 deletions spec/lib/hyrax/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
it { is_expected.to respond_to(:google_analytics_id?) }
it { is_expected.to respond_to(:google_analytics_id) }
it { is_expected.to respond_to(:libreoffice_path) }
it { is_expected.to respond_to(:license_service_class) }
it { is_expected.to respond_to(:license_service_class=) }
it { is_expected.to respond_to(:max_days_between_fixity_checks=) }
it { is_expected.to respond_to(:max_days_between_fixity_checks) }
it { is_expected.to respond_to(:max_notifications_for_dashboard) }
Expand All @@ -52,6 +54,8 @@
it { is_expected.to respond_to(:persistent_hostpath) }
it { is_expected.to respond_to(:realtime_notifications?) }
it { is_expected.to respond_to(:realtime_notifications=) }
it { is_expected.to respond_to(:rights_statement_service_class) }
it { is_expected.to respond_to(:rights_statement_service_class=) }
it { is_expected.to respond_to(:redis_namespace) }
it { is_expected.to respond_to(:subject_prefix) }
it { is_expected.to respond_to(:translate_id_to_uri) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
RSpec.describe Hyrax::Renderers::RightsStatementAttributeRenderer do
let(:field) { :rights_statement }
let(:renderer) { described_class.new(field, ['http://rightsstatements.org/vocab/InC/1.0/']) }

describe "#attribute_to_html" do
subject { Nokogiri::HTML(renderer.render) }

let(:expected) { Nokogiri::HTML(tr_content) }

let(:tr_content) do
"<tr><th>Rights statement</th>\n" \
"<td><ul class='tabular'>" \
"<li class=\"attribute rights_statement\"><a href=\"http://rightsstatements.org/vocab/InC/1.0/\" target=\"_blank\">In Copyright</a></li>" \
"</ul></td></tr>"
end

it { expect(renderer).not_to be_microdata(field) }
it { expect(subject).to be_equivalent_to(expected) }
end
end
2 changes: 1 addition & 1 deletion spec/views/hyrax/base/_attribute_rows.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
end

it 'shows rights statement with link to statement URL' do
expect(page).to have_link(rights_statement_uri)
expect(page).to have_link("In Copyright", href: rights_statement_uri)
end
end

0 comments on commit cf947a5

Please sign in to comment.