Class: Bulkrax::ObjectFactory

Inherits:
ObjectFactoryInterface show all
Includes:
FileFactory
Defined in:
app/factories/bulkrax/object_factory.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary

Attributes inherited from ObjectFactoryInterface

#attributes, #importer_run_id, #klass, #object, #related_parents_parsed_mapping, #replace_files, #source_identifier_value, #update_files, #user, #work_identifier, #work_identifier_search_field

Class Method Interface collapse

Instance Method Summary collapse

Methods inherited from ObjectFactoryInterface

add_user_to_collection_permissions, #add_user_to_collection_permissions, #base_permitted_attributes, #create, default_admin_set_id, default_admin_set_or_nil, #find, #find_or_create, find_or_nil, #initialize, #run, #run!, #search_by_identifier, #transformation_removes_blank_hash_values, #update

Methods included from Loggable

#log_created, #log_deleted_fs, #log_updated

Methods included from DynamicRecordLookup

#find_record

Constructor Details

This class inherits a constructor from Bulkrax::ObjectFactoryInterface

Class Method Details

.add_child_to_parent_work(parent:, child:) ⇒ Object

Note:

This does not save either object. We need to do that in another loop. Why? Because we might be adding many items to the parent.



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

def self.add_child_to_parent_work(parent:, child:)
  return true if parent.ordered_members.to_a.include?(child)

  parent.ordered_members << child
end

.add_resource_to_collection(collection:, resource:, user:) ⇒ Object



20
21
22
23
24
25
# File 'app/factories/bulkrax/object_factory.rb', line 20

def self.add_resource_to_collection(collection:, resource:, user:)
  collection.try(:reindex_extent=, Hyrax::Adapters::NestingIndexAdapter::LIMITED_REINDEX) if
    defined?(Hyrax::Adapters::NestingIndexAdapter)
  resource.member_of_collections << collection
  save!(resource: resource, user: user)
end

.clean!Object



129
130
131
132
133
# File 'app/factories/bulkrax/object_factory.rb', line 129

def self.clean!
  super do
    ActiveFedora::Cleaner.clean!
  end
end

.export_propertiesObject



33
34
35
36
37
# File 'app/factories/bulkrax/object_factory.rb', line 33

def self.export_properties
  # TODO: Consider how this may or may not work for Valkyrie
  properties = Bulkrax.curation_concerns.map { |work| work.properties.keys }.flatten.uniq.sort
  properties.reject { |prop| Bulkrax.reserved_properties.include?(prop) }
end

.field_multi_value?(field:, model:) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'app/factories/bulkrax/object_factory.rb', line 39

def self.field_multi_value?(field:, model:)
  return false unless field_supported?(field: field, model: model)
  return false unless model.singleton_methods.include?(:properties)

  model&.properties&.[](field)&.[]("multiple")
end

.field_supported?(field:, model:) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/factories/bulkrax/object_factory.rb', line 46

def self.field_supported?(field:, model:)
  model.method_defined?(field) && model.properties[field].present?
end

.file_sets_for(resource:) ⇒ Object



50
51
52
53
54
55
# File 'app/factories/bulkrax/object_factory.rb', line 50

def self.file_sets_for(resource:)
  return [] if resource.blank?
  return [resource] if resource.is_a?(Bulkrax.file_model_class)

  resource.file_sets
end

.find(id) ⇒ Object



60
61
62
63
64
# File 'app/factories/bulkrax/object_factory.rb', line 60

def self.find(id)
  ActiveFedora::Base.find(id)
rescue ActiveFedora::ObjectNotFoundError => e
  raise ObjectFactoryInterface::ObjectNotFoundError, e.message
end

.find_or_create_default_admin_setObject



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

def self.find_or_create_default_admin_set
  # NOTE: Hyrax 5+ removed this method
  AdminSet.find_or_create_default_admin_set_id
end

.ordered_file_sets_for(object) ⇒ Object



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

def self.ordered_file_sets_for(object)
  object&.ordered_members.to_a.select(&:file_set?)
end

.publishObject



71
72
73
# File 'app/factories/bulkrax/object_factory.rb', line 71

def self.publish(**)
  return true
end

.query(q, **kwargs) ⇒ Object

rubocop:enable Metrics/ParameterLists



125
126
127
# File 'app/factories/bulkrax/object_factory.rb', line 125

def self.query(q, **kwargs)
  ActiveFedora::SolrService.query(q, **kwargs)
end

.save!(resource:) ⇒ Object



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

def self.save!(resource:, **)
  resource.save!
end

.search_by_property(value:, klass:, field: nil, search_field: nil, name_field: nil, verify_property: false) ⇒ NilClass, ActiveFedora::Base

Note:

HEY WE’RE USING THIS FOR A WINGS CUSTOM QUERY. BE CAREFUL WITH REMOVING IT.

rubocop:disable Metrics/ParameterLists

Parameters:

  • value (String)
  • klass (Class, #where)
  • field (String, Symbol) (defaults to: nil)

    A convenience parameter where we pass the same value to search_field and name_field.

  • search_field (String, Symbol) (defaults to: nil)

    the Solr field name (e.g. “title_tesim”)

  • name_field (String) (defaults to: nil)

    the ActiveFedora::Base property name (e.g. “title”)

  • verify_property (TrueClass) (defaults to: false)

    when true, verify that the given :klass

Returns:

  • (NilClass)

    when no object is found.

  • (ActiveFedora::Base)

    when a match is found, an instance of given :klass

See Also:

  • {Wings::CustomQueries::FindBySourceIdentifier#find_by_model_and_property_value}


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/factories/bulkrax/object_factory.rb', line 95

def self.search_by_property(value:, klass:, field: nil, search_field: nil, name_field: nil, verify_property: false)
  return nil unless klass.respond_to?(:where)
  # We're not going to try to match nil nor "".
  return if value.blank?
  return if verify_property && !klass.properties.keys.include?(search_field)

  search_field ||= field
  name_field ||= field
  raise "You must provide either (search_field AND name_field) OR field parameters" if search_field.nil? || name_field.nil?
  # NOTE: 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.
  #
  # A ssi field is string, so we're looking at exact matches.
  # A tesi field is text, so partial matches work.
  #
  # We need to wrap the result in an Array, else we might have a scalar that
  # will result again in partial matches.
  match = klass.where(search_field => value).detect do |m|
    # Don't use Array.wrap as we likely have an ActiveTriples::Relation
    # which defiantly claims to be an Array yet does not behave consistently
    # with an Array.  Hopefully the name_field is not a Date or Time object,
    # Because that too will be a mess.
    Array(m.send(name_field)).include?(value)
  end
  return match if match
end

.solr_name(field_name) ⇒ Object



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

def self.solr_name(field_name)
  if defined?(Hyrax)
    Hyrax.index_field_mapper.solr_name(field_name)
  else
    ActiveFedora.index_field_mapper.solr_name(field_name)
  end
end

.update_index(resources: []) ⇒ Object



151
152
153
# File 'app/factories/bulkrax/object_factory.rb', line 151

def self.update_index(resources: [])
  Array(resources).each(&:update_index)
end

.update_index_for_file_sets_of(resource:) ⇒ Object



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

def self.update_index_for_file_sets_of(resource:)
  resource.file_sets.each(&:update_index) if resource.respond_to?(:file_sets)
end

Instance Method Details

#delete(_user) ⇒ Object



167
168
169
# File 'app/factories/bulkrax/object_factory.rb', line 167

def delete(_user)
  find&.delete
end

#find_by_idObject



157
158
159
160
161
162
163
164
165
# File 'app/factories/bulkrax/object_factory.rb', line 157

def find_by_id
  return false if attributes[:id].blank?
  # Rails / Ruby upgrade, we moved from :exists? to :exist?  However we want to continue (for a
  # bit) to support older versions.
  method_name = klass.respond_to?(:exist?) ? :exist? : :exists?
  klass.find(attributes[:id]) if klass.send(method_name, attributes[:id])
rescue Valkyrie::Persistence::ObjectNotFoundError
  false
end