Skip to content

Commit 216f0e0

Browse files
Fix: Export collections CSVs with non-ASCII chars (#907)
This commit monkey-patches `CSV.generate` to support non-ASCII locales, following what Ruby did in ruby/csv#63 Fixes #902
1 parent 928a8f2 commit 216f0e0

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

app/models/collection/csv_concern.rb

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
class CSV
2+
# monkey-patch CSV.generate to support non-ASCII locales
3+
# see ruby/csv#63 ( https://github.com/ruby/csv/pull/63/files )
4+
def self.generate(*args)
5+
# add a default empty String, if none was given
6+
if args.first.is_a? String
7+
io = StringIO.new(args.shift)
8+
io.seek(0, IO::SEEK_END)
9+
args.unshift(io)
10+
else
11+
encoding = args[-1][:encoding] if args.last.is_a?(Hash)
12+
str = +""
13+
str.force_encoding(encoding) if encoding
14+
args.unshift(str)
15+
end
16+
csv = new(*args) # wrap
17+
yield csv # yield for appending
18+
csv.string # return final String
19+
end
20+
end
21+
122
module Collection::CsvConcern
223
extend ActiveSupport::Concern
324

spec/controllers/api/collections_controller_spec.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,20 @@
211211
end
212212

213213
describe "GET CSV collection" do
214+
let!(:site3) {collection.sites.make :name => "किसी जगह", properties: { hierarchy.es_code => 'bro' } }
215+
214216
before(:each) do
215217
get :show, id: collection.id, format: 'csv'
216218
end
217219

218220
it { expect(response).to be_success }
219221

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

224226
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'])
227+
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]
225228
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]
226229
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]
227230
end

0 commit comments

Comments
 (0)