Class: Bulkrax::ObjectFactory
- Inherits:
-
Object
- Object
- Bulkrax::ObjectFactory
- Extended by:
- ActiveModel::Callbacks
- Includes:
- DynamicRecordLookup, FileFactory
- Defined in:
- app/factories/bulkrax/object_factory.rb
Overview
rubocop:disable Metrics/ClassLength
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
-
#base_permitted_attributes ⇒ Array<Symbol>
private
These are the attributes that we assume all “work type” classes (e.g. the given :klass) will have in addition to their specific attributes.
-
#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
- #transformation_removes_blank_hash_values ⇒ Boolean
- #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
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 = @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.
31 32 33 |
# File 'app/factories/bulkrax/object_factory.rb', line 31 def attributes @attributes end |
#importer_run_id ⇒ Object (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 |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
31 32 33 |
# File 'app/factories/bulkrax/object_factory.rb', line 31 def klass @klass end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
31 32 33 |
# File 'app/factories/bulkrax/object_factory.rb', line 31 def object @object end |
#related_parents_parsed_mapping ⇒ Object (readonly)
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 end |
#replace_files ⇒ Object (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_value ⇒ Object (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_files ⇒ Object (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_identifier ⇒ Object (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_attributes ⇒ Array<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.
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] |
#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!
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'app/factories/bulkrax/object_factory.rb', line 123 def create attrs = transform_attributes @object = klass.new object.reindex_extent = Hyrax::Adapters::NestingIndexAdapter::LIMITED_REINDEX if defined?(Hyrax::Adapters::NestingIndexAdapter) && 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 |
#find ⇒ Object
89 90 91 92 93 |
# File 'app/factories/bulkrax/object_factory.rb', line 89 def find found = find_by_id if attributes[:id].present? return found if found.present? return search_by_identifier if attributes[work_identifier].present? end |
#find_by_id ⇒ Object
95 96 97 |
# File 'app/factories/bulkrax/object_factory.rb', line 95 def find_by_id klass.find(attributes[:id]) if klass.exists?(attributes[:id]) end |
#find_or_create ⇒ Object
99 100 101 102 103 |
# File 'app/factories/bulkrax/object_factory.rb', line 99 def find_or_create o = find return o if o run(&:save!) end |
#log_created(obj) ⇒ Object
142 143 144 145 |
# File 'app/factories/bulkrax/object_factory.rb', line 142 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
152 153 154 155 |
# File 'app/factories/bulkrax/object_factory.rb', line 152 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
147 148 149 150 |
# File 'app/factories/bulkrax/object_factory.rb', line 147 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
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
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_identifier ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/factories/bulkrax/object_factory.rb', line 105 def search_by_identifier # TODO(alishaevn): return the proper `work_index` value below # ref: https://github.com/samvera-labs/bulkrax/issues/866 # ref:https://github.com/samvera-labs/bulkrax/issues/867 # work_index = ::ActiveFedora.index_field_mapper.solr_name(work_identifier, :facetable) work_index = work_identifier query = { work_index => 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_values ⇒ Boolean
28 |
# File 'app/factories/bulkrax/object_factory.rb', line 28 class_attribute :transformation_removes_blank_hash_values, default: false |
#update ⇒ Object
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_files ⇒ Object
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 |