class LocationImport < ScaffoldLogic::DataImport def self.save(upload) # create file path _path = File.join('tmp', upload['datafile'].original_filename) # write file File.open(_path, 'wb') { |f| f.write(upload['datafile'].read) } self.run(_path) end def self.test(path='public/sample-locations.xls') self.run(path) end def process_row @required_fields ||= [:'location #'] if valid_row? && _location = Location.find_or_initialize_by(:location_number => row_map[@required_fields.first]) COLUMNS_BY_MODEL_FIELDS.keys.each do |_field| _content = eval(COLUMNS_BY_MODEL_FIELDS[_field]) _location.send("#{_field}=", _content) unless _content.blank? end _location.validate_address! _location.localize sleep 0.33 else @errors << "Row #{@row_index}: Missing at least one required field: #{@required_fields * ', '}!" end end protected COLUMNS_BY_MODEL_FIELDS = { 'location_number' => 'row_map[:"location #"]', 'name' => 'row_map[:"name"]', 'address1' => 'row_map[:"address 1"]', 'address2' => 'row_map[:"address 2"]', 'city' => 'row_map[:"city"]', 'state' => 'row_map[:"state"]', 'zip' => 'row_map[:"postal code"]', 'country' => 'row_map[:"country code"]', 'phone' => 'row_map[:"phone"]', 'email' => 'row_map[:"email"]', 'meta_description' => 'row_map[:"description"]' } end