diff --git a/app/models/screened_ip_address.rb b/app/models/screened_ip_address.rb index 219976895..332fa2dde 100644 --- a/app/models/screened_ip_address.rb +++ b/app/models/screened_ip_address.rb @@ -14,9 +14,9 @@ class ScreenedIpAddress < ActiveRecord::Base match_for_ip_address(ip_address) || create(opts.slice(:action_type).merge(ip_address: ip_address)) end - # @Neil please review, in rails 4 when setting an ip address attribute a conversion takes place - # this may explode meaning you will never even reach the validator - # We can work around the issue like so, but I wonder if the spec is valid + # In Rails 4.0.0, validators are run to handle invalid assignments to inet columns (as they should). + # In Rails 4.0.1, an exception is raised before validation happens, so we need this hack for + # inet/cidr columns: def ip_address=(val) write_attribute(:ip_address, val) rescue IPAddr::InvalidAddressError diff --git a/lib/validators/ip_address_format_validator.rb b/lib/validators/ip_address_format_validator.rb index 460eda40f..ab7917759 100644 --- a/lib/validators/ip_address_format_validator.rb +++ b/lib/validators/ip_address_format_validator.rb @@ -4,8 +4,15 @@ class IpAddressFormatValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) if rails4? - # In Rails 4, ip_address will be nil if an invalid IP address was assigned. + # In Rails 4.0.0, ip_address will be nil if an invalid IP address was assigned. # https://github.com/jetthoughts/rails/commit/0aa95a71b04f2893921c58a7c1d9fca60dbdcbc2 + + # 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. if record.ip_address.nil? record.errors.add(attribute, :invalid) end