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 1cf7f5c commit 904a369
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 56 deletions.
45 changes: 16 additions & 29 deletions lib/importer/factory/object_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ class ObjectFactory
extend ActiveModel::Callbacks
define_model_callbacks :save, :create
class_attribute :klass, :system_identifier_field
attr_reader :attributes, :files_directory, :object
attr_reader :attributes, :files_directory, :object, :files

def initialize(attributes, files_dir = nil)
def initialize(attributes, files_dir = nil, files = [])
@attributes = attributes
@files_directory = files_dir
@files = files
end

def run
Expand All @@ -24,15 +25,11 @@ def run
object
end

## FOR CONSIDERATION: handle a row (i.e. Work) with more than one file:
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
replace_existing_file_set if object.file_sets.present?
# attach_file_to_work
log_updated(object)
end

Expand Down Expand Up @@ -65,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 @@ -111,18 +107,19 @@ def transform_attributes
.merge(file_attributes)
end

def upload_id
return [] if object.blank?
Hyrax::UploadedFile.where(file_set_uri: "#{object.file_sets.first.uri}").ids
# 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
hash = {}
if files_directory.present? && attributes[:file].present?
imported_file = [import_file(file_paths.first)]
hash[:files] = imported_file
hash[:uploaded_files] = upload_id
end
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

Expand All @@ -135,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 replace_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
41 changes: 14 additions & 27 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 Down

0 comments on commit 904a369

Please sign in to comment.