diff --git a/app/controllers/custom/admin/poll/results_controller.rb b/app/controllers/custom/admin/poll/results_controller.rb new file mode 100644 index 000000000000..3b94be75a044 --- /dev/null +++ b/app/controllers/custom/admin/poll/results_controller.rb @@ -0,0 +1,24 @@ +class Admin::Poll::ResultsController < Admin::Poll::BaseController + before_action :load_poll + + def index + questions = @poll.questions + answers = Poll::Answer.where(question: questions) + + respond_to do |format| + format.csv do + send_data Poll::Exporter.new(answers).to_csv, + filename: "answers.csv" + end + format.html + end + + @partial_results = @poll.partial_results + end + + private + + def load_poll + @poll = ::Poll.includes(:questions).find(params[:poll_id]) + end +end diff --git a/app/models/custom/poll/exporter.rb b/app/models/custom/poll/exporter.rb new file mode 100644 index 000000000000..c928ab7de575 --- /dev/null +++ b/app/models/custom/poll/exporter.rb @@ -0,0 +1,35 @@ +class Poll::Exporter + require "csv" + + def initialize(poll_answers = []) + @answers = poll_answers + end + + def to_csv + CSV.generate(headers: true) do |csv| + csv << headers + @answers.each { |answer| csv << csv_values(answer) } + end + end + + private + + def headers + [ + I18n.t("admin.polls.results.export_list.id"), + I18n.t("admin.polls.results.export_list.question_id"), + I18n.t("admin.polls.results.export_list.answer"), + I18n.t("admin.polls.results.export_list.author_id") + ] + end + + def csv_values(answer) + [ + answer.id.to_s, + answer.question_id.to_s, + answer.answer, + answer.author_id.to_s + ] + end + end + \ No newline at end of file diff --git a/app/views/custom/admin/poll/results/index.html.erb b/app/views/custom/admin/poll/results/index.html.erb new file mode 100644 index 000000000000..1716e19cb9e7 --- /dev/null +++ b/app/views/custom/admin/poll/results/index.html.erb @@ -0,0 +1,29 @@ +<%= render "/admin/poll/polls/poll_header" %> + +