Class: UrlValidator

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

Instance Method Summary collapse

Instance Method Details

#url_valid?(url) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
# File 'lib/trax/validators/url_validator.rb', line 6

def url_valid?(url)
  url = URI.parse(url) rescue false
  url.kind_of?(URI::HTTP) || url.kind_of?(URI::HTTPS)
end

#validate_each(record, attribute, value) ⇒ Object



2
3
4
# File 'lib/trax/validators/url_validator.rb', line 2

def validate_each(record, attribute, value)
  record.errors[attribute] << (options[:message] || "must be a valid URL") unless url_valid?(value)
end