Created
May 20, 2015 15:19
-
-
Save helrond/7ed19e8c378f764b29a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ExportHelpers.module_eval do; | |
def generate_labels(id) | |
obj = resolve_references(Resource.to_jsonmodel(id), ['tree', 'repository']) | |
labels = ASpaceExport.model(:labels).from_resource(JSONModel(:resource).new(obj)) | |
ASpaceExport::stream(labels) | |
end | |
end | |
class LabelModel < ASpaceExport::ExportModel | |
model_for :labels | |
include JSONModel | |
def headers | |
%w(Resource\ Title Resource\ Identifier Parents Restrictions Box\ Number) | |
end | |
def rows | |
@rows.uniq{|r| r['container']+r['parents']}.map {|r| [self.title, self.identifier, r['parents'], r['restrictions'], r['container']]} | |
end | |
def get_parents(ref, parents) | |
component = ArchivalObject.to_jsonmodel(JSONModel(:archival_object).id_for(ref)) | |
if component['level']!='file' && component['level']!='item' | |
title = component['level'].capitalize + ' ' + component['component_id'] + ' ' + component['display_string'] | |
else | |
title = component['display_string'] | |
end | |
parents << title | |
if component['parent'] | |
get_parents(component['parent']['ref'], parents) | |
end | |
parents.reverse.join(' > ') | |
end | |
def generate_label_rows(objects) | |
rows = [] | |
objects.each do |obj| | |
if obj.parent && obj.instances | |
parents = get_parents(obj.parent['ref'], []) | |
restrictions = [] | |
if obj.notes | |
obj.notes.each do |n| | |
if n['type'] = 'accessrestrict' | |
restrictions = 'Restricted material' | |
end | |
end | |
restrictions | |
end | |
restrictions | |
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, | |
'parents' => parents, | |
'restrictions' => restrictions | |
} | |
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