Skip to content

Commit

Permalink
Merge pull request 'Refactor code and increase tests coverage for cus…
Browse files Browse the repository at this point in the history
…tom export legislation process feature' (#11) from export_legislation_process_specs into master

Reviewed-on: https://git.nubarron.teide.int/shs-consul/cabildoabierto-consul/pulls/11
  • Loading branch information
javierm committed Feb 15, 2023
2 parents 45a46ce + 41eaf47 commit 493589d
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
class Admin::Legislation::ProcessesController
def download
respond_to do |format|
format.zip
format.csv do
send_data ::Legislation::Processes::Exporter.new(@process).to_zip, type: "application/zip", disposition: "attachment", filename: "process.zip"
format.zip do
filename = "process_#{@process.id}.zip"
send_data ::Legislation::Processes::Exporter.new(@process).to_zip, type: "application/zip",
disposition: "attachment",
filename: filename
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/custom/admin/legislation/processes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<td class="text-center"><%= process.sdg_target_list %></td>
<td>
<%= render Admin::TableActionsComponent.new(process) do |actions| %>
<%= actions.link_to "Descargar CSV",
download_admin_legislation_process_path(process, csv_params),
<%= actions.link_to t("admin.legislation.processes.index.download"),
download_admin_legislation_process_path(process, format: :zip),
target: "_blank",
rel: "",
class: "download-link" %>
Expand Down
1 change: 1 addition & 0 deletions config/locales/custom/es/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ es:
proposals_description: Descripcion de propuestas
index:
title: Procesos de propuestas del Cabildo
download: Descargar CSV
new:
title: Crear nuevo proceso de propuestas del Cabildo
answers:
Expand Down
3 changes: 0 additions & 3 deletions config/routes/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,6 @@
namespace :legislation do
resources :processes do
get :download, on: :member
member do
patch :toggle_selection
end
resources :questions
resources :proposals do
member { patch :toggle_selection }
Expand Down
20 changes: 20 additions & 0 deletions spec/models/custom/legislation/annotations/exporter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "rails_helper"

describe Legislation::Annotations::Exporter do
let(:annotation) { create(:legislation_annotation) }

context "#to_csv" do
scenario "generate csv with answer content" do
I18n.with_locale(:es) do
csv = Legislation::Annotations::Exporter.new(annotation.draft_version.process).to_csv

comment = annotation.comments.first
version = annotation.draft_version
csv_contents = "ID,Comentario,Usuario,ID del borrador,Versión del borrador,ID de la anotación,Texto de la anotación\n"\
"#{comment.id},#{comment.body},#{comment.user.id},#{version.id},#{version.title},"\
"#{annotation.id},#{annotation.quote}\n"
expect(csv).to eq csv_contents
end
end
end
end
18 changes: 18 additions & 0 deletions spec/models/custom/legislation/answers/exporter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "rails_helper"

describe Legislation::Answers::Exporter do
let(:answer) { create(:legislation_answer) }

context "#to_csv" do
scenario "generate csv with answer content" do
I18n.with_locale(:es) do
csv = Legislation::Answers::Exporter.new(answer.question.process).to_csv

csv_contents = "ID,Tipo de respuesta,ID de la pregunta,Texto de la pregunta,Respuesta,Usuario\n"\
"#{answer.id},Opcional,#{answer.question.id},#{answer.question.title},"\
"#{answer.question_option.value},#{answer.user.id}\n"
expect(csv).to eq csv_contents
end
end
end
end
18 changes: 18 additions & 0 deletions spec/models/custom/legislation/proposals/exporter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "rails_helper"

describe Legislation::Proposals::Exporter do
let(:proposal) { create(:legislation_proposal) }

context "#to_csv" do
scenario "generate csv with answer content" do
I18n.with_locale(:es) do
csv = Legislation::Proposals::Exporter.new(proposal.process).to_csv

csv_contents = "ID,Título de la propuesta,Descripción de la propuesta,ID del autor,Votos totales,Número de comentarios\n"\
"#{proposal.id},#{proposal.title},#{proposal.description},#{proposal.author.id},"\
"#{proposal.cached_votes_total},#{proposal.comments_count}\n"
expect(csv).to eq csv_contents
end
end
end
end
22 changes: 22 additions & 0 deletions spec/system/custom/admin/legislation/processes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "rails_helper"

describe "Admin collaborative legislation", :admin do
context "Index" do
scenario "Shows a download link per process", :no_js do
process = create(:legislation_process)

visit admin_legislation_processes_path(filter: "all", locale: :es)
within "#legislation_process_#{process.id}" do
click_link("Descargar CSV")
end

header = page.response_headers["Content-Disposition"]
expect(header).to match(/^attachment;/)
expect(header).to match(/filename="process_#{process.id}.zip"$/)
expect(page.body).to include "process.csv"
expect(page.body).to include "questions_answers.csv"
expect(page.body).to include "draft_comments.csv"
expect(page.body).to include "proposals.csv"
end
end
end

0 comments on commit 493589d

Please sign in to comment.