Skip to content

Commit

Permalink
Set visibility based on Date Created
Browse files Browse the repository at this point in the history
Closes #691.
  • Loading branch information
tpendragon committed Dec 28, 2017
1 parent 95761a8 commit caccdb5
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/change_set_persisters/plum_change_set_persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def self.registered_handlers
{
before_save: [
ApplyRemoteMetadata,
ApplyVisibilityByDate,
CreateFile::Factory.new(file_appender: FileAppender),
PropagateVisibilityAndState
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true
class PlumChangeSetPersister
class ApplyVisibilityByDate
attr_reader :change_set_persister, :change_set
def initialize(change_set_persister:, change_set:, post_save_resource: nil)
@change_set = change_set
@change_set_persister = change_set_persister
end

def run
return unless change_set.try(:set_visibility_by_date?)
return unless date_object
change_set.validate(visibility: calculated_visibility, rights_statement: calculated_rights_statement)
change_set.sync
change_set
end

private

def date
Array.wrap(change_set.resource.primary_imported_metadata.created).first
end

def date_object
return unless date
Time.zone.parse(date)
rescue
Rails.logger.warn("Unable to parse created date: #{change_set.resource.primary_imported_metadata.created}")
nil
end

def limit_date
Time.zone.parse("1924-01-01T00:00:00Z")
end

def public_access?
date_object < limit_date
end

def calculated_visibility
if public_access?
Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC
else
Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PRIVATE
end
end

def calculated_rights_statement
if public_access?
"http://rightsstatements.org/vocab/NKC/1.0/"
else
"http://rightsstatements.org/vocab/InC/1.0/"
end
end
end
end
5 changes: 5 additions & 0 deletions app/change_sets/concerns/visibility_property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ module VisibilityProperty
included do
# override this property to define a different default
property :visibility, multiple: false, default: Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC
property :set_visibility_by_date, virtual: true, multiple: false

def set_visibility_by_date?
set_visibility_by_date == "1"
end

def visibility=(visibility)
super.tap do |_result|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
<% current_id = @change_set.id if @change_set %>
<%= f.input :source_metadata_identifier, label: 'Source Metadata ID', input_html: { class: 'mutex detect-duplicates', data: { value: current_id, field: 'source_metadata_identifier_ssim', model: f.object.model.class.to_s } } %>
<%= f.input :refresh_remote_metadata, label: 'Refresh metadata from PULFA/Voyager', as: :boolean %>
<div class="row">
<div class="col-xs-3">
<%= f.input :refresh_remote_metadata, label: 'Refresh metadata from PULFA/Voyager', as: :boolean %>
</div>
<div class="col-xs-3">
<%= f.input :set_visibility_by_date, label: 'Set visibility by Date Created', as: :boolean %>
</div>
<div class="col-xs-6">
</div>
</div>
67 changes: 67 additions & 0 deletions spec/change_set_persisters/plum_change_set_persister_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -623,4 +623,71 @@
expect(solr_record["member_of_ssim"]).to eq ["id-#{parent.id}"]
end
end

context "setting visibility from remote metadata" do
context "when date is before 1924" do
it "sets it to public domain and open" do
stub_bibdata(bib_id: "4609321")
resource = FactoryBot.build(:pending_private_scanned_resource)
change_set = change_set_class.new(resource)
change_set.prepopulate!
change_set.validate(source_metadata_identifier: '4609321', set_visibility_by_date: "1")
change_set.sync

output = change_set_persister.save(change_set: change_set)
reloaded = query_service.find_by(id: output.id)
expect(reloaded.visibility).to eq [Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC]
expect(reloaded.read_groups).to eq ['public']
expect(reloaded.rights_statement).to eq [RDF::URI.new("http://rightsstatements.org/vocab/NKC/1.0/")]
end
end
context "when date is after 1924" do
it "sets it to in copyright and private" do
stub_bibdata(bib_id: "123456")
resource = FactoryBot.build(:pending_private_scanned_resource)
change_set = change_set_class.new(resource)
change_set.prepopulate!
change_set.validate(source_metadata_identifier: '123456', set_visibility_by_date: "1")
change_set.sync

output = change_set_persister.save(change_set: change_set)
reloaded = query_service.find_by(id: output.id)
expect(reloaded.visibility).to eq [Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PRIVATE]
expect(reloaded.read_groups).to eq []
expect(reloaded.rights_statement).to eq [RDF::URI.new("http://rightsstatements.org/vocab/InC/1.0/")]
end
end
context "when given a bad date" do
it "does nothing" do
stub_bibdata(bib_id: "123456789")
resource = FactoryBot.build(:pending_scanned_resource)
change_set = change_set_class.new(resource)
change_set.prepopulate!
change_set.validate(source_metadata_identifier: '123456789', set_visibility_by_date: "1")
change_set.sync

output = change_set_persister.save(change_set: change_set)
reloaded = query_service.find_by(id: output.id)
expect(reloaded.visibility).to eq [Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC]
expect(reloaded.read_groups).to eq ['public']
expect(reloaded.rights_statement).to eq [RDF::URI.new("http://rightsstatements.org/vocab/NKC/1.0/")]
end
end
context "when not told to set visibility by date" do
it "does nothing" do
stub_bibdata(bib_id: "123456")
resource = FactoryBot.build(:pending_scanned_resource)
change_set = change_set_class.new(resource)
change_set.prepopulate!
change_set.validate(source_metadata_identifier: '123456')
change_set.sync

output = change_set_persister.save(change_set: change_set)
reloaded = query_service.find_by(id: output.id)
expect(reloaded.visibility).to eq [Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC]
expect(reloaded.read_groups).to eq ['public']
expect(reloaded.rights_statement).to eq [RDF::URI.new("http://rightsstatements.org/vocab/NKC/1.0/")]
end
end
end
end
4 changes: 4 additions & 0 deletions spec/factories/scanned_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
state "complete"
visibility Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PRIVATE
end
factory :pending_private_scanned_resource do
state "pending"
visibility Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PRIVATE
end
factory :takedown_scanned_resource do
state "takedown"
end
Expand Down
24 changes: 24 additions & 0 deletions spec/fixtures/files/bibdata/123456789.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"@context":"http://bibdata.princeton.edu/context.json",
"@id":"http://bibdata.princeton.edu/bibliographic/123456",
"title":{
"@value":"Earth rites : fertility rites in pre-industrial Britain",
"@language":"fr"
},
"language":"eng",
"creator":"Bord, Janet, 1945-",
"call_number":"BL980.G7 B66 1982",
"extent":"xiv, 273 p. : ill. ; 24 cm.",
"format":"Book",
"description":"Includes index.",
"publisher":"London ; New York : Granada, 1982.",
"subject":"Fertility cults—Great Britain—History",
"title_sort":"Earth rites : fertility rites in pre-industrial Britain / Janet and Colin Bord.",
"spatial":"Great Britain",
"contributor":[
"Bord, Colin"
],
"author":"Bord, Janet, 1945-",
"created":"1982",
"date":"1982"
}

0 comments on commit caccdb5

Please sign in to comment.