From b2b64ca8a04cc775867b8aaeda7afedf6d62eb11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 25 Aug 2020 16:38:30 +0200 Subject: [PATCH] Add link to download summary in XLSX format again It was removed in commit 128a8164 alongside everything related to the legislation process summary. Co-Authored-By: taitus --- Gemfile | 2 + Gemfile.lock | 10 ++++ .../stylesheets/legislation_process.scss | 7 +++ .../legislation/processes_controller.rb | 5 ++ .../legislation/processes/summary.html.erb | 4 ++ .../legislation/processes/summary.xlsx.axlsx | 50 +++++++++++++++++++ config/locales/en/legislation.yml | 1 + config/locales/es/legislation.yml | 1 + .../legislation/processes_controller_spec.rb | 13 +++++ spec/system/legislation/summary_spec.rb | 7 +++ 10 files changed, 100 insertions(+) create mode 100644 app/views/legislation/processes/summary.xlsx.axlsx create mode 100644 spec/controllers/legislation/processes_controller_spec.rb diff --git a/Gemfile b/Gemfile index ae12ddb0b..b9659381a 100644 --- a/Gemfile +++ b/Gemfile @@ -10,6 +10,8 @@ gem "ancestry", "~> 3.0.7" gem "audited", "~> 4.9.0" gem "autoprefixer-rails", "~> 8.2.0" gem "cancancan", "~> 2.3.0" +gem "caxlsx", "~> 3.0.2" +gem "caxlsx_rails", "~> 0.6.2" gem "ckeditor", "~> 4.3.0" gem "cocoon", "~> 1.2.14" gem "daemons", "~> 1.3.1" diff --git a/Gemfile.lock b/Gemfile.lock index 566735c56..71fdf2d8d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -131,6 +131,14 @@ GEM rack (>= 1.4) rack-proxy (>= 0.6.0) selenium-webdriver (~> 3.0) + caxlsx (3.0.2) + htmlentities (~> 4.3, >= 4.3.4) + mimemagic (~> 0.3) + nokogiri (~> 1.10, >= 1.10.4) + rubyzip (>= 1.3.0, < 3) + caxlsx_rails (0.6.2) + actionpack (>= 3.1) + caxlsx (>= 3.0) chef-utils (16.4.41) childprocess (0.9.0) ffi (~> 1.0, >= 1.0.11) @@ -638,6 +646,8 @@ DEPENDENCIES capistrano3-puma (~> 4.0.0) capybara (~> 3.29.0) capybara-webmock (~> 0.5.5) + caxlsx (~> 3.0.2) + caxlsx_rails (~> 0.6.2) ckeditor (~> 4.3.0) cocoon (~> 1.2.14) coveralls (~> 0.8.22) diff --git a/app/assets/stylesheets/legislation_process.scss b/app/assets/stylesheets/legislation_process.scss index 81c090a5f..8f3199c06 100644 --- a/app/assets/stylesheets/legislation_process.scss +++ b/app/assets/stylesheets/legislation_process.scss @@ -1086,4 +1086,11 @@ padding: rem-calc(12); } } + + .download-button { + margin-bottom: 0; + margin-left: 50%; + margin-top: $line-height; + transform: translateX(-50%); + } } diff --git a/app/controllers/legislation/processes_controller.rb b/app/controllers/legislation/processes_controller.rb index 2c8f01076..0edd2cc5a 100644 --- a/app/controllers/legislation/processes_controller.rb +++ b/app/controllers/legislation/processes_controller.rb @@ -101,6 +101,11 @@ class Legislation::ProcessesController < Legislation::BaseController @phase = :summary @proposals = @process.proposals.selected @comments = @process.draft_versions.published.last&.best_comments || Comment.none + + respond_to do |format| + format.html + format.xlsx { render xlsx: "summary", filename: "summary-#{Date.current}.xlsx" } + end end def proposals diff --git a/app/views/legislation/processes/summary.html.erb b/app/views/legislation/processes/summary.html.erb index 97c211d7a..81145e8fe 100644 --- a/app/views/legislation/processes/summary.html.erb +++ b/app/views/legislation/processes/summary.html.erb @@ -6,6 +6,10 @@ <% if @process.debate_phase.enabled? || @process.proposals_phase.enabled? || @process.allegations_phase.enabled? %>
+ <%= link_to t("legislation.summary.download"), + summary_legislation_process_path(@process, format: :xlsx), + class: "button hollow download-button" %> + <% if @process.debate_phase.enabled? %> <%= render "summary_debate", questions: @process.questions %> <% end %> diff --git a/app/views/legislation/processes/summary.xlsx.axlsx b/app/views/legislation/processes/summary.xlsx.axlsx new file mode 100644 index 000000000..97baddc24 --- /dev/null +++ b/app/views/legislation/processes/summary.xlsx.axlsx @@ -0,0 +1,50 @@ +xlsx_package.workbook.add_worksheet(name: "Summary") do |sheet| + +styles = xlsx_package.workbook.styles +title = styles.add_style(b:true) +link = styles.add_style(fg_color: "0000FF", u: true) + + if @process.debate_phase.enabled? && @process.questions.any? + sheet.add_row [t("legislation.summary.debate_phase"), t("legislation.summary.debates", count: @process.questions.count)], style: title + @process.questions.each do |question| + sheet.add_row [question.title, t("legislation.summary.comments", count: question.comments.count)], style: link + sheet.add_hyperlink location: legislation_process_question_url(question.process, question), ref: sheet.rows.last.cells.first + sheet.add_hyperlink location: polymorphic_url(question, anchor: "comments"), ref: sheet.rows.last.cells.last + sheet.add_row [t("legislation.summary.most_voted_comments")] if question.best_comments.any? + question.best_comments.each do |comment| + sheet.add_row [comment.body, t("legislation.summary.votes", count: comment.votes_score)] + sheet.add_hyperlink location: comment_url(comment), ref: sheet.rows.last.cells.first + sheet.rows.last.cells.first.style = link + end + sheet.add_row ["", ""] + end + end + + if @process.proposals_phase.enabled? && @proposals.any? + sheet.add_row [t("legislation.summary.proposals_phase"), t("legislation.summary.proposals", count: @proposals.count)], style: title + @proposals.sort_by_supports.each do |proposal| + sheet.add_row [proposal.title, t("legislation.summary.votes", count: proposal.votes_score)] + sheet.add_hyperlink location: legislation_process_proposal_url(proposal.legislation_process_id, proposal), ref: sheet.rows.last.cells.first + sheet.rows.last.cells.first.style = link + end + sheet.add_row ["", ""] + end + + if @process.allegations_phase.enabled? && @comments.any? + sheet.add_row [t("legislation.summary.allegations_phase"), + t("legislation.summary.top_comments", count: @comments.count)], style: title + @comments.group_by(&:commentable).each do |annotation, annotation_comments| + sheet.add_row [t("legislation.annotations.index.comments_about")] + sheet.add_row [annotation.quote, t("legislation.summary.comments", count: annotation.comments.count)] + sheet.add_hyperlink location: polymorphic_url(annotation, anchor: "comments"), ref: sheet.rows.last.cells.last + sheet.rows.last.cells.last.style = link + + annotation_comments.each do |comment| + sheet.add_row [comment.body, t("legislation.summary.votes", count: comment.votes_score)] + sheet.add_hyperlink location: comment_url(comment), ref: sheet.rows.last.cells.first + sheet.rows.last.cells.first.style = link + end + sheet.add_row ["", ""] + end + end +end diff --git a/config/locales/en/legislation.yml b/config/locales/en/legislation.yml index 665cb35ee..263dd4662 100644 --- a/config/locales/en/legislation.yml +++ b/config/locales/en/legislation.yml @@ -139,6 +139,7 @@ en: zero: "No comments" one: "%{count} comment" other: "%{count} comments" + download: "Download summary" top_comments: zero: "No comments" one: "%{count} comment" diff --git a/config/locales/es/legislation.yml b/config/locales/es/legislation.yml index 2c88e185b..480fce46b 100644 --- a/config/locales/es/legislation.yml +++ b/config/locales/es/legislation.yml @@ -139,6 +139,7 @@ es: zero: "No hay comentarios" one: "%{count} comentario" other: "%{count} comentarios" + download: "Descargar resumen" top_comments: zero: "No hay comentarios" one: "%{count} comentario" diff --git a/spec/controllers/legislation/processes_controller_spec.rb b/spec/controllers/legislation/processes_controller_spec.rb new file mode 100644 index 000000000..c15a293f3 --- /dev/null +++ b/spec/controllers/legislation/processes_controller_spec.rb @@ -0,0 +1,13 @@ +require "rails_helper" + +describe Legislation::ProcessesController do + let(:legislation_process) { create(:legislation_process, end_date: Date.current - 1.day) } + + it "download excel file test" do + create(:legislation_question, process: legislation_process, title: "Question 1") + + get :summary, params: { id: legislation_process, format: :xlsx } + + expect(response).to be_success + end +end diff --git a/spec/system/legislation/summary_spec.rb b/spec/system/legislation/summary_spec.rb index 0763c16ba..880c7ee4e 100644 --- a/spec/system/legislation/summary_spec.rb +++ b/spec/system/legislation/summary_spec.rb @@ -164,6 +164,13 @@ describe "Legislation" do expect(page).to have_link "Comment 2" expect(page).to have_link "Comment 3" end + + scenario "excel download" do + visit summary_legislation_process_path(process) + click_link "Download summary" + + expect(page.response_headers["Content-Type"]).to match(/officedocument.spreadsheetml/) + end end def annotation_ranges(start_offset, end_offset)