From 44f3264c2e3021d2abaebadaf1d92769ef5e60f7 Mon Sep 17 00:00:00 2001 From: Luke Baker Date: Thu, 25 Apr 2013 09:23:13 -0400 Subject: [PATCH] use FasterCSV gem only for ruby 1.8 --- Gemfile | 2 +- app/models/question.rb | 2 +- lib/csv_bridge.rb | 10 ++++++++++ lib/tasks/import_bt_test_data.rb | 6 +++--- spec/models/question_spec.rb | 10 +++++----- 5 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 lib/csv_bridge.rb diff --git a/Gemfile b/Gemfile index becc37c..d182954 100644 --- a/Gemfile +++ b/Gemfile @@ -19,7 +19,7 @@ gem "has_scope", "0.4.2" gem "responders", "0.4.8" gem "thoughtbot-clearance", "0.8.2", :require => "clearance" -gem "fastercsv", "1.5.1" +gem "fastercsv", "1.5.1", :platforms => :ruby_18 gem "delayed_job", "2.0.6" gem "redis", "~> 3.0.1" diff --git a/app/models/question.rb b/app/models/question.rb index 024b8d7..51edc5b 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -580,7 +580,7 @@ class Question < ActiveRecord::Base raise "Unsupported export type: #{type}" end - csv_data = FasterCSV.generate do |csv| + csv_data = CSVBridge.generate do |csv| csv << headers case type when 'votes' diff --git a/lib/csv_bridge.rb b/lib/csv_bridge.rb new file mode 100644 index 0000000..6f56c8e --- /dev/null +++ b/lib/csv_bridge.rb @@ -0,0 +1,10 @@ +require 'csv' +require 'fastercsv' + +if CSV.const_defined?(:Reader) + class CSVBridge < FasterCSV + end +else + class CSVBridge < CSV + end +end diff --git a/lib/tasks/import_bt_test_data.rb b/lib/tasks/import_bt_test_data.rb index 7775f54..0fa0bd4 100644 --- a/lib/tasks/import_bt_test_data.rb +++ b/lib/tasks/import_bt_test_data.rb @@ -11,7 +11,7 @@ inserts = [] timestring = Time.now.to_s(:db) #isn't rails awesome? totalchoices=0 -FasterCSV.foreach(BASEDIR + "choices_7000.txt", {:headers => :first_row, :return_headers => false}) do |choice| +CSVBridge.foreach(BASEDIR + "choices_7000.txt", {:headers => :first_row, :return_headers => false}) do |choice| # for each choice, create an insert with unique id id = choice[0].to_i + choice_offset wins = choice[1].to_i @@ -26,7 +26,7 @@ ActiveRecord::Base.connection.execute(sql) inserts = [] prompt_offset = Prompt.last.id totalprompts = 0 -FasterCSV.foreach(BASEDIR + "prompts_7000.txt", {:headers => :first_row, :return_headers => false}) do |prompt| +CSVBridge.foreach(BASEDIR + "prompts_7000.txt", {:headers => :first_row, :return_headers => false}) do |prompt| id = prompt[0].to_i + prompt_offset left_choice_id = prompt[1].to_i + choice_offset right_choice_id = prompt[2].to_i + choice_offset @@ -43,7 +43,7 @@ ActiveRecord::Base.connection.execute(sql) inserts = [] vote_offset = Vote.last.id totalvotes=0 -FasterCSV.foreach(BASEDIR + "votes_7000.txt", {:headers => :first_row, :return_headers => false}) do |vote| +CSVBridge.foreach(BASEDIR + "votes_7000.txt", {:headers => :first_row, :return_headers => false}) do |vote| id = vote[0].to_i + vote_offset prompt_id = vote[1].to_i + prompt_offset choice_id = vote[2].to_i + choice_offset diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index ff5507a..4446f95 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -539,7 +539,7 @@ describe Question do # Not specifying exact file syntax, it's likely to change frequently # - rows = FasterCSV.parse(csv) + rows = CSVBridge.parse(csv) rows.first.should include("Vote ID") rows.first.should_not include("Idea ID") @@ -567,7 +567,7 @@ describe Question do it "should export non vote data to a string" do csv = @aoi_question.export('non_votes') - rows = FasterCSV.parse(csv) + rows = CSVBridge.parse(csv) rows.first.should include("Record ID") rows.first.should include("Record Type") rows.first.should_not include("Idea ID") @@ -580,7 +580,7 @@ describe Question do # Not specifying exact file syntax, it's likely to change frequently # - rows = FasterCSV.parse(csv) + rows = CSVBridge.parse(csv) rows.first.should include("Idea ID") rows.first.should_not include("Skip ID") end @@ -648,7 +648,7 @@ describe Question do # Not specifying exact file syntax, it's likely to change frequently # - rows = FasterCSV.parse(csv) + rows = CSVBridge.parse(csv) rows.first.should include("Idea ID") rows.first.should_not include("Skip ID") @@ -664,7 +664,7 @@ describe Question do # Not specifying exact file syntax, it's likely to change frequently # - rows = FasterCSV.parse(csv) + rows = CSVBridge.parse(csv) rows.first.should include("Vote ID") rows.first.should_not include("Idea ID") -- libgit2 0.21.2