Guest User

Untitled

a guest
Jul 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. namespace :db do
  2. desc "This loads questions and categories for a specific workshop"
  3. task :import_workshop_questionnaire => :environment do
  4. puts "Workshopname: #{ENV['WORKSHOPNAME']}"
  5. puts "File Path to CSV file #{ENV['CSVFILEPATH']}"
  6.  
  7. return if(ENV['WORKSHOPNAME'].blank? or ENV['CSVFILEPATH'].blank?)
  8.  
  9. class CSVParser
  10. def initialize(workshopname, filepath)
  11. @counter = 1
  12. @workshop = Workshop.find_by_name(ENV['WORKSHOPNAME'])
  13. if @workshop.blank?
  14. logger.debug "FATAL ERROR: Did not find a workshop"
  15. return
  16. end
  17. @questionnaire = @workshop.workshop_rka_questionnaires.build
  18. @questionnaire.name = "Teknisk faglige kompetencer"
  19. @questionnaire.save(false)
  20.  
  21. FasterCSV.foreach(filepath) do |row|
  22. case row[0]
  23. when "Kategorinavn:"
  24. add_category(row[1]) unless row[1].nil?
  25. when "Spørgemåde:"
  26. add_prefix_question(row[1]) unless row[1].nil?
  27. when "Spørgsmål:"
  28. add_question(row[1]) unless row[1].nil?
  29. when nil
  30. @questionnaire.save(false)
  31. end
  32. end
  33. puts "final save for #{@workshop.name} valid? #{@questionnaire.valid?}"
  34. puts "errors: #{@questionnaire.errors.full_messages}"
  35. @questionnaire.save(false)
  36. end
  37. def add_category(category_text)
  38. @current_category = @questionnaire.rka_categories.build(:topic => category_text)
  39. @current_category.position = @counter
  40. @counter += 1
  41. # raise "Could not save category with category_text: #{category_text} #{@current_category.errors}"
  42. #end
  43. end
  44. def add_prefix_question(prefix_text)
  45. @current_category.category_text = prefix_text
  46.  
  47. end
  48. def add_question(question_text)
  49. @question = @current_category.rka_questions.build(:question_text => question_text)
  50. # if !@question.save(false)
  51. # raise "Could not save question with question_text: #{question_text} #{@question.errors}"
  52. # end
  53. end
  54. end
  55. CSVParser.new(ENV['WORKSHOPNAME'], ENV['CSVFILEPATH'])
  56.  
  57.  
  58.  
  59. end
  60. end
Add Comment
Please, Sign In to add comment