2013-10-22 16:30:30 -04:00
|
|
|
# Allows unique IP address (10.0.1.20), and IP addresses with a mask (10.0.0.0/8).
|
|
|
|
# Useful when storing in a Postgresql inet column.
|
|
|
|
class IpAddressFormatValidator < ActiveModel::EachValidator
|
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
2013-10-22 18:55:30 -04:00
|
|
|
if rails4?
|
2013-11-05 11:24:13 -05:00
|
|
|
# In Rails 4.0.0, ip_address will be nil if an invalid IP address was assigned.
|
2013-10-22 18:55:30 -04:00
|
|
|
# https://github.com/jetthoughts/rails/commit/0aa95a71b04f2893921c58a7c1d9fca60dbdcbc2
|
2013-11-05 11:24:13 -05:00
|
|
|
|
|
|
|
# BUT: in Rails 4.0.1, validators don't get a chance to
|
|
|
|
# run before IPAddr::InvalidAddressError is raised.
|
|
|
|
# I don't see what broke it in rails 4.0.1...
|
|
|
|
# So this validator doesn't actually do anything anymore.
|
|
|
|
# But let's keep it in case a future version of rails fixes the problem and allows
|
|
|
|
# validators to work on inet and cidr columns.
|
2013-10-22 18:55:30 -04:00
|
|
|
if record.ip_address.nil?
|
|
|
|
record.errors.add(attribute, :invalid)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
unless !record.ip_address.nil? and record.ip_address.to_s.split('/').first =~ Resolv::AddressRegex
|
|
|
|
record.errors.add(attribute, :invalid)
|
|
|
|
end
|
2013-10-22 16:30:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|