Skip to content

Instantly share code, notes, and snippets.

@bjensen
Created February 10, 2011 14:57
Show Gist options
  • Select an option

  • Save bjensen/820650 to your computer and use it in GitHub Desktop.

Select an option

Save bjensen/820650 to your computer and use it in GitHub Desktop.
namespace :db do
desc "This loads questions and categories for a specific workshop"
task :import_workshop_questionnaire => :environment do
puts "Workshopname: #{ENV['WORKSHOPNAME']}"
puts "File Path to CSV file #{ENV['CSVFILEPATH']}"
return if(ENV['WORKSHOPNAME'].blank? or ENV['CSVFILEPATH'].blank?)
class CSVParser
def initialize(workshopname, filepath)
@counter = 1
@workshop = Workshop.find_by_name(ENV['WORKSHOPNAME'])
if @workshop.blank?
logger.debug "FATAL ERROR: Did not find a workshop"
return
end
@questionnaire = @workshop.workshop_rka_questionnaires.build
@questionnaire.name = "Teknisk faglige kompetencer"
@questionnaire.save(false)
FasterCSV.foreach(filepath) do |row|
case row[0]
when "Kategorinavn:"
add_category(row[1]) unless row[1].nil?
when "Spørgemåde:"
add_prefix_question(row[1]) unless row[1].nil?
when "Spørgsmål:"
add_question(row[1]) unless row[1].nil?
when nil
@questionnaire.save(false)
end
end
puts "final save for #{@workshop.name} valid? #{@questionnaire.valid?}"
puts "errors: #{@questionnaire.errors.full_messages}"
@questionnaire.save(false)
end
def add_category(category_text)
@current_category = @questionnaire.rka_categories.build(:topic => category_text)
@current_category.position = @counter
@counter += 1
# raise "Could not save category with category_text: #{category_text} #{@current_category.errors}"
#end
end
def add_prefix_question(prefix_text)
@current_category.category_text = prefix_text
end
def add_question(question_text)
@question = @current_category.rka_questions.build(:question_text => question_text)
# if !@question.save(false)
# raise "Could not save question with question_text: #{question_text} #{@question.errors}"
# end
end
end
CSVParser.new(ENV['WORKSHOPNAME'], ENV['CSVFILEPATH'])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment