Skip to content

Commit 032ce47

Browse files
authored
🐛 Normalizing export column names (#847)
Prior to this commit, when exporting a FileSet and GenericWork we'd see a "creator_1" and "creator" column. The "creator" column might look as follows: `["Sandra Samvera"]`. Which meant that creator was not an `ActiveTriples::Relation` (based on the prior logic). Yet the "creator" field was stored as multi-value. Perhaps having been previously coerced into an Array. With this commit, we're favoring the Bulkrax parser field mapping `"join"` configuration over whether or not the object is an =ActiveTriples::Relation=. That is to say, if you told us to join the field, we're going to do that regardless of the data we have. Likewise, if the field's value is not an enumerable, we're not going to introduce an ordinal suffix (e.g. "creator_1" when we have a scalar creator). In other words don't do the join logic if we don't have an "array" or we weren't told to join arrays. Perhaps we could interrogate the model to ask if it's single value or not? But this reduces the implementation knowledge of the properties by looking at a more primitive level (is the data multi-valued or not). Related to: - notch8/palni-palci#624 - #839
1 parent f2c2652 commit 032ce47

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

app/models/bulkrax/csv_entry.rb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -243,20 +243,17 @@ def build_object(_key, value)
243243
object_metadata(Array.wrap(data))
244244
end
245245

246-
def build_value(key, value)
247-
return unless hyrax_record.respond_to?(key.to_s)
248-
249-
data = hyrax_record.send(key.to_s)
250-
if data.is_a?(ActiveTriples::Relation)
251-
if value['join']
252-
self.parsed_metadata[key_for_export(key)] = data.map { |d| prepare_export_data(d) }.join(Bulkrax.multi_value_element_join_on).to_s
253-
else
254-
data.each_with_index do |d, i|
255-
self.parsed_metadata["#{key_for_export(key)}_#{i + 1}"] = prepare_export_data(d)
256-
end
257-
end
246+
def build_value(property_name, mapping_config)
247+
return unless hyrax_record.respond_to?(property_name.to_s)
248+
249+
data = hyrax_record.send(property_name.to_s)
250+
251+
if mapping_config['join'] || !data.is_a?(Enumerable)
252+
self.parsed_metadata[key_for_export(property_name)] = prepare_export_data_with_join(data)
258253
else
259-
self.parsed_metadata[key_for_export(key)] = prepare_export_data(data)
254+
data.each_with_index do |d, i|
255+
self.parsed_metadata["#{key_for_export(property_name)}_#{i + 1}"] = prepare_export_data(d)
256+
end
260257
end
261258
end
262259

@@ -269,6 +266,14 @@ def key_for_export(key)
269266
"#{unnumbered_key}#{key.sub(clean_key, '')}"
270267
end
271268

269+
def prepare_export_data_with_join(data)
270+
# Yes...it's possible we're asking to coerce a multi-value but only have a single value.
271+
return data.to_s unless data.is_a?(Enumerable)
272+
return "" if data.empty?
273+
274+
data.map { |d| prepare_export_data(d) }.join(Bulkrax.multi_value_element_join_on).to_s
275+
end
276+
272277
def prepare_export_data(datum)
273278
if datum.is_a?(ActiveTriples::Resource)
274279
datum.to_uri.to_s

spec/models/bulkrax/csv_entry_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ class ::Avacado < Work
916916
allow(work_obj).to receive(:member_work_ids).and_return([])
917917
allow(work_obj).to receive(:multiple_objects).and_return(source_id_rel)
918918
allow(source_id_rel).to receive(:is_a?).with(ActiveTriples::Relation).and_return(true)
919+
allow(source_id_rel).to receive(:is_a?).with(Enumerable).and_return(true)
919920
allow(source_id_rel).to receive(:to_a).and_return(['test_work_source_id'])
920921
allow(source_id_rel).to receive(:each_with_index).and_return(['test_work_source_id', 0])
921922
end

0 commit comments

Comments
 (0)