pcreux / csv-importer

CSV Import for humans on Ruby / Ruby on Rails
MIT License
590 stars 67 forks source link

Add Custom Cell Sanitize #77

Open joel opened 5 years ago

joel commented 5 years ago

Custom cell sanitising

By default cell values are stripped, that means " Bob Elvis " become "Bob Elvis", however you may want to go further and get rid of the inner extra spaces that often present on cell value and get the proper "Bob Elvis" To do that, add an initialiser config/initializers/csv_importer.rb and define you proper rules.

require 'csv_importer'
module CSVImporter
  def self.sanitize_cell(raw_value)
    raw_value.to_s
      .gsub(/[\b\s\u00A0]+/, ' ') # Normalize white space
      .strip # Remove trailing white space
  end
end
joel commented 5 years ago

Plus + You can create general behaviour here:

require 'csv_importer'
module CSVImporter
  def self.sanitize_cell(raw_value)
    if %(y yes t true 1).inlcude?(raw_value)
      'YES'
    else 
      raw_value
    end 
  end
end
joel commented 5 years ago

@pcreux sure, some source of inspiration https://github.com/s12chung/csv_row_model#attribute-values ;-)