Skip to content

Instantly share code, notes, and snippets.

@cmorss
Created July 14, 2016 16:40
Show Gist options
  • Save cmorss/184bf4620ba8270f1374b02da1de1cce to your computer and use it in GitHub Desktop.
Save cmorss/184bf4620ba8270f1374b02da1de1cce to your computer and use it in GitHub Desktop.
Create answer summaries on survey responses
Mongoid.default_session["survey_responses"].find.each_with_index do |response, index|
summary = {}
other_text_summary = {}
(response["answers"] || []).each do |answer|
if answer["_type"] == "Survey::Answers::Multiselect"
choices = (answer["selections"] || []).map { |s| s && s["answer_choice_id"] && s["answer_choice_id"].to_s }
summary[answer["question_id"].to_s] = choices if choices.present?
other = (answer["selections"] || []).detect { |s| s && s["text"].present? }
other_text_summary[answer["question_id"].to_s] = other["text"] if other
elsif answer["_type"] == "Survey::Answers::SingleSelect"
choice = (answer["selections"] || []).first
summary[answer["question_id"].to_s] = choice["answer_choice_id"] if choice && choice["answer_choice_id"].present?
other = (answer["selections"] || []).detect { |s| s && s["text"].present? }
other_text_summary[answer["question_id"].to_s] = other["text"] if other
elsif answer["_type"] = "Survey::Answers::Range" || answer["_type"] = "Survey::Answers::NPS"
summary[answer["question_id"].to_s] = answer["value"] if answer["value"].present?
elsif answer["_type"] == "Survey::Answers::FreeForm"
summary[answer["question_id"].to_s] = answer["text"] if answer["text"].present?
else
puts "Unknown answer type: #{answer["_type"]}"
puts "response: #{response}"
break
end
end
attrs = { summary: summary, other_text_summary: other_text_summary }
Mongoid.default_session["survey_responses"].find(_id: response["_id"]).update(:$set => attrs)
putc "." if index % 1_000 == 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment