Skip to content

Commit

Permalink
Fix: Export collections CSVs with non-ASCII chars (#907)
Browse files Browse the repository at this point in the history
This commit monkey-patches `CSV.generate` to support non-ASCII locales,
following what Ruby did in ruby/csv#63

Fixes #902
  • Loading branch information
matiasgarciaisaia committed Nov 17, 2022
1 parent 928a8f2 commit 216f0e0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
21 changes: 21 additions & 0 deletions app/models/collection/csv_concern.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
class CSV
# monkey-patch CSV.generate to support non-ASCII locales
# see ruby/csv#63 ( https://github.com/ruby/csv/pull/63/files )
def self.generate(*args)
# add a default empty String, if none was given
if args.first.is_a? String
io = StringIO.new(args.shift)
io.seek(0, IO::SEEK_END)
args.unshift(io)
else
encoding = args[-1][:encoding] if args.last.is_a?(Hash)
str = +""
str.force_encoding(encoding) if encoding
args.unshift(str)
end
csv = new(*args) # wrap
yield csv # yield for appending
csv.string # return final String
end
end

module Collection::CsvConcern
extend ActiveSupport::Concern

Expand Down
7 changes: 5 additions & 2 deletions spec/controllers/api/collections_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,20 @@
end

describe "GET CSV collection" do
let!(:site3) {collection.sites.make :name => "किसी जगह", properties: { hierarchy.es_code => 'bro' } }

before(:each) do
get :show, id: collection.id, format: 'csv'
end

it { expect(response).to be_success }

it "should return CSV" do
it "should return CSV with non-ASCII values" do
csv = CSV.parse response.body
expect(csv.length).to eq(3)
expect(csv.length).to eq(4)

expect(csv[0]).to eq(['resmap-id', 'name', 'lat', 'long', text.code, numeric.code, yes_no.code, select_one.code, select_many.code, hierarchy.code,"#{hierarchy.code}-1", "#{hierarchy.code}-2", site_ref.code, date.code, director.code, 'last updated'])
expect(csv).to include [site3.id.to_s, site3.name, site3.lat.to_s, site3.lng.to_s, "", "", "no", "", "", "bro", "Dad", "Bro", "", "", "", site3.updated_at.to_datetime.rfc822]
expect(csv).to include [site2.id.to_s, site2.name, site2.lat.to_s, site2.lng.to_s, "", "", "no", "", "", "bro", "Dad", "Bro", "", "", "", site2.updated_at.to_datetime.rfc822]
expect(csv).to include [site.id.to_s, site.name, site.lat.to_s, site.lng.to_s, site.properties[text.es_code], site.properties[numeric.es_code].to_s, 'yes', 'one', 'one, two', 'dad', 'Dad', '', site2.id.to_s, '10/24/2012', user.email, site.updated_at.to_datetime.rfc822]
end
Expand Down

0 comments on commit 216f0e0

Please sign in to comment.