Class: Missinglink::SurveyRespondentDetail

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/missinglink/survey_respondent_detail.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.first_or_create_by_survey_details(survey_id, sm_id) ⇒ Object



20
21
22
23
24
25
26
27
# File 'app/models/missinglink/survey_respondent_detail.rb', line 20

def self.first_or_create_by_survey_details(survey_id, sm_id)
  # spelling this one out more explicitly so it's easier to test
  if new_srd = SurveyRespondentDetail.find_by_survey_id_and_sm_respondent_id(survey_id, sm_id.to_i)
    return new_srd
  else
    return SurveyRespondentDetail.create(survey_id: survey_id, sm_respondent_id: sm_id.to_i)
  end
end

.parse(survey, hash) ⇒ Object



11
12
13
14
15
16
17
18
# File 'app/models/missinglink/survey_respondent_detail.rb', line 11

def self.parse(survey, hash)
  srd = SurveyRespondentDetail.first_or_create_by_survey_details(survey.id, hash['respondent_id'])

  attrs = prepare_respondent_details(hash)
  srd.update_attributes(attrs)

  return srd
end

Instance Method Details

#response_to_question(question) ⇒ Object



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
55
56
57
58
59
60
61
62
63
64
# File 'app/models/missinglink/survey_respondent_detail.rb', line 29

def response_to_question(question)
  return nil unless response = survey_responses.find_by_survey_question_id(question.id)
  response_texts = []

  case question.answer_strategy
  when "first_survey_response_answer_text"
    return response.survey_response_answers.first.text
  when "answer_row_for_response"
    response.survey_response_answers.each do |sra|
      if sra.text.nil?
        response_texts << SurveyAnswer.find(sra.row_survey_answer_id).text
      else
        response_texts << (SurveyAnswer.find(sra.row_survey_answer_id).text + ": " + sra.text)
      end
    end
  when "answer_row_and_column_for_response"
    response.survey_response_answers.each do |sra|
      if sra.text.nil?
        response_texts << (SurveyAnswer.find(sra.row_survey_answer_id).text + ": " + SurveyAnswer.find(sra.col_survey_answer_id).text)
      else
        response_texts << "Other: #{ sra.text }"
      end
    end
  when "answer_row_column_choice_for_response"
    response.survey_response_answers.each do |sra|
      if sra.text.nil?
        response_texts << (SurveyAnswer.find(sra.row_survey_answer_id).text + ", " +
                           SurveyAnswer.find(sra.col_survey_answer_id).text + ": " +
                           SurveyAnswer.find(sra.col_choice_survey_answer_id).text)
      else
        response_texts << "Other: #{ sra.text }"
      end
    end
  end
  return response_texts.join("; ")
end