From adce315a3e664a2ccb5e02badecc2776a507ed6a Mon Sep 17 00:00:00 2001 From: Diego Calvo Castillo Date: Thu, 26 Oct 2023 12:56:54 +0200 Subject: [PATCH] Allow exporting poll results --- .../custom/admin/poll/results_controller.rb | 24 +++++++++++++ app/models/custom/poll/exporter.rb | 35 +++++++++++++++++++ .../custom/admin/poll/results/index.html.erb | 29 +++++++++++++++ config/locales/custom/es/admin.yml | 6 ++++ 4 files changed, 94 insertions(+) create mode 100644 app/controllers/custom/admin/poll/results_controller.rb create mode 100644 app/models/custom/poll/exporter.rb create mode 100644 app/views/custom/admin/poll/results/index.html.erb 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" %> + +
+ <%= render "/admin/poll/polls/subnav" %> + +

<%= t("admin.results.index.title") %>

+ + <% if @partial_results.empty? && @poll.voters.empty? %> +
+ <%= t("admin.results.index.no_results") %> +
+ <% end %> + + <% if @poll.expired? %> + <%= link_to(t("admin.proposals.index.export"), + current_path_with_query_params(format: :csv), + class: "float-right small clear") %> + <% end %> + + <% if @partial_results.present? %> + <%= render "recount", resource: @poll %> + <%= render "result" %> + <%= render "results_by_booth" %> + <% end %> + + <% if @poll.voters.any? %> + <%= render "show_results", resource: @poll %> + <% end %> +
diff --git a/config/locales/custom/es/admin.yml b/config/locales/custom/es/admin.yml index 19ce88834313..e5c5294e687e 100644 --- a/config/locales/custom/es/admin.yml +++ b/config/locales/custom/es/admin.yml @@ -71,6 +71,12 @@ es: audits: Registro de cambios legislation: Propuestas del Cabildo polls: + results: + export_list: + id: ID + question_id: ID de la pregunta + answer: Respuesta + author_id: ID del autor votation_type: open_description: "Permite que el usuario responda libremente en un cuadro de texto." proposals: