Fixes for postgresql inet columns in Rails 4. They're backed by an IPAddr class now, which breaks sql parameter marker support, and automatically sets the attribute to nil when trying to assign an invalid ip address.

This commit is contained in:
Neil Lalonde
2013-10-22 18:55:30 -04:00
parent 6394d924c8
commit c1008f4359
5 changed files with 21 additions and 7 deletions

View File

@ -3,8 +3,16 @@
class IpAddressFormatValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless record.ip_address.nil? or record.ip_address.split('/').first =~ Resolv::AddressRegex
record.errors.add(attribute, :invalid)
if rails4?
# In Rails 4, ip_address will be nil if an invalid IP address was assigned.
# https://github.com/jetthoughts/rails/commit/0aa95a71b04f2893921c58a7c1d9fca60dbdcbc2
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
end
end