Commit bc10fdca456deadd5321df1479e6c8644cd6f4e6
1 parent
cd53dbe2
Exists in
master
and in
1 other branch
have FasterCSV do proper CSV escaping
In the votes CSV file, let FasterCSV do proper escaping for characters in strings like commas and quotes
Showing
2 changed files
with
26 additions
and
2 deletions
Show diff stats
app/models/question.rb
| @@ -533,13 +533,13 @@ class Question < ActiveRecord::Base | @@ -533,13 +533,13 @@ class Question < ActiveRecord::Base | ||
| 533 | self.votes.find_each(:include => [:prompt, :choice, :loser_choice, :voter]) do |v| | 533 | self.votes.find_each(:include => [:prompt, :choice, :loser_choice, :voter]) do |v| |
| 534 | prompt = v.prompt | 534 | prompt = v.prompt |
| 535 | # these may not exist | 535 | # these may not exist |
| 536 | - loser_data = v.loser_choice.nil? ? "" : "'#{v.loser_choice.data.strip}'" | 536 | + loser_data = v.loser_choice.nil? ? "" : v.loser_choice.data.strip |
| 537 | left_id = v.prompt.nil? ? "" : v.prompt.left_choice_id | 537 | left_id = v.prompt.nil? ? "" : v.prompt.left_choice_id |
| 538 | right_id = v.prompt.nil? ? "" : v.prompt.right_choice_id | 538 | right_id = v.prompt.nil? ? "" : v.prompt.right_choice_id |
| 539 | 539 | ||
| 540 | time_viewed = v.time_viewed.nil? ? "NA": v.time_viewed.to_f / 1000.0 | 540 | time_viewed = v.time_viewed.nil? ? "NA": v.time_viewed.to_f / 1000.0 |
| 541 | 541 | ||
| 542 | - csv << [ v.id, v.voter_id, v.question_id, v.choice_id, "'#{v.choice.data.strip}'", v.loser_choice_id, loser_data, | 542 | + csv << [ v.id, v.voter_id, v.question_id, v.choice_id, v.choice.data.strip, v.loser_choice_id, loser_data, |
| 543 | v.prompt_id, left_id, right_id, v.created_at, v.updated_at, v.appearance_id, | 543 | v.prompt_id, left_id, right_id, v.created_at, v.updated_at, v.appearance_id, |
| 544 | time_viewed, v.missing_response_time_exp , v.voter.identifier] | 544 | time_viewed, v.missing_response_time_exp , v.voter.identifier] |
| 545 | end | 545 | end |
spec/models/question_spec.rb
| @@ -487,6 +487,7 @@ describe Question do | @@ -487,6 +487,7 @@ describe Question do | ||
| 487 | 487 | ||
| 488 | rows.shift | 488 | rows.shift |
| 489 | rows.each do |row| | 489 | rows.each do |row| |
| 490 | + # Idea Text | ||
| 490 | row[2].should =~ /^foo.bar$/m | 491 | row[2].should =~ /^foo.bar$/m |
| 491 | end | 492 | end |
| 492 | 493 | ||
| @@ -494,6 +495,29 @@ describe Question do | @@ -494,6 +495,29 @@ describe Question do | ||
| 494 | 495 | ||
| 495 | end | 496 | end |
| 496 | 497 | ||
| 498 | + it "should export vote data to a csv file with proper escaping" do | ||
| 499 | + filename = @aoi_question.export('votes') | ||
| 500 | + | ||
| 501 | + filename.should_not be nil | ||
| 502 | + filename.should match /.*ideamarketplace_#{@aoi_question.id}_votes[.]csv$/ | ||
| 503 | + File.exists?(filename).should be_true | ||
| 504 | + # Not specifying exact file syntax, it's likely to change frequently | ||
| 505 | + # | ||
| 506 | + rows = FasterCSV.read(filename) | ||
| 507 | + rows.first.should include("Vote ID") | ||
| 508 | + rows.first.should_not include("Idea ID") | ||
| 509 | + | ||
| 510 | + rows.shift | ||
| 511 | + rows.each do |row| | ||
| 512 | + # Winner Text | ||
| 513 | + row[4].should =~ /^foo.bar$/m | ||
| 514 | + # Loser Text | ||
| 515 | + row[6].should =~ /^foo.bar$/m | ||
| 516 | + end | ||
| 517 | + File.delete(filename).should be_true | ||
| 518 | + | ||
| 519 | + end | ||
| 520 | + | ||
| 497 | after(:all) { truncate_all } | 521 | after(:all) { truncate_all } |
| 498 | end | 522 | end |
| 499 | 523 |