module Reactor::Validations::ClassMethods

Public Instance Methods

inherited(subclass) click to toggle source
Calls superclass method
# File lib/reactor/validations.rb, line 31
def inherited(subclass)
  super(subclass) # if you remove this line, y'll get TypeError: can't dup NilClass at some point

  # Add validation for each mandatory attribute
  mandatory_attrs = __mandatory_cms_attributes(subclass.name)
  mandatory_attrs.each  do |attr|
    subclass.send(:validates_presence_of, attr.to_sym, :on => :release)
  end if mandatory_attrs

  cms_attributes  = __cms_attributes(subclass).values
  # Add validation for linklist & multienum [minSize/maxSize]
  array_attributes= cms_attributes.select {|attr| ["linklist", "multienum"].include?(attr.attribute_type) }
  array_attributes.each do |attr|
    length_hash = {}
    length_hash[:minimum] = attr.min_size if attr.min_size && "linklist" != attr.attribute_type # CMS ignores minimum for linklists.
    length_hash[:maximum] = attr.max_size if attr.max_size
  
    subclass.send(:validates, attr.attribute_name.to_sym, :length => length_hash, :on => :release) unless length_hash.empty?
  end

  subclass
end