Module: Rooftop::Nested

Defined in:
lib/rooftop/nested.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
# File 'lib/rooftop/nested.rb', line 4

def self.included(base)
  @nested_classes ||= []
  @nested_classes << base unless @nested_classes.include?(base)
end

.nested_classesObject



9
10
11
# File 'lib/rooftop/nested.rb', line 9

def self.nested_classes
  @nested_classes
end

Instance Method Details

#ancestorsObject



17
18
19
20
21
22
23
# File 'lib/rooftop/nested.rb', line 17

def ancestors
  if respond_to?(:resource_links)
    resource_links.find_by(link_type: "#{Rooftop::ResourceLinks::CUSTOM_LINK_RELATION_BASE}/ancestors")
  else
    []
  end
end

#childrenObject



25
26
27
28
29
30
31
# File 'lib/rooftop/nested.rb', line 25

def children
  if respond_to?(:resource_links)
    resource_links.find_by(link_type: "#{Rooftop::ResourceLinks::CUSTOM_LINK_RELATION_BASE}/children")
  else
    []
  end
end

#parentObject



33
34
35
36
37
# File 'lib/rooftop/nested.rb', line 33

def parent
  if respond_to?(:resource_links) && resource_links
    ancestors.first
  end
end

#rootObject



13
14
15
# File 'lib/rooftop/nested.rb', line 13

def root
  ancestors.last || resource_links.find_by(link_type: 'self').first
end

#siblingsObject



39
40
41
# File 'lib/rooftop/nested.rb', line 39

def siblings
  self.class.find(parent.id).children.reject! {|c| c.id == self.id}
end