diff --git a/app/models/question.rb b/app/models/question.rb index 6ac6b79..8fc0548 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -533,13 +533,13 @@ class Question < ActiveRecord::Base self.votes.find_each(:include => [:prompt, :choice, :loser_choice, :voter]) do |v| prompt = v.prompt # these may not exist - loser_data = v.loser_choice.nil? ? "" : "'#{v.loser_choice.data.strip}'" + loser_data = v.loser_choice.nil? ? "" : v.loser_choice.data.strip left_id = v.prompt.nil? ? "" : v.prompt.left_choice_id right_id = v.prompt.nil? ? "" : v.prompt.right_choice_id time_viewed = v.time_viewed.nil? ? "NA": v.time_viewed.to_f / 1000.0 - csv << [ v.id, v.voter_id, v.question_id, v.choice_id, "'#{v.choice.data.strip}'", v.loser_choice_id, loser_data, + csv << [ v.id, v.voter_id, v.question_id, v.choice_id, v.choice.data.strip, v.loser_choice_id, loser_data, v.prompt_id, left_id, right_id, v.created_at, v.updated_at, v.appearance_id, time_viewed, v.missing_response_time_exp , v.voter.identifier] end diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index d5e24ac..33da502 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -487,6 +487,7 @@ describe Question do rows.shift rows.each do |row| + # Idea Text row[2].should =~ /^foo.bar$/m end @@ -494,6 +495,29 @@ describe Question do end + it "should export vote data to a csv file with proper escaping" do + filename = @aoi_question.export('votes') + + filename.should_not be nil + filename.should match /.*ideamarketplace_#{@aoi_question.id}_votes[.]csv$/ + File.exists?(filename).should be_true + # Not specifying exact file syntax, it's likely to change frequently + # + rows = FasterCSV.read(filename) + rows.first.should include("Vote ID") + rows.first.should_not include("Idea ID") + + rows.shift + rows.each do |row| + # Winner Text + row[4].should =~ /^foo.bar$/m + # Loser Text + row[6].should =~ /^foo.bar$/m + end + File.delete(filename).should be_true + + end + after(:all) { truncate_all } end -- libgit2 0.21.2