From 0245d4cbd5dab7952d6f21cd31bdc3f6f2efad88 Mon Sep 17 00:00:00 2001 From: Loic Ginoux Date: Thu, 13 Oct 2016 18:12:42 +0200 Subject: [PATCH 1/3] csv export options need to be HashWithIndifferentAccess.new --- lib/rails_admin/support/csv_converter.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/rails_admin/support/csv_converter.rb b/lib/rails_admin/support/csv_converter.rb index 1f71b49624..0a71f47e8e 100644 --- a/lib/rails_admin/support/csv_converter.rb +++ b/lib/rails_admin/support/csv_converter.rb @@ -32,6 +32,7 @@ def initialize(objects = [], schema = {}) end def to_csv(options = {}) + options = HashWithIndifferentAccess.new(options) encoding_to = Encoding.find(options[:encoding_to]) if options[:encoding_to].present? csv_string = generate_csv_string(options) From fe9ea4ba7b0c303f669899aa336343ef8100e5c2 Mon Sep 17 00:00:00 2001 From: Loic Ginoux Date: Tue, 8 Nov 2016 18:29:50 +0100 Subject: [PATCH 2/3] adding test for csv_converter#to_csv --- .../rails_admin/support/csv_converter_spec.rb | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/spec/rails_admin/support/csv_converter_spec.rb b/spec/rails_admin/support/csv_converter_spec.rb index 7c2a2a3e97..abf082bb22 100644 --- a/spec/rails_admin/support/csv_converter_spec.rb +++ b/spec/rails_admin/support/csv_converter_spec.rb @@ -29,8 +29,9 @@ let(:objects) { FactoryGirl.create_list :player, 1, number: 1, name: 'なまえ' } let(:schema) { {only: [:number, :name]} } + let(:options) { {encoding_to: encoding} } - subject { RailsAdmin::CSVConverter.new(objects, schema).to_csv(encoding_to: encoding) } + subject { RailsAdmin::CSVConverter.new(objects, schema).to_csv(options) } context 'when encoding FROM latin1', active_record: true do let(:encoding) { '' } @@ -93,5 +94,23 @@ to eq 'feff004e0075006d006200650072002c004e0061006d0065000a0031002c306a307e3048000a' end end + + context "when specifying a column separator" do + context "when options keys are symbolized" do + let(:options) { {encoding_to: 'UTF-8', generator: {col_sep: '___'}} } + it "uses the column separator specified" do + expect(subject[2].unpack('H*').first). + to eq( "efbbbf4e756d6265725f5f5f4e616d650a315f5f5fe381aae381bee381880a") + end + end + + context "when options keys are string" do + let(:options) { {'encoding_to' => 'UTF-8', 'generator' => {'col_sep' => '___'}} } + it "uses the column separator specified" do + expect(subject[2].unpack('H*').first). + to eq( "efbbbf4e756d6265725f5f5f4e616d650a315f5f5fe381aae381bee381880a") + end + end + end end end From d20f622c9ca3f7683d585b881ab0e9d43fc39ab4 Mon Sep 17 00:00:00 2001 From: Loic Ginoux Date: Fri, 18 Nov 2016 15:41:41 +0100 Subject: [PATCH 3/3] resolve rubocop offenses --- spec/rails_admin/support/csv_converter_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/rails_admin/support/csv_converter_spec.rb b/spec/rails_admin/support/csv_converter_spec.rb index abf082bb22..8cc6ff08be 100644 --- a/spec/rails_admin/support/csv_converter_spec.rb +++ b/spec/rails_admin/support/csv_converter_spec.rb @@ -100,7 +100,7 @@ let(:options) { {encoding_to: 'UTF-8', generator: {col_sep: '___'}} } it "uses the column separator specified" do expect(subject[2].unpack('H*').first). - to eq( "efbbbf4e756d6265725f5f5f4e616d650a315f5f5fe381aae381bee381880a") + to eq 'efbbbf4e756d6265725f5f5f4e616d650a315f5f5fe381aae381bee381880a' end end @@ -108,7 +108,7 @@ let(:options) { {'encoding_to' => 'UTF-8', 'generator' => {'col_sep' => '___'}} } it "uses the column separator specified" do expect(subject[2].unpack('H*').first). - to eq( "efbbbf4e756d6265725f5f5f4e616d650a315f5f5fe381aae381bee381880a") + to eq 'efbbbf4e756d6265725f5f5f4e616d650a315f5f5fe381aae381bee381880a' end end end