Skip to content
13 changes: 10 additions & 3 deletions lib/active_admin_csv_import/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ def csv_importable(options = {})
collection_action :import_csv do
@columns = columns
@required_columns = required_columns
@collection_path = parent ? collection_path({"#{parent.class.name.underscore}_id" => parent.id}) : collection_path

@post_path = options[:path].try(:call)
@post_path ||= collection_path + '/import_rows'
@post_path ||= @collection_path + '/import_rows'

@redirect_path = options[:redirect_path].try(:call)
@redirect_path ||= collection_path
@redirect_path ||= @collection_path

@delimiter = options[:delimiter]

Expand All @@ -53,7 +54,7 @@ def csv_importable(options = {})
row_number = row_params.delete('_row')

resource = existing_row_resource(options[:import_unique_key], row_params)
resource ||= active_admin_config.resource_class.new
resource ||= build_row_resource

unless update_row_resource(resource, row_params)
@failures << {
Expand Down Expand Up @@ -87,6 +88,12 @@ def update_row_resource(resource, params)
resource.save
end

def build_row_resource
resource = end_of_association_chain.new
Copy link
Member

Choose a reason for hiding this comment

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

Does build work here instead of new? Might be a bit more idiomatic.

# controller before create callback
resource = before_create(resource) if respond_to? :before_create
Copy link
Member

Choose a reason for hiding this comment

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

This feels a bit odd. Should at least be namespaced to CSV. Was this something here already?

Copy link
Author

Choose a reason for hiding this comment

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

yea, good point namespacing. It's a new addition, it felt right to put the build logic into it's own method but it's something we could of easily had left in the import_csv action

end

def existing_row_resource(lookup_column, params)
return unless lookup_column

Expand Down