Module: ActiveRecordSurvey::Answer::Chained::InstanceMethods

Defined in:
lib/active_record_survey/node/answer/chained.rb

Instance Method Summary collapse

Instance Method Details

#build_answer(question_node) ⇒ Object

Chain nodes are different - they must find the final answer node added and add to it They must also see if the final answer node then points somewhere else - and fix the links on that



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/active_record_survey/node/answer/chained.rb', line 23

def build_answer(question_node)
	self.survey = question_node.survey

	question_node_maps = self.survey.node_maps.select { |i| i.node == question_node && !i.marked_for_destruction? }

	# No node_maps exist yet from this question
	if question_node_maps.length === 0
		# Build our first node-map
		question_node_maps << self.survey.node_maps.build(:node => question_node, :survey => self.survey)
	end

	last_answer_in_chain = (question_node.answers.last || question_node)

	# Each instance of this question needs the answer hung from it
	self.survey.node_maps.select { |i|
		i.node == last_answer_in_chain
	}.each { |node_map|
		curr_children = self.survey.node_maps.select { |j|
			node_map.children.include?(j)
		}

		new_node_map = self.survey.node_maps.build(:node => self, :survey => self.survey)

		node_map.children << new_node_map

		curr_children.each { |c|
			new_node_map.children << c
		}
	}

	true
end

#move_downObject

Moves answer down relative to other answers by swapping parent and children



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/active_record_survey/node/answer/chained.rb', line 90

def move_down
	# Ensure each parent node to this node (the goal here is to hit a question node) is valid
	!self.survey.node_maps.select { |i|
		i.node == self
	}.collect { |node_map|
		# Must have children to move lower!
		# And the children are also answers!
		if node_map.children.length > 0 && !node_map.children.select { |j| j.node.class.ancestors.include?(::ActiveRecordSurvey::Node::Answer) }.empty?
			# I know this looks overly complicated, but we need to always work with the survey.node_maps - never children/parent of the relation
			parent_node = self.survey.node_maps.select { |j|
				node_map.parent == j
			}.first

			children = self.survey.node_maps.select { |j|
				node_map.children.include?(j)
			}

			children_children = self.survey.node_maps.select { |j|
				children.collect { |k| k.children }.flatten.include?(j)
			}

			children.each { |c|
				parent_node.children << c
			}

			children.each { |c|
				c.children << node_map
			}

			children_children.each { |i|
				node_map.children << i
			}
		end
	}
end

#move_upObject

Moves answer down relative to other answers by swapping parent and children



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/active_record_survey/node/answer/chained.rb', line 57

def move_up
	# Ensure each parent node to this node (the goal here is to hit a question node) is valid
	!self.survey.node_maps.select { |i|
		i.node == self
	}.collect { |node_map|
		# Parent must be an answer - cannot move into the position of a Question!
		if !node_map.parent.nil? && node_map.parent.node.class.ancestors.include?(::ActiveRecordSurvey::Node::Answer)
			# I know this looks overly complicated, but we need to always work with the survey.node_maps - never children/parent of the relation
			parent_node = self.survey.node_maps.select { |j|
				node_map.parent == j
			}.first

			parent_parent = self.survey.node_maps.select { |j|
				node_map.parent.parent == j
			}.first

			node_map.parent = parent_parent
			parent_parent.children << node_map

			self.survey.node_maps.select { |j|
				node_map.children.include?(j)
			}.each { |c|
				c.parent = parent_node
				parent_node.children << c
			}

			parent_node.parent = node_map
			node_map.children << parent_node
		end
	}
end

#sibling_indexObject

Gets index relative to other chained answers



11
12
13
14
15
16
17
18
19
# File 'lib/active_record_survey/node/answer/chained.rb', line 11

def sibling_index
	if node_map = self.survey.node_maps.select { |i|
		i.node == self
	}.first
		return node_map.ancestors_until_node_not_ancestor_of(::ActiveRecordSurvey::Node::Answer).length-1
	end

	return 0
end