lib/spree_importer/importer.rb
# Load a file and the get data from each file row
Load a file and the get data from each file row
Load a file and the get data from each file row
def load_products
      puts I18n.t(:reading, scope: [:spree, :spree_importer, :logs], filename: @filename) if Spree::Config.verbose
      start = Time.now
      begin
        open_spreadsheet
      rescue RuntimeError => e
        return e.message
      end
      puts I18n.t(:loading_file, scope: [:spree, :spree_importer, :logs], filename: @filename, time: Time.now - start) if Spree::Config.verbose
      start = Time.now
      failed_rows = []
      2.upto(@spreadsheet.last_row).each do |row_index|
        Spree::Product.transaction do
          begin
            if Spree::Config.skip_if_product_exists and @product_identifier.exists?(@spreadsheet, row_index)
              puts I18n.t(:already_exists, scope: [:spree, :spree_importer, :logs], filename: @filename, row: row_index, rows: @spreadsheet.last_row, data: @spreadsheet.row(row_index))
              next
            end
            data = default_hash.deep_dup
            @mappers.each do |mapper|
              mapper.load @spreadsheet, row_index, data
            end
            before_make data
            make_products data
            make_variants data
            make_aditionals data
            after_make data
            if Spree::Config.verbose and row_index % Spree::Config[:log_progress_every] == 0
              puts I18n.t(:progress, scope: [:spree, :spree_importer, :logs], filename: @filename, time: Time.now - start, row: row_index, rows: @spreadsheet.last_row, data: data)
              start = Time.now
            end
          rescue RuntimeError => e
            puts I18n.t(:error, scope: [:spree, :spree_importer, :logs], filename: @filename, row: row_index, rows: @spreadsheet.last_row, data: data.inspect, message: e.message) if Spree::Config.verbose
            failed_rows << {row_index: row_index, message: e.message, data: data, backtrace: e.backtrace}
            raise ActiveRecord::Rollback
          rescue => e
            puts I18n.t(:error, scope: [:spree, :spree_importer, :logs], filename: @filename, row: row_index, rows: @spreadsheet.last_row, data: data.inspect, message: e.message) if Spree::Config.verbose
            failed_rows << {row_index: row_index, message: e.message, data: data, backtrace: e.backtrace}
            raise ActiveRecord::Rollback
          ensure
          end
        end
      end
      puts I18n.t(:done, scope: [:spree, :spree_importer, :logs], filename: @filename) if Spree::Config.verbose
      if failed_rows.empty?
        NotificationMailer.successfully(@filename).deliver
      else
        NotificationMailer.error(@filename, failed_rows).deliver
      end
    end
def load_products puts I18n . t ( :reading , scope : [ :spree , :spree_importer , :logs ] , filename : @filename ) if Spree :: Config . verbose start = Time . now begin open_spreadsheet rescue RuntimeError => e return e . message end puts I18n . t ( :loading_file , scope : [ :spree , :spree_importer , :logs ] , filename : @filename , time : Time . now - start ) if Spree :: Config . verbose start = Time . now failed_rows = [ ] 2 . upto ( @spreadsheet . last_row ) . each do | row_index | Spree :: Product . transaction do begin if Spree :: Config . skip_if_product_exists and @product_identifier . exists? ( @spreadsheet , row_index ) puts I18n . t ( :already_exists , scope : [ :spree , :spree_importer , :logs ] , filename : @filename , row : row_index , rows : @spreadsheet . last_row , data : @spreadsheet . row ( row_index ) ) next end data = default_hash . deep_dup @mappers . each do | mapper | mapper . load @spreadsheet , row_index , data end before_make data make_products data make_variants data make_aditionals data after_make data if Spree :: Config . verbose and row_index % Spree :: Config [ :log_progress_every ] == 0 puts I18n . t ( :progress , scope : [ :spree , :spree_importer , :logs ] , filename : @filename , time : Time . now - start , row : row_index , rows : @spreadsheet . last_row , data : data ) start = Time . now end rescue RuntimeError => e puts I18n . t ( :error , scope : [ :spree , :spree_importer , :logs ] , filename : @filename , row : row_index , rows : @spreadsheet . last_row , data : data . inspect , message : e . message ) if Spree :: Config . verbose failed_rows << { row_index : row_index , message : e . message , data : data , backtrace : e . backtrace } raise ActiveRecord :: Rollback rescue => e puts I18n . t ( :error , scope : [ :spree , :spree_importer , :logs ] , filename : @filename , row : row_index , rows : @spreadsheet . last_row , data : data . inspect , message : e . message ) if Spree :: Config . verbose failed_rows << { row_index : row_index , message : e . message , data : data , backtrace : e . backtrace } raise ActiveRecord :: Rollback ensure end end end puts I18n . t ( :done , scope : [ :spree , :spree_importer , :logs ] , filename : @filename ) if Spree :: Config . verbose if failed_rows . empty? NotificationMailer . successfully ( @filename ) . deliver else NotificationMailer . error ( @filename , failed_rows ) . deliver end end
Load a file and the get data from each file row
Load a file and the get data from each file row
# ToDo - Corregir # Load each row element