Class: Gemgento::ProductImportValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/validators/gemgento/product_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
20
21
22
# File 'app/validators/gemgento/product_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_unique_skus
  validate_required_attribute_codes
  validate_valid_attribute_codes
  validate_categories
  validate_images
end

#validate_categoriesObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/validators/gemgento/product_import_validator.rb', line 92

def validate_categories
  errors = []

  @record.content_index_range.each do |index|
    row = @record.spreadsheet.row(index)
    categories = row[@record.header_row.index('category').to_i].to_s.strip.split('&')

    categories.each do |category_string|
      category_string.strip!
      subcategories = category_string.split('>')
      parent = @record.root_category

      subcategories.each do |category_url_key|
        category_url_key.strip!
        category =  Gemgento::Category.find_by(url_key: category_url_key, parent_id: parent.id)

        if category.nil?
          errors << category_url_key unless errors.include? category_url_key
        else
          parent = category
        end
      end
    end
  end

  unless errors.empty? || errors[0] == ""
    error = '<b>The following category url key(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_imagesObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/validators/gemgento/product_import_validator.rb', line 126

def validate_images
  return unless @record.include_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? || errors[0] == ""
    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_required_attribute_codesObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/validators/gemgento/product_import_validator.rb', line 50

def validate_required_attribute_codes
  errors = []
  required_attributes = %w[magento_type sku status weight]
  (required_attributes << @record.configurable_attributes.pluck(:code)).flatten!
  required_attributes << 'image' if @record.include_images?

  required_attributes.each do |attribute_code|
    if attribute_code != '' && !attribute_code.nil? && !@record.header_row.include?(attribute_code)
      errors << attribute_code
    end
  end

  unless errors.empty?
    error = '<b>The following required attribute code(s) were not found:</b><br /><ul><li>'
    error += errors.join('</li><li>')
    error += '</li></ul>'

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

#validate_unique_skusObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/validators/gemgento/product_import_validator.rb', line 24

def validate_unique_skus
  errors = []
  skus = []

  @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? # skip blank skus

    if skus.include? sku
      errors << sku
    else
      skus << sku
    end
  end

  unless errors.empty?
    error = '<b>The following SKU(s) are not unique:</b><br /><ul><li>'
    error += errors.join('</li><li>')
    error += '</li></ul>'

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

#validate_valid_attribute_codesObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/validators/gemgento/product_import_validator.rb', line 71

def validate_valid_attribute_codes
  errors = []
  default_attributes = %w[magento_type sku status image visibility category]

  @record.header_row.each do |attribute_code|
    next if default_attributes.include? attribute_code

    unless @record.product_attribute_set.product_attributes.exists?(code: attribute_code)
      errors << attribute_code
    end
  end

  unless errors.empty?
    error = '<b>The following attribute code(s) are unknown:</b><br /><ul><li>'
    error += errors.join('</li><li>')
    error += '</li></ul>'

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