Skip to content
Merged
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
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling symbolize_keys! would be a bit simpler, I guess.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is easier but I prefered to use the safest option. First, not knowing the rest of the code and the consequences, I prefered to be able to use string and symbol as keys, to be safe. Also, you suggestion would actually be to use .deep_symbolize_keys!
At least using HashWithIndifferentAccess, I am sure I am not introducing any regression.

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