-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically add self deposit items to associated collections
* Create the collections that are required for the contribute deposits * If a collection gets deleted, eradicate it so the id can be used again and re-create the collection * Make a rake task so this can be run easily from the command line * Add it to capistrano to ensure they exist whenever we deploy * Query for a work type and get the collection it should go into * Queries at deposit time recover gracefully and re-create the missing Collection object if necessary * Add contributions to the appropriate collection according to their class
- Loading branch information
Showing
11 changed files
with
194 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
module Tufts | ||
# Create and maintain the Collection objects required by the Contribute controller | ||
class ContributeCollections | ||
attr_reader :seed_data | ||
|
||
def initialize | ||
@seed_data = make_seed_data_hash | ||
end | ||
|
||
def make_seed_data_hash | ||
seed_hash = {} | ||
SEED_DATA.each do |c| | ||
id = convert_id(c[:identifier]) | ||
seed_hash[id] = c | ||
end | ||
seed_hash | ||
end | ||
|
||
def self.create | ||
Tufts::ContributeCollections.new.create | ||
end | ||
|
||
# Convert a legacy Tufts identifier into a predictable and valid Fedora identifier | ||
# @param [String] id | ||
# @return [String] | ||
def convert_id(id) | ||
newid = id.downcase | ||
newid.slice!('tufts:') | ||
newid.tr!('.', '_') | ||
end | ||
|
||
def create | ||
@seed_data.keys.each do |collection_id| | ||
find_or_create_collection(collection_id) | ||
end | ||
end | ||
|
||
# Given a collection id, find or create the collection. | ||
# If the collection has been deleted, eradicate it so the id can be | ||
# re-used, and re-create the collection object. | ||
# @param [String] collection_id | ||
# @return [Collection] | ||
def find_or_create_collection(collection_id) | ||
Collection.find(collection_id) | ||
rescue ActiveFedora::ObjectNotFoundError | ||
create_collection(collection_id) | ||
rescue Ldp::Gone | ||
Collection.eradicate(collection_id) | ||
create_collection(collection_id) | ||
end | ||
|
||
# @param [String] collection_id | ||
# @return [Collection] | ||
def create_collection(collection_id) | ||
collection = Collection.new(id: collection_id) | ||
collection_hash = @seed_data[collection_id] | ||
collection.title = Array(collection_hash[:title]) | ||
collection.identifier = Array(collection_hash[:identifier]) | ||
collection.visibility = Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC | ||
collection.save | ||
collection | ||
end | ||
|
||
# Convenience method for use by the contribute controller | ||
# @param [Class] work_type | ||
# @return [Collection] | ||
# @example | ||
# Tufts::ContributeCollections.collection_for_work_type(FacultyScholarship) | ||
def self.collection_for_work_type(work_type) | ||
Tufts::ContributeCollections.new.collection_for_work_type(work_type) | ||
end | ||
|
||
# For a given work type, determine which Collection contributions should go into. | ||
# If that collection object doesn't exist for some reason, create it. | ||
# @param [Class] work_type | ||
# @return [Collection] | ||
def collection_for_work_type(work_type) | ||
collection_id = @seed_data.select { |_key, hash| hash[:work_types].include? work_type }.keys.first | ||
Collection.find(collection_id) | ||
rescue ActiveFedora::ObjectNotFoundError | ||
create_collection(collection_id) | ||
rescue Ldp::Gone | ||
Collection.eradicate(collection_id) | ||
create_collection(collection_id) | ||
end | ||
|
||
SEED_DATA = [ | ||
{ | ||
title: "Tufts Published Scholarship, 1987-2014", | ||
identifier: "tufts:UA069.001.DO.PB", | ||
work_types: [GenericDeposit, GenericTischDeposit, GisPoster, UndergradSummerScholar, FacultyScholarship] | ||
}, | ||
{ | ||
title: "Fletcher School Records, 1923 -- 2016", | ||
identifier: "tufts:UA069.001.DO.UA015", | ||
work_types: [CapstoneProject] | ||
}, | ||
{ | ||
title: "Cummings School of Veterinary Medicine records, 1969-2012", | ||
identifier: "tufts:UA069.001.DO.UA041", | ||
work_types: [CummingsThesis] | ||
}, | ||
{ | ||
title: "Undergraduate honors theses, 1929-2015", | ||
identifier: "tufts:UA069.001.DO.UA005", | ||
work_types: [HonorsThesis] | ||
}, | ||
{ | ||
title: "Public Health and Professional Degree Programs Records, 1990 -- 2011", | ||
identifier: "tufts:UA069.001.DO.UA187", | ||
work_types: [PublicHealth] | ||
}, | ||
{ | ||
title: "Department of Education records, 2007-02-01-2014", | ||
identifier: "tufts:UA069.001.DO.UA071", | ||
work_types: [QualifyingPaper] | ||
} | ||
].freeze | ||
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,8 @@ | ||
require 'rake' | ||
|
||
namespace :tufts do | ||
desc "Create contribute collections" | ||
task create_contribute_collections: :environment do | ||
Tufts::ContributeCollections.create | ||
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
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 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Tufts::ContributeCollections, :clean do | ||
let(:cc) { described_class.new } | ||
|
||
it "has a hash of all the collections to be made, with their ids and titles" do | ||
expect(cc.seed_data).to be_instance_of(Hash) | ||
end | ||
it "converts a legacy identifier into a predictable valid fedora id" do | ||
expect(cc.convert_id('tufts:UA069.001.DO.PB')).to eq("ua069_001_do_pb") | ||
end | ||
|
||
context "creating all the collections" do | ||
before do | ||
described_class.create | ||
end | ||
it "creates a collection object for each item in the seed array" do | ||
expect(Collection.count).to eq(6) | ||
end | ||
it "populates title and legacy identifier" do | ||
c = Collection.find("ua069_001_do_pb") | ||
expect(c.title.first).to eq("Tufts Published Scholarship, 1987-2014") | ||
expect(c.identifier.first).to eq("tufts:UA069.001.DO.PB") | ||
end | ||
end | ||
|
||
context "putting contributed works into collections" do | ||
before do | ||
described_class.create | ||
end | ||
it "finds the right collection for a given work type" do | ||
faculty_scholarship_collection = cc.collection_for_work_type(FacultyScholarship) | ||
expect(faculty_scholarship_collection).to be_instance_of(Collection) | ||
expect(faculty_scholarship_collection.id).to eq('ua069_001_do_pb') | ||
end | ||
it "recovers if one of the expected collections has been deleted" do | ||
Collection.find('ua069_001_do_pb').delete | ||
faculty_scholarship_collection = cc.collection_for_work_type(FacultyScholarship) | ||
expect(faculty_scholarship_collection).to be_instance_of(Collection) | ||
expect(faculty_scholarship_collection.id).to eq('ua069_001_do_pb') | ||
end | ||
end | ||
end |