Skip to content

Instantly share code, notes, and snippets.

@helrond
Created May 13, 2015 16:01
Show Gist options
  • Save helrond/5ca8f16f889cf90e4aff to your computer and use it in GitHub Desktop.
Save helrond/5ca8f16f889cf90e4aff to your computer and use it in GitHub Desktop.
ExportHelpers.module_eval do;
def generate_labels(id)
obj = resolve_references(Resource.to_jsonmodel(id), ['tree', 'repository', 'archival_object', 'parent'])
labels = ASpaceExport.model(:labels).from_resource(JSONModel(:resource).new(obj))
ASpaceExport::serialize(labels, :serializer => :tsv)
end
end
class LabelModel < ASpaceExport::ExportModel
model_for :labels
def headers
%w(Resource\ Title Resource\ Identifier Parents Box\ Number)
end
def rows
@rows.uniq{|r| r['container']}.map {|r| [self.title, self.identifier, r['parent'], r['container']]}
end
def generate_label_rows(objects)
rows = []
objects.each do |obj|
if obj.parent
ref = obj.parent['ref']
component = ArchivalObject.to_jsonmodel(JSONModel(:archival_object).id_for(ref))
end
instances = obj.instances
instances.each do |i|
c = i['container']
next unless c
if c['type_1'] && c['indicator_1']
container = "#{c['type_1'].capitalize} #{c['indicator_1']}"
crow = {
'container' => container,
'parent' => parent,
}
end
rows << crow
end
rows.push(*generate_label_rows(obj.children))
end
rows
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment