Class: ActiveRecordSurvey::Node::Question
- Inherits:
-
ActiveRecordSurvey::Node
- Object
- ActiveRecord::Base
- ActiveRecordSurvey::Node
- ActiveRecordSurvey::Node::Question
- Defined in:
- lib/active_record_survey/node/question.rb
Instance Method Summary collapse
-
#build_answer(answer_node) ⇒ Object
Build an answer off this node.
-
#next_questions ⇒ Object
Returns the questions that follows this question (either directly or via its answers).
-
#validate_parent_instance_node(instance_node, child_node) ⇒ Object
Stop validating at the Question node.
Methods inherited from ActiveRecordSurvey::Node
#answers, #build_link, #has_instance_node_for_instance?, #instance_node_for_instance, #instance_node_path_to_root?, #is_answered_for_instance?, #remove_link, #validate_instance_node
Instance Method Details
#build_answer(answer_node) ⇒ Object
Build an answer off this node
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/active_record_survey/node/question.rb', line 11 def build_answer(answer_node) # A survey must either be passed or already present in self.node_maps if self.survey.nil? raise ArgumentError.new "A survey must be passed if ActiveRecordSurvey::Node::Question is not yet added to a survey" end # Cannot mix answer types # Check if not match existing - throw error if !self.answers.select { |answer| answer.class != answer_node.class }.empty? raise ArgumentError.new "Cannot mix answer types on question" end # Answers actually define how they're built off the parent node answer_node.send(:build_answer, self) end |
#next_questions ⇒ Object
Returns the questions that follows this question (either directly or via its answers)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/active_record_survey/node/question.rb', line 30 def next_questions list = [] if question_node_map = self.survey.node_maps.select { |i| i.node == self && !i.marked_for_destruction? }.first question_node_map.children.each { |child| if !child.node.nil? && !child.marked_for_destruction? if child.node.class.ancestors.include?(::ActiveRecordSurvey::Node::Question) list << child.node elsif child.node.class.ancestors.include?(::ActiveRecordSurvey::Node::Answer) list << child.node.next_question end end } end list.compact.uniq end |
#validate_parent_instance_node(instance_node, child_node) ⇒ Object
Stop validating at the Question node
4 5 6 7 8 |
# File 'lib/active_record_survey/node/question.rb', line 4 def validate_parent_instance_node(instance_node, child_node) !self.node_validations.collect { |node_validation| node_validation.validate_instance_node(instance_node, self) }.include?(false) end |