From f592d68f2c611cee3733ce3f1ef1085a89a1b898 Mon Sep 17 00:00:00 2001 From: "M.Shibuya" Date: Sun, 17 Feb 2019 14:09:46 +0900 Subject: [PATCH] Revert c91fd9cff7 and raise error if CSV has encoding issue This is safer than to change existing encoding handling. --- .travis.yml | 4 ++-- lib/rails_admin/support/csv_converter.rb | 7 ++++++- spec/rails_admin/support/csv_converter_spec.rb | 9 +++------ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index b097de96fb..0f18d21dfe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/lib/rails_admin/support/csv_converter.rb b/lib/rails_admin/support/csv_converter.rb index 26ce716e12..dc001aa044 100644 --- a/lib/rails_admin/support/csv_converter.rb +++ b/lib/rails_admin/support/csv_converter.rb @@ -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 diff --git a/spec/rails_admin/support/csv_converter_spec.rb b/spec/rails_admin/support/csv_converter_spec.rb index f27227ff76..9bf93404f2 100644 --- a/spec/rails_admin/support/csv_converter_spec.rb +++ b/spec/rails_admin/support/csv_converter_spec.rb @@ -35,17 +35,14 @@ 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 @@ -53,7 +50,7 @@ when 'postgresql' @connection.set_client_encoding('utf8') when 'mysql2' - @connection.send :charset_name=, 'utf8' + ActiveRecord::Base.connection.execute('SET NAMES utf8;') end end