Class: Bulkrax::ObjectFactory
- Inherits:
-
Object
- Object
- Bulkrax::ObjectFactory
- Extended by:
- ActiveModel::Callbacks
- Includes:
- DynamicRecordLookup, FileFactory
- Defined in:
- app/factories/bulkrax/object_factory.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#importer_run_id ⇒ Object
readonly
Returns the value of attribute importer_run_id.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#related_parents_parsed_mapping ⇒ Object
readonly
Returns the value of attribute related_parents_parsed_mapping.
-
#replace_files ⇒ Object
readonly
Returns the value of attribute replace_files.
-
#source_identifier_value ⇒ Object
readonly
Returns the value of attribute source_identifier_value.
-
#update_files ⇒ Object
readonly
Returns the value of attribute update_files.
-
#work_identifier ⇒ Object
readonly
Returns the value of attribute work_identifier.
Instance Method Summary collapse
-
#create ⇒ Object
An ActiveFedora bug when there are many habtm <-> has_many associations means they won’t all get saved.
- #find ⇒ Object
- #find_by_id ⇒ Object
- #find_or_create ⇒ Object
-
#initialize(attributes:, source_identifier_value:, work_identifier:, related_parents_parsed_mapping: nil, replace_files: false, user: nil, klass: nil, importer_run_id: nil, update_files: false) ⇒ ObjectFactory
constructor
rubocop:disable Metrics/ParameterLists.
- #log_created(obj) ⇒ Object
- #log_deleted_fs(obj) ⇒ Object
- #log_updated(obj) ⇒ Object
- #run {|object| ... } ⇒ Object
- #run! ⇒ Object
- #search_by_identifier ⇒ Object
- #update ⇒ Object
-
#with_files ⇒ Object
update files is set, replace files is set or this is a create.
Methods included from DynamicRecordLookup
#curation_concern?, #find_record
Methods included from FileFactory
#destroy_existing_files, #file_attributes, #file_paths, #import_file, #import_files, #import_files_filenames, #local_file_sets, #new_remote_files, #ordered_file_sets, #parsed_remote_files, #set_removed_filesets, #update_filesets, #upload_ids, #work_files_filenames
Constructor Details
#initialize(attributes:, source_identifier_value:, work_identifier:, related_parents_parsed_mapping: nil, replace_files: false, user: nil, klass: nil, importer_run_id: nil, update_files: false) ⇒ ObjectFactory
rubocop:disable Metrics/ParameterLists
13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/factories/bulkrax/object_factory.rb', line 13 def initialize(attributes:, source_identifier_value:, work_identifier:, related_parents_parsed_mapping: nil, replace_files: false, user: nil, klass: nil, importer_run_id: nil, update_files: false) @attributes = ActiveSupport::HashWithIndifferentAccess.new(attributes) @replace_files = replace_files @update_files = update_files @user = user || User.batch_user @work_identifier = work_identifier = @source_identifier_value = source_identifier_value @klass = klass || Bulkrax.default_work_type.constantize @importer_run_id = importer_run_id end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
10 11 12 |
# File 'app/factories/bulkrax/object_factory.rb', line 10 def attributes @attributes end |
#importer_run_id ⇒ Object (readonly)
Returns the value of attribute importer_run_id.
10 11 12 |
# File 'app/factories/bulkrax/object_factory.rb', line 10 def importer_run_id @importer_run_id end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
10 11 12 |
# File 'app/factories/bulkrax/object_factory.rb', line 10 def klass @klass end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
10 11 12 |
# File 'app/factories/bulkrax/object_factory.rb', line 10 def object @object end |
#related_parents_parsed_mapping ⇒ Object (readonly)
Returns the value of attribute related_parents_parsed_mapping.
10 11 12 |
# File 'app/factories/bulkrax/object_factory.rb', line 10 def end |
#replace_files ⇒ Object (readonly)
Returns the value of attribute replace_files.
10 11 12 |
# File 'app/factories/bulkrax/object_factory.rb', line 10 def replace_files @replace_files end |
#source_identifier_value ⇒ Object (readonly)
Returns the value of attribute source_identifier_value.
10 11 12 |
# File 'app/factories/bulkrax/object_factory.rb', line 10 def source_identifier_value @source_identifier_value end |
#update_files ⇒ Object (readonly)
Returns the value of attribute update_files.
10 11 12 |
# File 'app/factories/bulkrax/object_factory.rb', line 10 def update_files @update_files end |
#work_identifier ⇒ Object (readonly)
Returns the value of attribute work_identifier.
10 11 12 |
# File 'app/factories/bulkrax/object_factory.rb', line 10 def work_identifier @work_identifier end |
Instance Method Details
#create ⇒ Object
An ActiveFedora bug when there are many habtm <-> has_many associations means they won’t all get saved. github.com/projecthydra/active_fedora/issues/874 2+ years later, still open!
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/factories/bulkrax/object_factory.rb', line 96 def create attrs = transform_attributes @object = klass.new object.reindex_extent = Hyrax::Adapters::NestingIndexAdapter::LIMITED_REINDEX if object.respond_to?(:reindex_extent) run_callbacks :save do run_callbacks :create do if klass == Collection create_collection(attrs) elsif klass == FileSet create_file_set(attrs) else work_actor.create(environment(attrs)) end end end object.(@user) && object.save! if object.depositor.nil? log_created(object) end |
#find ⇒ Object
68 69 70 71 |
# File 'app/factories/bulkrax/object_factory.rb', line 68 def find return find_by_id if attributes[:id].present? return search_by_identifier if attributes[work_identifier].present? end |
#find_by_id ⇒ Object
73 74 75 |
# File 'app/factories/bulkrax/object_factory.rb', line 73 def find_by_id klass.find(attributes[:id]) if klass.exists?(attributes[:id]) end |
#find_or_create ⇒ Object
77 78 79 80 81 |
# File 'app/factories/bulkrax/object_factory.rb', line 77 def find_or_create o = find return o if o run(&:save!) end |
#log_created(obj) ⇒ Object
115 116 117 118 |
# File 'app/factories/bulkrax/object_factory.rb', line 115 def log_created(obj) msg = "Created #{klass.model_name.human} #{obj.id}" Rails.logger.info("#{msg} (#{Array(attributes[work_identifier]).first})") end |
#log_deleted_fs(obj) ⇒ Object
125 126 127 128 |
# File 'app/factories/bulkrax/object_factory.rb', line 125 def log_deleted_fs(obj) msg = "Deleted All Files from #{obj.id}" Rails.logger.info("#{msg} (#{Array(attributes[work_identifier]).first})") end |
#log_updated(obj) ⇒ Object
120 121 122 123 |
# File 'app/factories/bulkrax/object_factory.rb', line 120 def log_updated(obj) msg = "Updated #{klass.model_name.human} #{obj.id}" Rails.logger.info("#{msg} (#{Array(attributes[work_identifier]).first})") end |
#run {|object| ... } ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/factories/bulkrax/object_factory.rb', line 31 def run arg_hash = { id: attributes[:id], name: 'UPDATE', klass: klass } @object = find if object object.reindex_extent = Hyrax::Adapters::NestingIndexAdapter::LIMITED_REINDEX if object.respond_to?(:reindex_extent) ActiveSupport::Notifications.instrument('import.importer', arg_hash) { update } else ActiveSupport::Notifications.instrument('import.importer', arg_hash.merge(name: 'CREATE')) { create } end yield(object) if block_given? object end |
#run! ⇒ Object
44 45 46 47 48 49 |
# File 'app/factories/bulkrax/object_factory.rb', line 44 def run! self.run # Create the error exception if the object is not validly saved for some reason raise ActiveFedora::RecordInvalid, object if !object.persisted? || object.changed? object end |
#search_by_identifier ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'app/factories/bulkrax/object_factory.rb', line 83 def search_by_identifier query = { work_identifier => source_identifier_value } # Query can return partial matches (something6 matches both something6 and something68) # so we need to weed out any that are not the correct full match. But other items might be # in the multivalued field, so we have to go through them one at a time. match = klass.where(query).detect { |m| m.send(work_identifier).include?(source_identifier_value) } return match if match end |
#update ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/factories/bulkrax/object_factory.rb', line 51 def update raise "Object doesn't exist" unless object destroy_existing_files if @replace_files && ![Collection, FileSet].include?(klass) attrs = transform_attributes(update: true) run_callbacks :save do if klass == Collection update_collection(attrs) elsif klass == FileSet update_file_set(attrs) else work_actor.update(environment(attrs)) end end object.(@user) && object.save! if object.depositor.nil? log_updated(object) end |
#with_files ⇒ Object
update files is set, replace files is set or this is a create
27 28 29 |
# File 'app/factories/bulkrax/object_factory.rb', line 27 def with_files update_files || replace_files || !object end |