Class: Gemgento::ImageImportValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/validators/gemgento/image_import_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/validators/gemgento/image_import_validator.rb', line 4

def validate(record)
  @record = record

  begin
    @record.spreadsheet
  rescue Exception => e
    Rails.logger.error e.message
    @record.errors[:file] = 'Invalid Spreadsheet'
    return
  end

  @record.set_total_rows
  validate_sku
  validate_images
  validate_image_types
end

#validate_image_typesObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/validators/gemgento/image_import_validator.rb', line 78

def validate_image_types
  errors = []

  @record.image_types.each do |types|
    next if types.empty?

    types.each do |type|
      asset_type = Gemgento::AssetType.find_by(code: type)
      errors << type if asset_type.nil?
    end
  end

  unless errors.empty?
    error = '<b>The following image type(s) could not be found:</b><br /><ul><li>'
    error += errors.join('</li><li>')
    error += '</li></ul>'

    @record.errors[:image_types_raw] << "<div>#{error}</div>"
  end
end

#validate_imagesObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/validators/gemgento/image_import_validator.rb', line 43

def validate_images
  errors = []

  @record.content_index_range.each do |index|
    row = @record.spreadsheet.row(index)
    file_name_base = row[@record.header_row.index('image').to_i].to_s.strip
    images_found = false

    @record.image_labels.each do |label|

      @record.image_file_extensions.each do |extension|
        file_name = @record.image_path + file_name_base + '_' + label + extension
        next unless File.exist?(file_name)

        images_found = true
        break
      end

      break if images_found
    end

    unless images_found
      errors << file_name_base unless errors.include? file_name_base
    end
  end

  unless errors.empty?
    error = '<b>The following image(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

#validate_skuObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/validators/gemgento/image_import_validator.rb', line 21

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?

    if Gemgento::Product.unscoped.not_deleted.find_by(sku: sku).nil?
      errors << sku
    end
  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