Class: ActiveModel::EachValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/no_brainer/document/validation/core.rb

Instance Method Summary collapse

Instance Method Details

#should_validate_field?(record, attribute) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/no_brainer/document/validation/core.rb', line 44

def should_validate_field?(record, attribute)
  return true unless record.is_a?(NoBrainer::Document)
  return true if record.new_record?

  attr_changed = "#{attribute}_changed?"
  return record.respond_to?(attr_changed) ? record.__send__(attr_changed) : true
end

#validate(record) ⇒ Object

XXX Monkey Patching :(



53
54
55
56
57
58
59
60
61
# File 'lib/no_brainer/document/validation/core.rb', line 53

def validate(record)
  attributes.each do |attribute|
    next unless should_validate_field?(record, attribute) # <--- Added
    value = record.read_attribute_for_validation(attribute)
    next if value.is_a?(NoBrainer::Document::AtomicOps::PendingAtomic) # <--- Added
    next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
    validate_each(record, attribute, value)
  end
end