Commit b109cfa5d616da72cc58c59cfa2715501580f4c3

Authored by Dhruv Kapadia
1 parent fbf51098

Modifying CSV data export format

app/controllers/questions_controller.rb
@@ -89,14 +89,20 @@ class QuestionsController < InheritedResources::Base @@ -89,14 +89,20 @@ class QuestionsController < InheritedResources::Base
89 @question = Question.find(params[:id]) 89 @question = Question.find(params[:id])
90 90
91 outfile = "question_#{@question.id}_votes" + Time.now.strftime("%m-%d-%Y") + ".csv" 91 outfile = "question_#{@question.id}_votes" + Time.now.strftime("%m-%d-%Y") + ".csv"
92 - headers = ['Vote ID', 'Voter ID', 'Question ID','Choice Voted on ID', 'Choice Voted on Data', 'Loser Choice ID',  
93 - 'Prompt ID', 'Created at', 'Updated at'] 92 + headers = ['Vote ID', 'Voter ID', 'Question ID','Winner ID', 'Winner Text', 'Loser ID', 'Loser Text',
  93 + 'Prompt ID', 'Left Choice ID', 'Right Choice ID', 'Created at', 'Updated at']
94 @votes = @question.votes 94 @votes = @question.votes
95 csv_data = FasterCSV.generate do |csv| 95 csv_data = FasterCSV.generate do |csv|
96 csv << headers 96 csv << headers
97 @votes.each do |v| 97 @votes.each do |v|
98 - csv << [ v.id, v.voter_id, v.question_id, v.choice_id, v.choice.data, v.loser_choice_id,  
99 - v.prompt_id, v.created_at, v.updated_at] 98 + prompt = v.prompt
  99 + # these may not exist
  100 + loser_data = v.loser_choice.nil? ? "" : "'#{v.loser_choice.data.strip}'"
  101 + left_id = v.prompt.nil? ? "" : v.prompt.left_choice_id
  102 + right_id = v.prompt.nil? ? "" : v.prompt.right_choice_id
  103 +
  104 + csv << [ v.id, v.voter_id, v.question_id, v.choice_id, "\'#{v.choice.data.strip}'", v.loser_choice_id, loser_data,
  105 + v.prompt_id, left_id, right_id, v.created_at, v.updated_at]
100 end 106 end
101 end 107 end
102 108
@@ -116,7 +122,7 @@ class QuestionsController &lt; InheritedResources::Base @@ -116,7 +122,7 @@ class QuestionsController &lt; InheritedResources::Base
116 csv_data = FasterCSV.generate do |csv| 122 csv_data = FasterCSV.generate do |csv|
117 csv << headers 123 csv << headers
118 @question.choices.each do |c| 124 @question.choices.each do |c|
119 - csv << [ c.id, c.item_id, c.data, c.question_id, c.item.creator != @question.creator, c.item.creator_id, 125 + csv << [ c.id, c.item_id, "'#{c.data.strip}'", c.question_id, c.item.creator != @question.creator, c.item.creator_id,
120 c.wins, c.losses, c.created_at, c.updated_at, c.active, c.score, c.local_identifier, 126 c.wins, c.losses, c.created_at, c.updated_at, c.active, c.score, c.local_identifier,
121 c.prompts_on_the_left(true).size, c.prompts_on_the_right(true).size, c.prompts_count] 127 c.prompts_on_the_left(true).size, c.prompts_on_the_right(true).size, c.prompts_count]
122 end 128 end
app/models/vote.rb
@@ -4,4 +4,5 @@ class Vote &lt; ActiveRecord::Base @@ -4,4 +4,5 @@ class Vote &lt; ActiveRecord::Base
4 belongs_to :question, :counter_cache => true 4 belongs_to :question, :counter_cache => true
5 belongs_to :prompt, :counter_cache => true 5 belongs_to :prompt, :counter_cache => true
6 belongs_to :choice, :counter_cache => true 6 belongs_to :choice, :counter_cache => true
  7 + belongs_to :loser_choice, :class_name => "Choice", :foreign_key => "loser_choice_id"
7 end 8 end