Skip to content

Commit fcd5d60

Browse files
Merge pull request #165 from DiputacioBarcelona/utf8_with_bom
Add override to include BOM in CSVs exported by Decidim::Exporters::CSV
2 parents 803dab6 + 69a221f commit fcd5d60

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
module Decidim
4+
module Exporters
5+
# Overrides Decidim::Exporters::CSV#export to prepend the UTF-8 byte order
6+
# mark to the generated CSV, so spreadsheet software (notably Microsoft
7+
# Excel) detects the encoding and renders non-ASCII characters correctly.
8+
#
9+
# Upstream Decidim does not include the BOM and the maintainers won't accept
10+
# this change, so it is applied locally as an override instead of forking.
11+
module CsvOverrides
12+
# UTF-8 byte order mark.
13+
BOM = ""
14+
15+
# Public: Exports a CSV serialized version of the collection, prepending
16+
# the UTF-8 BOM to the output.
17+
#
18+
# Returns an ExportData instance.
19+
def export(col_sep = Decidim.default_csv_col_sep)
20+
data = ::CSV.generate(BOM.dup, headers:, write_headers: true, col_sep:) do |csv|
21+
processed_collection.each do |resource|
22+
csv << headers.map { |header| custom_sanitize(resource[header]) }
23+
end
24+
end
25+
ExportData.new(data, "csv")
26+
end
27+
end
28+
end
29+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
Rails.application.config.to_prepare do
4+
Decidim::Exporters::CSV.prepend(Decidim::Exporters::CsvOverrides)
5+
end

spec/lib/overrides_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
files: {
1111
"/app/cells/decidim/newsletter_templates/basic_only_text/show.erb" => "7d9d4f2ab8897143fe66e8ac8db2cedb",
1212
"/app/cells/decidim/newsletter_templates/basic_only_text_settings_form/show.erb" => "cc8e26ddc53c0b65eddf472ed4c5614e",
13-
"/app/cells/decidim/newsletter_templates/image_text_cta/show.erb" => "08881fe2df7d87db0497376b08821594"
13+
"/app/cells/decidim/newsletter_templates/image_text_cta/show.erb" => "08881fe2df7d87db0497376b08821594",
14+
# Overridden by Decidim::Exporters::CsvOverrides (config/initializers/lib_overrides.rb)
15+
# to prepend the UTF-8 BOM. If this checksum changes, review the override.
16+
"/lib/decidim/exporters/csv.rb" => "26eeaecb13dbb4dbce28e39e8dc7a3fd"
1417
}
1518
}
1619
]

0 commit comments

Comments
 (0)