Class: Bulkrax::ValkyrieObjectFactory
Overview
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Classes: FileFactoryInnerWorkings
Instance Attribute Summary
#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
-
.add_child_to_parent_work(parent:, child:) ⇒ Object
-
.add_resource_to_collection(collection:, resource:, user:) ⇒ Object
-
.field_multi_value?(field:, model:) ⇒ Boolean
-
.field_supported?(field:, model:) ⇒ Boolean
-
.file_sets_for(resource:) ⇒ Object
-
.find(id) ⇒ Object
-
.find_or_create_default_admin_set ⇒ Object
-
.ordered_file_sets_for(object) ⇒ Object
-
.publish(event:, **kwargs) ⇒ Object
-
.query(q, **kwargs) ⇒ Object
-
.save!(resource:, user:) ⇒ Object
-
.schema_properties(klass) ⇒ Array<String>
Retrieve properties from M3 model.
-
.search_by_property(value:, klass:, field: nil, name_field: nil) ⇒ NilClass, Valkyrie::Resource
rubocop:disable Metrics/ParameterLists.
-
.solr_name(field_name) ⇒ Object
-
.update_index(resources:) ⇒ Object
-
.update_index_for_file_sets_of(resource:) ⇒ Object
-
#delete(user) ⇒ Object
-
#run! ⇒ Object
Class Method Summary
collapse
Instance Method Summary
collapse
add_user_to_collection_permissions, #add_user_to_collection_permissions, #base_permitted_attributes, clean!, #create, default_admin_set_id, default_admin_set_or_nil, export_properties, #find, #find_or_create, find_or_nil, #initialize, #run, #search_by_identifier, #transformation_removes_blank_hash_values, #update
Methods included from Loggable
#log_created, #log_deleted_fs, #log_updated
#find_record
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.
68
69
70
71
72
73
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 68
def self.add_child_to_parent_work(parent:, child:)
return true if parent.member_ids.include?(child.id)
parent.member_ids << child.id
parent.save
end
|
.add_resource_to_collection(collection:, resource:, user:) ⇒ Object
75
76
77
78
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 75
def self.add_resource_to_collection(collection:, resource:, user:)
resource.member_of_collection_ids << collection.id
save!(resource: resource, user: user)
end
|
.field_multi_value?(field:, model:) ⇒ Boolean
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 80
def self.field_multi_value?(field:, model:)
return false unless field_supported?(field: field, model: model)
if model.respond_to?(:schema)
schema = model.new.singleton_class.schema || model.schema
dry_type = schema.key(field.to_sym)
return true if dry_type.respond_to?(:primitive) && dry_type.primitive == Array
false
else
Bulkrax::ObjectFactory.field_multi_value?(field: field, model: model)
end
end
|
.field_supported?(field:, model:) ⇒ Boolean
94
95
96
97
98
99
100
101
102
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 94
def self.field_supported?(field:, model:)
if model.respond_to?(:schema)
schema_properties(model).include?(field)
else
Bulkrax::ObjectFactory.field_supported?(field: field, model: model)
end
end
|
.file_sets_for(resource:) ⇒ Object
104
105
106
107
108
109
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 104
def self.file_sets_for(resource:)
return [] if resource.blank?
return [resource] if resource.is_a?(Bulkrax.file_model_class)
Hyrax.query_service.custom_queries.find_child_file_sets(resource: resource)
end
|
.find_or_create_default_admin_set ⇒ Object
119
120
121
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 119
def self.find_or_create_default_admin_set
Hyrax::AdminSetCreateService.find_or_create_default_admin_set
end
|
.ordered_file_sets_for(object) ⇒ Object
203
204
205
206
207
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 203
def self.ordered_file_sets_for(object)
return [] if object.blank?
Hyrax.custom_queries.find_child_file_sets(resource: object)
end
|
.publish(event:, **kwargs) ⇒ Object
129
130
131
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 129
def self.publish(event:, **kwargs)
Hyrax.publisher.publish(event, **kwargs)
end
|
.query(q, **kwargs) ⇒ Object
133
134
135
136
137
138
139
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 133
def self.query(q, **kwargs)
raise NotImplementedError, "#{self}.#{__method__}" unless defined?(Hyrax)
Hyrax::SolrService.query(q, **kwargs)
end
|
.save!(resource:, user:) ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 141
def self.save!(resource:, user:)
if resource.respond_to?(:save!)
resource.save!
else
result = Hyrax.persister.save(resource: resource)
raise Valkyrie::Persistence::ObjectNotFoundError unless result
Hyrax.index_adapter.save(resource: result)
if result.collection?
publish('collection.metadata.updated', collection: result, user: user)
else
publish('object.metadata.updated', object: result, user: user)
end
resource
end
end
|
.schema_properties(klass) ⇒ Array<String>
Retrieve properties from M3 model
193
194
195
196
197
198
199
200
201
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 193
def self.schema_properties(klass)
@schema_properties_map ||= {}
klass_key = klass.name
schema = klass.new.singleton_class.schema || klass.schema
@schema_properties_map[klass_key] = schema.map { |k| k.name.to_s } unless @schema_properties_map.key?(klass_key)
@schema_properties_map[klass_key]
end
|
.search_by_property(value:, klass:, field: nil, name_field: nil) ⇒ NilClass, Valkyrie::Resource
rubocop:disable Metrics/ParameterLists
179
180
181
182
183
184
185
186
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 179
def self.search_by_property(value:, klass:, field: nil, name_field: nil, **)
name_field ||= field
raise "Expected named_field or field got nil" if name_field.blank?
return if value.blank?
Hyrax.query_service.custom_query.find_by_model_and_property_value(model: klass, property: name_field, value: value)
end
|
.solr_name(field_name) ⇒ Object
123
124
125
126
127
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 123
def self.solr_name(field_name)
raise NotImplementedError, "#{self}.#{__method__}" unless defined?(Hyrax)
Hyrax.config.index_field_mapper.solr_name(field_name)
end
|
.transactions ⇒ Object
When you want a different set of transactions you can change the container.
54
55
56
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 54
def self.transactions
@transactions || Hyrax::Transactions::Container
end
|
.update_index(resources:) ⇒ Object
157
158
159
160
161
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 157
def self.update_index(resources:)
Array(resources).each do |resource|
Hyrax.index_adapter.save(resource: resource)
end
end
|
.update_index_for_file_sets_of(resource:) ⇒ Object
163
164
165
166
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 163
def self.update_index_for_file_sets_of(resource:)
file_sets = Hyrax.query_service.custom_queries.find_child_file_sets(resource: resource)
update_index(resources: file_sets)
end
|
Instance Method Details
#delete(user) ⇒ Object
209
210
211
212
213
214
215
216
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 209
def delete(user)
obj = find
return false unless obj
Hyrax.persister.delete(resource: obj)
Hyrax.index_adapter.delete(resource: obj)
self.class.publish(event: 'object.deleted', object: obj, user: user)
end
|
#run! ⇒ Object
218
219
220
221
222
223
224
225
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 218
def run!
run
object = find
return object if object&.persisted?
raise(ObjectFactoryInterface::RecordInvalid, object)
end
|
#transactions ⇒ Object
58
59
60
|
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 58
def transactions
self.class.transactions
end
|