Class: AncestorListing
- Inherits:
-
Object
- Object
- AncestorListing
- Defined in:
- backend/app/model/ancestor_listing.rb
Defined Under Namespace
Classes: Ancestor
Class Method Summary collapse
-
.add_ancestors(objs, jsons) ⇒ Object
For a given set of Sequel
objs
and their correspondingjsons
records, walk back up the tree enumerating their ancestor records and add them as refs to theancestors
JSON property.
Instance Method Summary collapse
-
#has_level? ⇒ Boolean
-
#initialize(node_class, objs) ⇒ AncestorListing
constructor
A new instance of AncestorListing.
-
#parent_of(node_id) ⇒ Object
The parent of the given node.
-
#root_record(root_record_id) ⇒ Object
The root record of the given node.
Constructor Details
#initialize(node_class, objs) ⇒ AncestorListing
Returns a new instance of AncestorListing.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'backend/app/model/ancestor_listing.rb', line 45 def initialize(node_class, objs) @node_model = node_class @root_model = node_class.root_model @ancestors = {} @root_records = {} objs.each do |obj| if has_level? @ancestors[obj.id] = {:id => obj.id, :level => level_value(obj.level, obj.other_level), :parent_id => obj.parent_id, :root_record_id => obj.root_record_id} else @ancestors[obj.id] = {:id => obj.id, :parent_id => obj.parent_id, :root_record_id => obj.root_record_id} end end build_ancestor_links end |
Class Method Details
.add_ancestors(objs, jsons) ⇒ Object
For a given set of Sequel objs
and their corresponding jsons
records,
walk back up the tree enumerating their ancestor records and add them as
refs to the ancestors
JSON property.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'backend/app/model/ancestor_listing.rb', line 6 def self.add_ancestors(objs, jsons) return if objs.empty? node_class = objs[0].class ancestors = new(node_class, objs) jsons.zip(objs).each do |json, obj| json['ancestors'] = [] current = obj while (parent = ancestors.parent_of(current.id)) if ancestors.has_level? json['ancestors'] << {'ref' => parent.uri, 'level' => parent.level} else json['ancestors'] << {'ref' => parent.uri} end current = parent end root = ancestors.root_record(obj[:root_record_id]) if ancestors.has_level? json['ancestors'] << { 'ref' => root.uri, 'level' => root.level } else json['ancestors'] << { 'ref' => root.uri } end end end |
Instance Method Details
#has_level? ⇒ Boolean
41 42 43 |
# File 'backend/app/model/ancestor_listing.rb', line 41 def has_level? @node_model != DigitalObjectComponent end |
#parent_of(node_id) ⇒ Object
The parent of the given node
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'backend/app/model/ancestor_listing.rb', line 69 def parent_of(node_id) parent_id = @ancestors.fetch(node_id)[:parent_id] if parent_id ancestor = @ancestors.fetch(parent_id) if has_level? Ancestor.new(ancestor[:id], @node_model.uri_for(@node_model.my_jsonmodel.record_type, ancestor[:id]), ancestor[:level]) else Ancestor.new(ancestor[:id], @node_model.uri_for(@node_model.my_jsonmodel.record_type, ancestor[:id]), nil) end end end |
#root_record(root_record_id) ⇒ Object
The root record of the given node
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'backend/app/model/ancestor_listing.rb', line 88 def root_record(root_record_id) root_record = @root_records.fetch(root_record_id) if has_level? Ancestor.new(root_record[:id], @root_model.uri_for(@root_model.my_jsonmodel.record_type, root_record[:id]), root_record[:level]) else Ancestor.new(root_record[:id], @root_model.uri_for(@root_model.my_jsonmodel.record_type, root_record[:id]), nil) end end |