Skip to content

Commit

Permalink
WIP- set uploaded_files param to use existing Actor
Browse files Browse the repository at this point in the history
  • Loading branch information
cldambrosio committed Jul 19, 2018
1 parent 9650e54 commit 1cf7f5c
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions lib/importer/factory/object_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ class ObjectFactory
extend ActiveModel::Callbacks
define_model_callbacks :save, :create
class_attribute :klass, :system_identifier_field
attr_reader :attributes, :files_directory, :object, :files
attr_reader :attributes, :files_directory, :object

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

def run
Expand All @@ -26,15 +25,14 @@ def run
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))
end
destroy_existing_file_set if object.file_sets.present?
attach_file_to_work
replace_existing_file_set if object.file_sets.present?
# attach_file_to_work
log_updated(object)
end

Expand Down Expand Up @@ -74,7 +72,7 @@ def create
klass == Collection ? create_collection(@attr) : work_actor.create(environment(@attr))
end
end
attach_file_to_work
# attach_file_to_work
log_created(object)
end

Expand Down Expand Up @@ -113,8 +111,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
end

def file_attributes
files_directory.present? && files.present? ? { files: file_paths } : {}
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
end

def file_paths
Expand All @@ -132,12 +141,12 @@ def import_file(path)
## 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 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
def replace_existing_file_set
f = object.file_sets.first
f.destroy if attributes[:file] != f.title
end
Expand Down

0 comments on commit 1cf7f5c

Please sign in to comment.