Skip to content

Commit

Permalink
Remove :files from attributes
Browse files Browse the repository at this point in the history
Remove method to replace existing file_set

Restore code where change is no longer necessary
  • Loading branch information
cldambrosio committed Jul 20, 2018
1 parent c42894c commit 70d8076
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 50 deletions.
38 changes: 17 additions & 21 deletions lib/importer/factory/object_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,11 @@ def run
object
end

## FOR CONSIDERATION: handle a row (i.e. Work) with more than one file:
## currently the file_set is replaced on update
def update
raise "Object doesn't exist" unless object
@attr = update_attributes
run_callbacks(:save) do
work_actor.update(environment(@attr))
work_actor.update(environment(update_attributes))
end
destroy_existing_file_set if object.file_sets.present?
attach_file_to_work
log_updated(object)
end

Expand Down Expand Up @@ -67,14 +62,13 @@ def search_by_identifier
# https://github.com/projecthydra/active_fedora/issues/874
# 2+ years later, still open!
def create
@attr = create_attributes
attrs = create_attributes
@object = klass.new
run_callbacks :save do
run_callbacks :create do
klass == Collection ? create_collection(@attr) : work_actor.create(environment(@attr))
klass == Collection ? create_collection(attrs) : work_actor.create(environment(attrs))
end
end
attach_file_to_work
log_created(object)
end

Expand Down Expand Up @@ -113,8 +107,20 @@ def transform_attributes
.merge(file_attributes)
end

# Find existing file or upload new file. This assumes a Work will have unique file titles;
# could filter by URIs instead (slower).
# When an uploaded_file already exists we do not want to pass its id in `file_attributes`
# otherwise it gets reuploaded by `work_actor`.
def upload_ids
work_files_titles = object.file_sets.map(&:title) if object.present? && object.file_sets.present?
work_files_titles && work_files_titles.include?(attributes[:file]) ? [] : [import_file(file_paths.first)]
end

def file_attributes
files_directory.present? && files.present? ? { files: file_paths } : {}
hash = {}
hash[:uploaded_files] = upload_ids if files_directory.present? && attributes[:file].present?
hash[:remote_files] = attributes[:remote_files] if attributes[:remote_files].present?
hash
end

def file_paths
Expand All @@ -126,21 +132,11 @@ def import_file(path)
u.user_id = User.find_by_user_key(User.batch_user_key).id if User.find_by_user_key(User.batch_user_key)
u.file = CarrierWave::SanitizedFile.new(path)
u.save
u
u.id
end

## If no file name is provided in the CSV file, `attach_file_to_work` is not performed
## TO DO: handle invalid file in CSV
## currently the importer stops if no file corresponding to a given file_name is found
def attach_file_to_work
imported_file = [import_file(file_paths.first)] if file_paths
AttachFilesToWorkJob.new.perform(object, imported_file, @attr) if imported_file
end

def destroy_existing_file_set
f = object.file_sets.first
f.destroy if attributes[:file] != f.title
end

# Regardless of what the MODS Parser gives us, these are the properties we are prepared to accept.
def permitted_attributes
Expand Down
45 changes: 16 additions & 29 deletions spec/support/shared_examples_for_csv_importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,23 @@
it "uploads the content of the file" do
expect(Hyrax::UploadedFile.last[:file]).to eq("world.png")
end
end

it "attaches the uploaded file to a work" do
expect(Hyrax::UploadedFile.last[:file_set_uri]).to eq(work.find("123").file_sets.first.uri)
describe "when a work with the same id already exists" do
let(:new_attr) do
{
id: "123",
title: ["Squid tofu banjo"],
file: ["nypl-hydra-of-lerna.jpg"]
}
end
end

# describe "when a work with the same id already exists" do
# let(:new_attr) do
# {
# id: "123",
# title: ["Squid tofu banjo"],
# file: ["nypl-hydra-of-lerna.jpg"]
# }
# end
#
# # Ldp::NotFound Error for the update method in 2 tests below - how to stub??
#
# it "updates metadata" do
# new_factory = described_class.new(new_attr, 'spec/fixtures/images')
# new_factory.run
# expect(work.last.title).to eq(["Squid tofu banjo"])
# end
#
# # current behavior - may want to handle this differently?
# it "replaces its file_set" do
# new_factory = described_class.new(new_attr, 'spec/fixtures/images')
# new_factory.run
# expect(Hyrax::UploadedFile.last[:file]).to eq("nypl-hydra-of-lerna.jpg")
# end
# end
it "updates metadata" do
new_factory = described_class.new(new_attr, 'spec/fixtures/images')
new_factory.run
expect(work.last.title).to eq(["Squid tofu banjo"])
end
end
end

context "without a file" do
Expand All @@ -61,10 +48,10 @@
end

it "updates a Work with supplied metadata" do
new_attr = { id: "345", title: ["Retro humblebrag "] }
new_attr = { id: "345", title: ["Retro humblebrag"] }
new_factory = described_class.new(new_attr)
new_factory.run
expect(work.find("345").title).to eq(["Retro humblebrag "])
expect(work.find("345").title).to eq(["Retro humblebrag"])
end
end
end

0 comments on commit 70d8076

Please sign in to comment.