Skip to content

Commit

Permalink
Correct encode errors when generating .xls
Browse files Browse the repository at this point in the history
Add specs to test the new exporter.
I18n texts for the headers.
  • Loading branch information
iagirre committed Dec 11, 2017
1 parent 4196e16 commit 146de60
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 28 deletions.
13 changes: 13 additions & 0 deletions app/models/event.rb
Expand Up @@ -140,6 +140,19 @@ def self.searches(search_person, search_title, search_lobby_activity)
return event_ids
end

def position_names
names = ''
positions.each do |position|
names += position.holder.full_name_comma + ' - ' + position.title
names += ' / ' unless position == positions.last
end
return names
end

def user_name
user.full_name
end

private

def participants_uniqueness
Expand Down
24 changes: 23 additions & 1 deletion config/locales/en.yml
Expand Up @@ -115,4 +115,26 @@ en:
approach: approach
legal_representant_full_name: legal representant full name
user_name: user name
invalidate: invalidate
invalidate: invalidate
events_exporter:
title: título
description: descripción
scheduled: fecha
updated_at: última actualización
user_name: usuario
position_names: titular de la agenda
location: lugar
status: estado
notes: notas
reasons: razones
published_at: fecha pública
canceled_at: cancelado
lobby_activity: actividad de lobby
organization_name: organización
lobby_scheduled: descripción de fecha propuesta
general_remarks: observaciones generales
lobby_contact_firstname: nombre contacto
lobby_contact_lastname: apellido contacto
lobby_contact_email: email contacto
lobby_contact_phone: teléfono contacto
manager_general_remarks: observaciones generales del gestor
22 changes: 22 additions & 0 deletions config/locales/es.yml
Expand Up @@ -124,3 +124,25 @@ es:
legal_representant_full_name: nombre y apellidos del representante legal
user_name: nombre y apellidos
invalidate: invalidada
events_exporter:
title: título
description: descripción
scheduled: fecha
updated_at: última actualización
user_name: usuario
position_names: titular de la agenda
location: lugar
status: estado
notes: notas
reasons: razones
published_at: fecha pública
canceled_at: cancelado
lobby_activity: actividad de lobby
organization_name: organización
lobby_scheduled: descripción de fecha propuesta
general_remarks: observaciones generales
lobby_contact_firstname: nombre contacto
lobby_contact_lastname: apellido contacto
lobby_contact_email: email contacto
lobby_contact_phone: teléfono contacto
manager_general_remarks: observaciones generales del gestor
12 changes: 6 additions & 6 deletions lib/events_exporter.rb
@@ -1,6 +1,6 @@
class EventsExporter
FIELDS = ['title', 'description', 'scheduled', 'user_id', 'position_id', 'location', 'status',
'notes', 'reasons', 'published_at', 'canceled_at',
FIELDS = ['title', 'description', 'scheduled', 'updated_at', 'user_name', 'position_names', 'location', 'status',
'notes', 'reasons', 'published_at', 'canceled_at', 'lobby_activity',
'organization_name', 'lobby_scheduled', 'general_remarks', 'lobby_contact_firstname',
'lobby_contact_lastname', 'lobby_contact_email', 'lobby_contact_phone', 'manager_general_remarks'].freeze

Expand All @@ -25,7 +25,7 @@ def windows_event_row(event)
def save_csv(path)
CSV.open(path, 'w', col_sep: ';', force_quotes: true, encoding: "ISO-8859-1") do |csv|
csv << windows_headers
Event.with_lobby_activity_active.find_each do |event|
Event.find_each do |event|
csv << windows_event_row(event)
end
end
Expand All @@ -37,7 +37,7 @@ def save_xls(path)
sheet.row(0).default_format = Spreadsheet::Format.new color: :blue, weight: :bold
sheet.row(0).concat headers
index = 1
Event.with_lobby_activity_active.find_each do |event|
Event.find_each do |event|
sheet.row(index).concat windows_event_row(event)
index += 1
end
Expand All @@ -48,7 +48,7 @@ def save_xls(path)
def save_json(path)
data = []
h = headers
Event.with_lobby_activity_active.find_each do |event|
Event.find_each do |event|
data << h.zip(windows_event_row(event)).to_h
end
File.open(path, "w") do |f|
Expand All @@ -59,7 +59,7 @@ def save_json(path)
private

def windows_array(values)
values.map { |v| v.to_s.encode("ISO-8859-1", invalid: :replace, undef: :replace, replace: '') }
values.map { |v| v.to_s.encode("UTF-8", invalid: :replace, undef: :replace, replace: '') }
end

end
28 changes: 13 additions & 15 deletions lib/tasks/export.rake
Expand Up @@ -2,32 +2,30 @@ require 'public_organization_exporter'
require 'fileutils'

namespace :export do
desc "Exports organizations to public/export/public_organizations.csv,
public/export/public_organizations.xls and public/export/public_organizations.json"
desc "Exports organizations to public/export/lobbies.csv,
public/export/lobbies.xls and public/export/lobbies.json"
task organizations: :environment do
folder = Rails.root.join('public', 'export')
FileUtils.rm_rf folder
FileUtils.mkdir_p folder
FileUtils.mkdir_p folder unless Dir.exist?(folder)

exporter = PublicOrganizationExporter.new

exporter.save_csv(folder.join('public_organizations.csv'))
exporter.save_xls(folder.join('public_organizations.xls'))
exporter.save_json(folder.join('public_organizations.json'))
exporter.save_csv(folder.join('lobbies.csv'))
exporter.save_xls(folder.join('lobbies.xls'))
exporter.save_json(folder.join('lobbies.json'))
end

desc "Exports organizations' events to public/export/organizations_events.csv,
public/export/organizations_events.xls and public/export/organizations_events.json"
task events: :environment do
desc "Exports organizations' events to public/export/agendas.csv,
public/export/agendas.xls and public/export/agendas.json"
task agendas: :environment do
folder = Rails.root.join('public', 'export')
FileUtils.rm_rf folder
FileUtils.mkdir_p folder
FileUtils.mkdir_p folder unless Dir.exist?(folder)

exporter = EventsExporter.new

exporter.save_csv(folder.join('organizations_events.csv'))
exporter.save_xls(folder.join('organizations_events.xls'))
exporter.save_json(folder.join('organizations_events.json'))
exporter.save_csv(folder.join('agendas.csv'))
exporter.save_xls(folder.join('agendas.xls'))
exporter.save_json(folder.join('agendas.json'))
end

end
46 changes: 46 additions & 0 deletions spec/events_exporter_spec.rb
@@ -0,0 +1,46 @@
require 'rails_helper'
require 'events_exporter'

describe EventsExporter do
let(:exporter) { EventsExporter.new }

describe '#headers' do
it "generates localized headers" do
expect(exporter.headers.first).to eq('título')
expect(exporter.headers.last).to eq('observaciones generales del gestor')
end
end

describe '#events_to_row' do
it "generates a row of info based on a event" do
position = create(:position)
event = create(:event, organization_name: "Organization name", position: position)

row = exporter.event_to_row(event)

expect(row).to include(event.title)
expect(row).to include(event.description)
expect(row).to include(event.scheduled)
expect(row).to include(event.updated_at)
expect(row).to include(event.user_name)
expect(row).to include(event.position_names)
expect(row).to include(event.location)
expect(row).to include(event.status)
expect(row).to include(event.notes)
expect(row).to include(event.reasons)
expect(row).to include(event.published_at)
expect(row).to include(event.canceled_at)
expect(row).to include(event.organization_name)
expect(row).to include(event.lobby_scheduled)
expect(row).to include(event.general_remarks)
expect(row).to include(event.lobby_contact_firstname)
expect(row).to include(event.lobby_contact_lastname)
expect(row).to include(event.lobby_scheduled)
expect(row).to include(event.general_remarks)
expect(row).to include(event.lobby_contact_phone)
expect(row).to include(event.manager_general_remarks)

end
end

end
65 changes: 59 additions & 6 deletions spec/lib/tasks/export_rake_spec.rb
Expand Up @@ -7,9 +7,9 @@
it "check files existence a files contents" do
subject.invoke

csv_file = File.open(Rails.root.to_s + '/public/export/public_organizations.csv')
json_file = File.open(Rails.root.to_s + '/public/export/public_organizations.json')
xls_file = File.open(Rails.root.to_s + '/public/export/public_organizations.xls')
csv_file = File.open(Rails.root.to_s + '/public/export/lobbies.csv')
json_file = File.open(Rails.root.to_s + '/public/export/lobbies.json')
xls_file = File.open(Rails.root.to_s + '/public/export/lobbies.xls')

expect(File).to exist(csv_file)
expect(File).to exist(json_file)
Expand All @@ -21,7 +21,7 @@
o.save!

subject.invoke
csv_file = File.open(Rails.root.to_s + '/public/export/public_organizations.csv').read.scrub
csv_file = File.open(Rails.root.to_s + '/public/export/lobbies.csv').read.scrub

csv = CSV.parse(csv_file, headers: true, col_sep: ';')
expect(csv.first.to_hash.values[0]).to eq(o.reference)
Expand All @@ -32,7 +32,7 @@
o.save!

subject.invoke
json_file = File.open(Rails.root.to_s + '/public/export/public_organizations.json')
json_file = File.open(Rails.root.to_s + '/public/export/lobbies.json')

parsed_json = ActiveSupport::JSON.decode(json_file.read)
expect(parsed_json.first.values.first).to eq(o.reference)
Expand All @@ -43,7 +43,7 @@
o.save!

subject.invoke
xls_file = File.open(Rails.root.to_s + '/public/export/public_organizations.xls')
xls_file = File.open(Rails.root.to_s + '/public/export/lobbies.xls')

require 'spreadsheet'
book = Spreadsheet.open(xls_file)
Expand All @@ -52,3 +52,56 @@
end

end

describe "export:agendas" do

include_context "rake"

it "check files existence a files contents" do
subject.invoke

csv_file = File.open(Rails.root.to_s + '/public/export/agendas.csv')
json_file = File.open(Rails.root.to_s + '/public/export/agendas.json')
xls_file = File.open(Rails.root.to_s + '/public/export/agendas.xls')

expect(File).to exist(csv_file)
expect(File).to exist(json_file)
expect(File).to exist(xls_file)
end

it "check csv contents" do
o = create(:event)
o.save!

subject.invoke
csv_file = File.open(Rails.root.to_s + '/public/export/agendas.csv').read.scrub

csv = CSV.parse(csv_file, headers: true, col_sep: ';')
expect(csv.first.to_hash.values[0]).to eq(o.title)
end

it "check json contents" do
o = create(:event)
o.save!

subject.invoke
json_file = File.open(Rails.root.to_s + '/public/export/agendas.json')

parsed_json = ActiveSupport::JSON.decode(json_file.read)
expect(parsed_json.first.values.first).to eq(o.title)
end

it "check xsl contents" do
o = create(:event)
o.save!

subject.invoke
xls_file = File.open(Rails.root.to_s + '/public/export/agendas.xls')

require 'spreadsheet'
book = Spreadsheet.open(xls_file)
sheet = book.worksheets[0]
expect(sheet.rows[1][0]).to eq(o.title)
end

end

0 comments on commit 146de60

Please sign in to comment.