Class: UrlValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/url_attribute/url_validator.rb

Overview

global namespace so you can use the rails 3 “validate :field, email: [options]”

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/url_attribute/url_validator.rb', line 3

def validate_each(record,attribute,value)
  return if value.blank? && options[:allow_blank]

  case value
  when UrlAttribute::Url
    record.errors.add(attribute, options.fetch(:message, :invalid)) unless value.valid?
  when String
    record.errors.add(attribute, options.fetch(:message, :invalid)) unless UrlAttribute::Url.load(value).valid?
  end
end