Class: Fulcrum::ClassificationSetValidator

Inherits:
BaseValidator show all
Defined in:
lib/fulcrum/validators/classification_set_validator.rb

Instance Attribute Summary

Attributes inherited from BaseValidator

#data, #errors

Instance Method Summary collapse

Methods inherited from BaseValidator

#add_error, #initialize, #valid?

Constructor Details

This class inherits a constructor from Fulcrum::BaseValidator

Instance Method Details

#items(elements, child = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fulcrum/validators/classification_set_validator.rb', line 19

def items(elements, child = false)
  parent, child = child ? ['child_classification', 'item'] : ['items', 'item']
  if elements.kind_of?(Array) && !elements.empty?
    elements.each do |element|
      if element.blank?
        add_error(parent, child, 'cannot be empty')
      else
        if element['label'].blank?
          add_error(child, 'label', 'is required')
        end
      end
      items(element['child_classifications'], true) if element.has_key?('child_classifications')
    end
  else
    add_error(parent, child, 'must be a non-empty array')
  end
end

#validate!Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/fulcrum/validators/classification_set_validator.rb', line 4

def validate!
  if data['classification_set']
    add_error('classification_set', 'name', 'cannot be blank') if data['classification_set']['name'].blank?

    if data['classification_set']['items'].blank? || !data['classification_set']['items'].kind_of?(Array)
      add_error('classification_set', 'items', 'must be a non-empty array')
    else
      items(data['classification_set']['items'])
    end
  else
    @errors['classification_set'] = ['must exist and not be blank']
  end
  return valid?
end