Skip to content

Commit 4bff696

Browse files
committed
fix handling of rewrite headers option
1 parent f055cb8 commit 4bff696

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

lib/active_admin_import/importer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def import_options
8080
protected
8181

8282
def prepare_headers(headers)
83-
@headers = Hash[headers.zip(headers.map { |el| el.underscore.gsub(/\s+/, '_') })]
83+
@headers = Hash[headers.zip(headers.map { |el| el.underscore.gsub(/\s+/, '_') })].with_indifferent_access
8484
@headers.merge!(options[:headers_rewrites])
8585
@headers
8686
end

lib/active_admin_import/options.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ module ActiveAdminImport
1616
:template_object,
1717
:resource_class,
1818
:resource_label,
19-
:plural_resource_label
19+
:plural_resource_label ,
20+
:headers_rewrites
2021
].freeze
2122

2223

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Name,Second name,Birthday
2+
John,Doe,1986-05-01

spec/import_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ def upload_file!(name, ext='csv')
145145
end
146146
end
147147

148+
context "with different header attribute names" do
149+
150+
let(:options) {
151+
{
152+
headers_rewrites: { :'Second name' => :last_name }
153+
}
154+
}
155+
156+
it "should import file" do
157+
upload_file!(:author_broken_header)
158+
expect(page).to have_content "Successfully imported 1 author"
159+
expect(Author.count).to eq(1)
160+
end
161+
end
148162

149163
context "with semicolons separator" do
150164
let(:options) {

0 commit comments

Comments
 (0)