solidusio-contrib / solidus_importer

Solidus importer extension to migrate data from other eCommerce systems
BSD 3-Clause "New" or "Revised" License
15 stars 30 forks source link

Allow custom SolidusImporter::Import strategy to read CSV files #61

Open cesartalves opened 3 years ago

cesartalves commented 3 years ago

Right now SolidusImporter::ProcessImport is reading the CSV file by doing a File.read on the import file path.

def scan
      data = CSV.parse(
        File.read(@import.file.path),
        headers: true,
        encoding: 'UTF-8',
        header_converters: ->(h) { h.strip }
      )
      prepare_rows(data)
end

However, on some projects that path may not be available depending on which customizations have been applied to Paperclip or ActiveStorage. On a project I'm working on the files are sent directly to AWS, even on development, and their bucket path is interpolated in such a way that the @import.file local path can't be determined.

Therefore it would be useful to allow user-customizable strategies for reading the content from the file. E.g.:

SolidusImporter.config do |c|
  c.import_file_reader = -> (import) {
     open(@import.file.public_url)
  }
end

def scan
      raw_content = SolidusImporter::Config.import_file_reader.call(@import)
      data = CSV.parse(
        raw_content,
        headers: true,
        encoding: 'UTF-8',
        header_converters: ->(h) { h.strip }
      )
      prepare_rows(data)
end

Or any other approach may also be viable as long as we allow this to me easily customizable.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It might be closed if no further activity occurs. Thank you for your contributions.