Skip to content

Commit

Permalink
Allow exporting poll results
Browse files Browse the repository at this point in the history
  • Loading branch information
dzenreda authored and taitus committed May 10, 2024
1 parent 5b4a537 commit adce315
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
24 changes: 24 additions & 0 deletions app/controllers/custom/admin/poll/results_controller.rb
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions app/models/custom/poll/exporter.rb
Original file line number Diff line number Diff line change
@@ -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

29 changes: 29 additions & 0 deletions app/views/custom/admin/poll/results/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%= render "/admin/poll/polls/poll_header" %>

<div id="poll-resources">
<%= render "/admin/poll/polls/subnav" %>

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

<% if @partial_results.empty? && @poll.voters.empty? %>
<div class="callout primary margin-top">
<%= t("admin.results.index.no_results") %>
</div>
<% 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 %>
</div>
6 changes: 6 additions & 0 deletions config/locales/custom/es/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit adce315

Please sign in to comment.