Class: ActiveRecordSurvey::NodeMap

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/active_record_survey/node_map.rb

Instance Method Summary collapse

Instance Method Details

#ancestors_until_node_not_ancestor_of(klass) ⇒ Object

Gets all the ancestor nodes until one is not an ancestor of klass



41
42
43
44
45
46
47
# File 'lib/active_record_survey/node_map.rb', line 41

def ancestors_until_node_not_ancestor_of(klass)
  if !self.parent || !self.node.class.ancestors.include?(klass)
    return []
  end

  [self] + self.parent.ancestors_until_node_not_ancestor_of(klass)
end

#as_map(options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_record_survey/node_map.rb', line 20

def as_map(options)
  node_maps = options[:node_maps]

  c = (node_maps.nil?)? self.children : node_maps.select { |i|
    i.parent == self && !i.marked_for_destruction?
  }.sort { |a,b| a.left<=>b.left }

  result = {}
  result.merge!({ :id => self.id, :node_id => ((self.node.respond_to?(:id))? self.node.id : "") }) if !options[:no_ids] && !self.node.nil?
  result.merge!({
    :type => ((!self.node.nil?)? self.node.class.to_s : ""),
    :children => c.collect { |i|
      i.as_map(options)
    }
  })
  

  result
end

#children_until_node_not_ancestor_of(klass) ⇒ Object

Gets all the child nodes until one is not an ancestor of klass



50
51
52
53
54
55
56
57
58
# File 'lib/active_record_survey/node_map.rb', line 50

def children_until_node_not_ancestor_of(klass)
  if !self.node.class.ancestors.include?(klass)
    return []
  end

  [self] + self.children.collect { |i|
    i.children_until_node_not_ancestor_of(klass)
  }
end

#has_infinite_loop?(path = []) ⇒ Boolean

Check to see whether there is an infinite loop from this node_map

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
# File 'lib/active_record_survey/node_map.rb', line 61

def has_infinite_loop?(path = [])
  self.survey.node_maps.select { |i| i.parent == self && !i.marked_for_destruction? }.each { |i|
    # Detect infinite loop
    if path.include?(self.node) || i.has_infinite_loop?(path.clone.push(self.node))
      return true
    end
  }
  path.include?(self.node)
end

#mark_self_and_children_for_destructionObject



71
72
73
74
75
76
77
78
# File 'lib/active_record_survey/node_map.rb', line 71

def mark_self_and_children_for_destruction
  removed = [self]
  self.mark_for_destruction
  self.children.each { |i|
    removed.concat(i.mark_self_and_children_for_destruction)
  }
  removed
end

#recursive_cloneObject

Recursively creates a copy of this entire node_map



11
12
13
14
15
16
17
18
# File 'lib/active_record_survey/node_map.rb', line 11

def recursive_clone
  node_map = self.survey.node_maps.build(:survey => self.survey, :node => self.node)
  self.survey.node_maps.select { |i| i.parent == self && !i.marked_for_destruction? }.each { |child_node|
    child_node.survey = self.survey # required due to voodoo - we want to use the same survey with the same object_id
    node_map.children << child_node.recursive_clone
  }
  node_map
end