Class: TrackingNumberValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/tracking_number/active_model_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/tracking_number/active_model_validator.rb', line 2

def validate_each(record, attribute, value)
  return unless value.present?
  if options[:exception] == value || options[:except] == value
    # magic valid value (an exception that says "not really shipped" or something)
  elsif TrackingNumber.new(value).valid?
    # looks good to me
  else
    record.errors[attribute] << options[:message] || 'is not a valid tracking number'
  end
end