From 904a36916f74ed2ae71c9f6e6687fdeb1f971312 Mon Sep 17 00:00:00 2001 From: Claire Date: Fri, 20 Jul 2018 10:15:24 +0100 Subject: [PATCH] Remove :files from attributes Remove method to replace existing file_set Restore code where change is no longer necessary --- lib/importer/factory/object_factory.rb | 45 +++++++------------ .../shared_examples_for_csv_importer_spec.rb | 41 ++++++----------- 2 files changed, 30 insertions(+), 56 deletions(-) diff --git a/lib/importer/factory/object_factory.rb b/lib/importer/factory/object_factory.rb index cb9a45ba3..ae7d78b41 100644 --- a/lib/importer/factory/object_factory.rb +++ b/lib/importer/factory/object_factory.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/support/shared_examples_for_csv_importer_spec.rb b/spec/support/shared_examples_for_csv_importer_spec.rb index 82b5eac2b..b4e15a7b4 100644 --- a/spec/support/shared_examples_for_csv_importer_spec.rb +++ b/spec/support/shared_examples_for_csv_importer_spec.rb @@ -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