Class: Missinglink::SurveyQuestion
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Missinglink::SurveyQuestion
- Defined in:
- app/models/missinglink/survey_question.rb
Class Method Summary collapse
Instance Method Summary collapse
- #answer_strategy ⇒ Object
-
#possible_responses(search_other = false) ⇒ Object
for reference, when searching, listing all possible responses is logical, but it is impossible to track all survey response answers that match the desired answer.
- #similar_response_answers(response_answer, search_other = false) ⇒ Object
Class Method Details
.first_or_create_by_sm_id(sm_id) ⇒ Object
28 29 30 31 32 33 34 |
# File 'app/models/missinglink/survey_question.rb', line 28 def self.first_or_create_by_sm_id(sm_id) if question = SurveyQuestion.find_by_sm_question_id(sm_id) return question else return SurveyQuestion.create(sm_question_id: sm_id) end end |
.parse(page, hash) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'app/models/missinglink/survey_question.rb', line 11 def self.parse(page, hash) question = SurveyQuestion.first_or_create_by_sm_id(hash['question_id']) question.update_attributes({ heading: hash['heading'], position: hash['position'].to_i, type_family: hash['type']['family'], type_subtype: hash['type']['subtype'] }) question.survey_pages = [page] question.save hash['answers'].each do |answer| SurveyAnswer.parse(question, answer) end return question.reload end |
Instance Method Details
#answer_strategy ⇒ Object
94 95 96 |
# File 'app/models/missinglink/survey_question.rb', line 94 def answer_strategy Missinglink.answer_strategies[type_family][type_subtype] end |
#possible_responses(search_other = false) ⇒ Object
for reference, when searching, listing all possible responses is logical, but it is impossible to track all survey response answers that match the desired answer. therefore, we only track one example, and later find all similar response answers based on the question strategy
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 |
# File 'app/models/missinglink/survey_question.rb', line 41 def possible_responses(search_other = false) {}.tap do |hash| survey_response_answers.each do |sra| sa_row = (sra.row_survey_answer_id ? SurveyAnswer.find(sra.row_survey_answer_id) : nil) sa_col = (sra.col_survey_answer_id ? SurveyAnswer.find(sra.col_survey_answer_id) : nil) sa_col_choice = (sra.col_choice_survey_answer_id ? SurveyAnswer.find(sra.col_choice_survey_answer_id) : nil) case answer_strategy when "first_survey_response_answer_text" hash[sra.text] = sra.id unless (sra.text.nil? || hash[sra.text]) when "answer_row_match_for_survey_response_answer_text" other_text = ((!search_other || sra.text.nil?) ? nil : "#{ (sa_row.try(:text) || "Other") }: #{ sra.text }") hash[sa_row.text] = sra.id unless (sa_row.nil? || hash[sa_row.text]) hash[other_text] = sra.id unless (other_text.nil? || hash[other_text]) when "row_column_survey_response_answers_and_text" main_text = "#{ sa_row.try(:text) }: #{ sa_col.try(:text) }" other_text = ((!search_other || sra.text.nil? || !sa_row.nil?) ? nil : "Other: #{ sra.text }") hash[main_text] = sra.id unless (sa_row.nil? || sa_col.nil? || hash[main_text]) hash[other_text] = sra.id unless (other_text.nil? || hash[other_text]) when "row_column_and_choice_survey_response_answers_and_text" main_text = "#{ sa_row.try(:text) }, #{ sa_col.try(:text) }: #{ sa_col_choice.try(:text) }" other_text = ((!search_other || sra.text.nil? || !sa_row.nil?) ? nil : "Other: #{ sra.text }") hash[main_text] = sra.id unless (sa_row.nil? || sa_col.nil? || sa_col_choice.nil? || hash[main_text]) hash[other_text] = sra.id unless (other_text.nil? || hash[other_text]) end end end end |
#similar_response_answers(response_answer, search_other = false) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/models/missinglink/survey_question.rb', line 70 def similar_response_answers(response_answer, search_other = false) if search_other || answer_strategy == "first_survey_response_answer_text" return survey_response_answers.select { |sra| sra.text == response_answer.text }.sort end case answer_strategy when "answer_row_match_for_survey_response_answer_text" survey_response_answers.select do |sra| sra.row_survey_answer_id == response_answer.row_survey_answer_id end.sort when "row_column_survey_response_answers_and_text" survey_response_answers.select do |sra| sra.row_survey_answer_id == response_answer.row_survey_answer_id && sra.col_survey_answer_id == response_answer.col_survey_answer_id end.sort when "row_column_and_choice_survey_response_answers_and_text" survey_response_answers.select do |sra| sra.row_survey_answer_id == response_answer.row_survey_answer_id && sra.col_survey_answer_id == response_answer.col_survey_answer_id && sra.col_choice_survey_answer_id == response_answer.col_choice_survey_answer_id end.sort end end |