Commit b109cfa5d616da72cc58c59cfa2715501580f4c3
1 parent
fbf51098
Exists in
master
and in
1 other branch
Modifying CSV data export format
Showing
2 changed files
with
12 additions
and
5 deletions
Show diff stats
app/controllers/questions_controller.rb
... | ... | @@ -89,14 +89,20 @@ class QuestionsController < InheritedResources::Base |
89 | 89 | @question = Question.find(params[:id]) |
90 | 90 | |
91 | 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 | 94 | @votes = @question.votes |
95 | 95 | csv_data = FasterCSV.generate do |csv| |
96 | 96 | csv << headers |
97 | 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 | 106 | end |
101 | 107 | end |
102 | 108 | |
... | ... | @@ -116,7 +122,7 @@ class QuestionsController < InheritedResources::Base |
116 | 122 | csv_data = FasterCSV.generate do |csv| |
117 | 123 | csv << headers |
118 | 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 | 126 | c.wins, c.losses, c.created_at, c.updated_at, c.active, c.score, c.local_identifier, |
121 | 127 | c.prompts_on_the_left(true).size, c.prompts_on_the_right(true).size, c.prompts_count] |
122 | 128 | end | ... | ... |
app/models/vote.rb
... | ... | @@ -4,4 +4,5 @@ class Vote < ActiveRecord::Base |
4 | 4 | belongs_to :question, :counter_cache => true |
5 | 5 | belongs_to :prompt, :counter_cache => true |
6 | 6 | belongs_to :choice, :counter_cache => true |
7 | + belongs_to :loser_choice, :class_name => "Choice", :foreign_key => "loser_choice_id" | |
7 | 8 | end | ... | ... |