Module: ActiveRecordSurvey::Answer::Chained

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



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_record_survey/node/answer/chained.rb', line 5

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|
    node_map.children << self.survey.node_maps.build(:node => self, :survey => self.survey)
  }

  true
end