Class: Gemgento::InventoryImportValidator
- Inherits:
-
ActiveModel::Validator
- Object
- ActiveModel::Validator
- Gemgento::InventoryImportValidator
- Defined in:
- app/validators/gemgento/inventory_import_validator.rb
Instance Method Summary collapse
- #validate(record) ⇒ Object
- #validate_attributes ⇒ Object
- #validate_required_attributes ⇒ Object
- #validate_sku ⇒ Object
Instance Method Details
#validate(record) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'app/validators/gemgento/inventory_import_validator.rb', line 4 def validate(record) @record = record begin @record.spreadsheet rescue Exception => e Rails.logger.error e. @record.errors[:file] = 'Invalid Spreadsheet' return end validate_required_attributes validate_sku validate_attributes end |
#validate_attributes ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/validators/gemgento/inventory_import_validator.rb', line 55 def validate_attributes errors = [] @record.header_row.each do |attribute| next if attribute == 'sku' errors << attribute unless Gemgento::Inventory.column_names.include? attribute end unless errors.empty? error = '<b>The following inventory attributes could not be found:</b><br /><ul><li>' error += errors.join('</li><li>') error += '</li></ul>' @record.errors[:file]= "<div>#{error}</div>" end end |
#validate_required_attributes ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/validators/gemgento/inventory_import_validator.rb', line 20 def validate_required_attributes errors = [] %w[sku quantity manage_stock is_in_stock].each do |attribute| errors << attribute unless @record.header_row.include? attribute end unless errors.empty? error = '<b>The following required attributes could not be found:</b><br /><ul><li>' error += errors.join('</li><li>') error += '</li></ul>' @record.errors[:file]= "<div>#{error}</div>" end end |
#validate_sku ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/validators/gemgento/inventory_import_validator.rb', line 35 def validate_sku errors = [] @record.content_index_range.each do |index| row = @record.spreadsheet.row(index) sku = row[@record.header_row.index('sku').to_i].to_s.strip next if sku.blank? errors << sku unless product = Gemgento::Product.not_deleted.find_by(sku: sku) end unless errors.empty? error = '<b>The following SKU(s) could not be found:</b><br /><ul><li>' error += errors.join('</li><li>') error += '</li></ul>' @record.errors[:file]= "<div>#{error}</div>" end end |