Skip to content

Commit

Permalink
Revert c91fd9c and raise error if CSV has encoding issue
Browse files Browse the repository at this point in the history
This is safer than to change existing encoding handling.
  • Loading branch information
mshibuya committed Feb 17, 2019
1 parent 6e4a5a5 commit f592d68
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ matrix:
- rvm: 2.5.3
env: CI_ORM=active_record CI_DB_ADAPTER=sqlite3
gemfile: gemfiles/rails_5.2.gemfile
- rvm: 2.6.0
- rvm: 2.6.1
env: CI_ORM=mongoid
gemfile: gemfiles/rails_5.2.gemfile
- rvm: 2.6.0
- rvm: 2.6.1
env: CI_ORM=active_record CI_DB_ADAPTER=sqlite3
gemfile: gemfiles/rails_5.2.gemfile
- rvm: ruby-head
Expand Down
7 changes: 6 additions & 1 deletion lib/rails_admin/support/csv_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ def initialize(objects = [], schema = {})
end

def to_csv(options = {})
if CSV::VERSION == '3.0.2'
raise <<-MSG.gsub(/^\s+/, '')
CSV library bundled with Ruby 2.6.0 has encoding issue, please upgrade Ruby to 2.6.1 or later.
https://github.com/ruby/csv/issues/62
MSG
end
options = HashWithIndifferentAccess.new(options)
encoding_to = Encoding.find(options[:encoding_to]) if options[:encoding_to].present?

csv_string = generate_csv_string(options)
csv_string.force_encoding(@abstract_model.encoding) if @abstract_model
if encoding_to
csv_string = csv_string.encode(encoding_to, invalid: :replace, undef: :replace, replace: '?')
end
Expand Down
9 changes: 3 additions & 6 deletions spec/rails_admin/support/csv_converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,22 @@

context 'when encoding FROM latin1', active_record: true do
let(:encoding) { '' }
let!(:objects) { FactoryBot.create_list :player, 1, number: 1, name: 'Josè'.encode('ISO-8859-1') }
let(:objects) { FactoryBot.create_list :player, 1, number: 1, name: 'Josè'.encode('ISO-8859-1') }
before do
case ActiveRecord::Base.connection_config[:adapter]
when 'postgresql'
@connection = ActiveRecord::Base.connection.instance_variable_get(:@connection)
@connection.set_client_encoding('latin1')
when 'mysql2'
@connection = ActiveRecord::Base.connection.instance_variable_get(:@connection)
@connection.send :charset_name=, 'latin1'
when 'sqlite3'
skip 'SQLite3 does not support latin1'
ActiveRecord::Base.connection.execute('SET NAMES latin1;')
end
end
after do
case ActiveRecord::Base.connection_config[:adapter]
when 'postgresql'
@connection.set_client_encoding('utf8')
when 'mysql2'
@connection.send :charset_name=, 'utf8'
ActiveRecord::Base.connection.execute('SET NAMES utf8;')
end
end

Expand Down

0 comments on commit f592d68

Please sign in to comment.