Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Submission < ApplicationRecord
- has_many :answers, inverse_of: :submission, dependent: :destroy
- def self.to_csv(options = {}, questions)
- attributes =[I18n.t("app.survey.export.id"), I18n.t("app.survey.export.survey"),
- I18n.t("app.survey.export.submitted_by")] + questions.map { |s| s.title }
- CSV.generate(headers: true) do |csv|
- csv << attributes
- all.each do |submission|
- csv << [submission.id, submission.survey.name, submission.sender] + submission.show_answers(submission.answers)
- end
- end
- end
- def show_answers(answers)
- answers.map do |a|
- if a.answer_date.present?
- a.answer_date.response.strftime("%m/%d/%Y")
- elsif a.answer_open.present?
- a.answer_open.response
- elsif a.answer_image.present?
- a.answer_image.image.try(:name)
- elsif a.choice_answer.present?
- a.choice_answer.choice.try(:title)
- elsif a.answer_multiple.present?
- a.answer_multiple.choices.map(&:title).join(', ')
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement