Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to add UTF-8 BOM #103

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ GEM
multi_json (~> 1.0)
simplecov-html (~> 0.9.0)
simplecov-html (0.9.0)
sqlite3 (1.3.5)
sqlite3 (1.3.11)
thor (0.19.1)

PLATFORMS
Expand All @@ -61,3 +61,6 @@ DEPENDENCIES
rest-client (> 1.6.2, < 1.7)
rspec (~> 2.8.0)
sqlite3 (~> 1.3.4)

BUNDLED WITH
1.12.5
5 changes: 4 additions & 1 deletion lib/comma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ module Comma
filename = options[:filename] || 'data'
extension = options[:extension] || 'csv'
mime_type = options[:mime_type] || Mime::CSV
with_bom = options[:with_bom] || false
#Capture any CSV optional settings passed to comma or comma specific options
csv_options = options.slice(*CSV_HANDLER::DEFAULT_OPTIONS.merge(Comma::DEFAULT_OPTIONS).keys)
send_data obj.to_comma(csv_options), :type => mime_type, :disposition => "attachment; filename=\"#{filename}.#{extension}\""
data = obj.to_comma(csv_options)
data = "\xEF\xBB\xBF#{data}" if with_bom
send_data data, :type => mime_type, :disposition => "attachment; filename=\"#{filename}.#{extension}\""
end
end
end
12 changes: 12 additions & 0 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@
response.content_type.should == 'text/plain'
end

it 'should allow bom to be set' do
get :with_custom_options, :format => :csv, :custom_options => { :with_bom => true }

expected_content =<<-CSV.gsub(/^\s+/,'')
\xEF\xBB\xBFFirst name,Last name
Fred,Flintstone
Wilma,Flintstone
CSV

response.body.should == expected_content
end

describe 'headers' do

it 'should allow toggling on' do
Expand Down