Class: Bulkrax::ObjectFactory

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
DynamicRecordLookup, FileFactory
Defined in:
app/factories/bulkrax/object_factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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



34
35
36
37
38
39
40
41
42
43
44
# File 'app/factories/bulkrax/object_factory.rb', line 34

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
  @related_parents_parsed_mapping = related_parents_parsed_mapping
  @source_identifier_value = source_identifier_value
  @klass = klass || Bulkrax.default_work_type.constantize
  @importer_run_id = importer_run_id
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



31
32
33
# File 'app/factories/bulkrax/object_factory.rb', line 31

def attributes
  @attributes
end

#importer_run_idObject (readonly)

Returns the value of attribute importer_run_id.



31
32
33
# File 'app/factories/bulkrax/object_factory.rb', line 31

def importer_run_id
  @importer_run_id
end

#klassObject (readonly)

Returns the value of attribute klass.



31
32
33
# File 'app/factories/bulkrax/object_factory.rb', line 31

def klass
  @klass
end

#objectObject (readonly)

Returns the value of attribute object.



31
32
33
# File 'app/factories/bulkrax/object_factory.rb', line 31

def object
  @object
end

Returns the value of attribute related_parents_parsed_mapping.



31
32
33
# File 'app/factories/bulkrax/object_factory.rb', line 31

def related_parents_parsed_mapping
  @related_parents_parsed_mapping
end

#replace_filesObject (readonly)

Returns the value of attribute replace_files.



31
32
33
# File 'app/factories/bulkrax/object_factory.rb', line 31

def replace_files
  @replace_files
end

#source_identifier_valueObject (readonly)

Returns the value of attribute source_identifier_value.



31
32
33
# File 'app/factories/bulkrax/object_factory.rb', line 31

def source_identifier_value
  @source_identifier_value
end

#update_filesObject (readonly)

Returns the value of attribute update_files.



31
32
33
# File 'app/factories/bulkrax/object_factory.rb', line 31

def update_files
  @update_files
end

#work_identifierObject (readonly)

Returns the value of attribute work_identifier.



31
32
33
# File 'app/factories/bulkrax/object_factory.rb', line 31

def work_identifier
  @work_identifier
end

Instance Method Details

#base_permitted_attributesArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

These are the attributes that we assume all “work type” classes (e.g. the given :klass) will have in addition to their specific attributes.

Returns:

  • (Array<Symbol>)

See Also:

  • #permitted_attributes


16
17
# File 'app/factories/bulkrax/object_factory.rb', line 16

class_attribute :base_permitted_attributes,
default: i[id edit_users edit_groups read_groups visibility work_members_attributes admin_set_id]

#createObject

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!



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/factories/bulkrax/object_factory.rb', line 117

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
        create_work(attrs)
      end
    end
  end
  object.(@user) && object.save! if object.depositor.nil?
  log_created(object)
end

#findObject



89
90
91
92
# File 'app/factories/bulkrax/object_factory.rb', line 89

def find
  return find_by_id if attributes[:id].present?
  return search_by_identifier if attributes[work_identifier].present?
end

#find_by_idObject



94
95
96
# File 'app/factories/bulkrax/object_factory.rb', line 94

def find_by_id
  klass.find(attributes[:id]) if klass.exists?(attributes[:id])
end

#find_or_createObject



98
99
100
101
102
# File 'app/factories/bulkrax/object_factory.rb', line 98

def find_or_create
  o = find
  return o if o
  run(&:save!)
end

#log_created(obj) ⇒ Object



136
137
138
139
# File 'app/factories/bulkrax/object_factory.rb', line 136

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



146
147
148
149
# File 'app/factories/bulkrax/object_factory.rb', line 146

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



141
142
143
144
# File 'app/factories/bulkrax/object_factory.rb', line 141

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

Yields:



52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/factories/bulkrax/object_factory.rb', line 52

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

Raises:

  • (ActiveFedora::RecordInvalid)


65
66
67
68
69
70
# File 'app/factories/bulkrax/object_factory.rb', line 65

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_identifierObject



104
105
106
107
108
109
110
111
112
# File 'app/factories/bulkrax/object_factory.rb', line 104

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

#transformation_removes_blank_hash_valuesBoolean

Examples:

Bulkrax::ObjectFactory.transformation_removes_blank_hash_values = true

Returns:

  • (Boolean)

See Also:



28
# File 'app/factories/bulkrax/object_factory.rb', line 28

class_attribute :transformation_removes_blank_hash_values, default: false

#updateObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/factories/bulkrax/object_factory.rb', line 72

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
      update_work(attrs)
    end
  end
  object.(@user) && object.save! if object.depositor.nil?
  log_updated(object)
end

#with_filesObject

update files is set, replace files is set or this is a create



48
49
50
# File 'app/factories/bulkrax/object_factory.rb', line 48

def with_files
  update_files || replace_files || !object
end