Skip to content

Commit

Permalink
Merge pull request #2741 from loicginoux/hotfix/export-options
Browse files Browse the repository at this point in the history
csv export options need to be HashWithIndifferentAccess
  • Loading branch information
mshibuya committed Nov 19, 2016
2 parents 9e1c66e + d20f622 commit 81f612f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/rails_admin/support/csv_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 20 additions & 1 deletion spec/rails_admin/support/csv_converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) { '' }
Expand Down Expand Up @@ -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

0 comments on commit 81f612f

Please sign in to comment.