Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. class Submission < ApplicationRecord
  2. has_many :answers, inverse_of: :submission, dependent: :destroy
  3.  
  4. def self.to_csv(options = {}, questions)
  5. attributes =[I18n.t("app.survey.export.id"), I18n.t("app.survey.export.survey"),
  6. I18n.t("app.survey.export.submitted_by")] + questions.map { |s| s.title }
  7.  
  8. CSV.generate(headers: true) do |csv|
  9. csv << attributes
  10. all.each do |submission|
  11. csv << [submission.id, submission.survey.name, submission.sender] + submission.show_answers(submission.answers)
  12. end
  13. end
  14. end
  15.  
  16. def show_answers(answers)
  17. answers.map do |a|
  18. if a.answer_date.present?
  19. a.answer_date.response.strftime("%m/%d/%Y")
  20. elsif a.answer_open.present?
  21. a.answer_open.response
  22. elsif a.answer_image.present?
  23. a.answer_image.image.try(:name)
  24. elsif a.choice_answer.present?
  25. a.choice_answer.choice.try(:title)
  26. elsif a.answer_multiple.present?
  27. a.answer_multiple.choices.map(&:title).join(', ')
  28. end
  29. end
  30. end
  31. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement