Module: ActiveRecordSurvey::NodeMapGroup::InstanceMethods
- Defined in:
- lib/active_record_survey/node_map_group/instance_methods.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#build_question(question, previous_answer = nil) ⇒ Boolean
Adds a question to the page.
-
#build_questions(questions) ⇒ Object
Builds questions.
Class Method Details
.included(base) ⇒ Object
4 5 6 |
# File 'lib/active_record_survey/node_map_group/instance_methods.rb', line 4 def self.included(base) base.table_name = :active_record_survey_api_node_map_groups end |
Instance Method Details
#build_question(question, previous_answer = nil) ⇒ Boolean
Adds a question to the page
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/active_record_survey/node_map_group/instance_methods.rb', line 12 def build_question(question, previous_answer = nil) results = self.survey.node_maps.select { |i| i.node === question && ((previous_answer.nil?)? true : i.parent && i.parent.node == previous_answer) }.collect { |question_node| if self.send(:node_map_valid?, question_node) self.node_maps << question_node true else false end } # At least one node_map was valid (results.length > 0 && !results.include?(false)) end |
#build_questions(questions) ⇒ Object
Builds questions
From an array of questions (and question/answer key value pairs) builds all the node_map_groups to be associated with this node_map_group
-
Args :
-
array-> The array of questions
-
-
Returns :
-
boolean-> Whether successul or not
-
-
Raises :
-
StandardError-> Survey has not yet been associated -
ArgumentError-> Question or Answers do not exist
-
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/active_record_survey/node_map_group/instance_methods.rb', line 40 def build_questions(questions) # Must be associated with a survey first raise StandardError, "SURVEY_MISSING" if self.survey.nil? questions.each { |datum| # Find question by inputs question = case datum.class.to_s when 'Fixnum' then ActiveRecordSurvey::Node::Question.where(:id => datum, :survey => self.survey).first when 'Hash' then ActiveRecordSurvey::Node::Question.where(:id => datum[:question_id], :survey => self.survey).first else nil end # Don't continue if question does not exist raise ArgumentError, "INVALID_QUESTION" if question.nil? # Find answer by inputs previous_node = case datum.class.to_s when 'Hash' then ActiveRecordSurvey::Node.where(:id => datum[:previous_id], :survey => self.survey).first else nil end # Via Survey object, find all the node_maps that match the criteria passed question_node_maps = self.survey.node_maps.select { |node_map| node_map.node == question } # Filter list by the previous node question_node_maps.select! { |node_map| previous_node.node_maps.select { |i| i.children.include?(node_map) }.length > 0 } unless previous_node.nil? # Filter by existing node maps filtered = question_node_maps.select { |new_node_map| # Only keep new node_maps if there is an existing node_map that is an ancestor of it self.node_maps.select { |existing_node_map| # This is the only path we want - throw out the others new_node_map.is_decendant_of?(existing_node_map) }.length > 0 } # At least one existing node_map was an ancestor of the one we're adding question_node_maps = filtered if filtered.length != 0 # Now prune existing node maps # We are removing node_maps that are apart of paths that don't logically make sense anymore self.node_maps = self.node_maps.select { |existing_node_map| keep = true question_node_maps.select { |new_node_map| if existing_node_map.node != new_node_map.node && !existing_node_map.is_decendant_of?(new_node_map) && ! new_node_map.is_decendant_of?(existing_node_map) existing_node_map.node_map_group = nil keep = false end } keep } # After our investigation - no nodes found! raise ArgumentError, "QUESTION_NOT_FOUND" if question_node_maps.length === 0 # Add the remaining nodes question_node_maps.each { |node_map| self.node_maps << node_map } } true end |