aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2013-08-14 11:27:40 -0700
committerYves Senn <yves.senn@gmail.com>2013-08-14 11:27:40 -0700
commita4d4af4083d4b41c348f7e707874aedfa1d8a3cd (patch)
treea995d1c82062e245e4dae5ff62a71681e2329318 /activerecord
parentf04b75f5b7b07d4ad4ad58dba01a5493f3198ab6 (diff)
parent0aa95a71b04f2893921c58a7c1d9fca60dbdcbc2 (diff)
downloadrails-a4d4af4083d4b41c348f7e707874aedfa1d8a3cd.tar.gz
rails-a4d4af4083d4b41c348f7e707874aedfa1d8a3cd.tar.bz2
rails-a4d4af4083d4b41c348f7e707874aedfa1d8a3cd.zip
Merge pull request #11574 from jetthoughts/11552_rescue_on_invalid_inet_assign
Fix assign ip address with invalid values raise exception
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md18
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/cast.rb6
-rw-r--r--activerecord/test/cases/adapters/postgresql/datatype_test.rb14
3 files changed, 37 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index e8ec99511e..904ed0e362 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,21 @@
+* Assign inet/cidr attribute with `nil` value for invalid address.
+
+ Example:
+
+ record = User.new
+
+ record.logged_in_from_ip # is type of an inet or a cidr
+
+ # Before:
+ record.logged_in_from_ip = 'bad ip address' # raise exception
+
+ # After:
+ record.logged_in_from_ip = 'bad ip address' # do not raise exception
+ record.logged_in_from_ip # => nil
+ record.logged_in_from_ip_before_type_cast # => 'bad ip address'
+
+ *Paul Nikitochkin*
+
* `add_to_target` now accepts a second optional `skip_callbacks` argument
If truthy, it will skip the :before_add and :after_add callbacks.
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
index 56dc9ea813..ef7b976d4f 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
@@ -100,7 +100,11 @@ module ActiveRecord
if string.nil?
nil
elsif String === string
- IPAddr.new(string)
+ begin
+ IPAddr.new(string)
+ rescue ArgumentError
+ nil
+ end
else
string
end
diff --git a/activerecord/test/cases/adapters/postgresql/datatype_test.rb b/activerecord/test/cases/adapters/postgresql/datatype_test.rb
index 9579992bd0..75b6f4f8ce 100644
--- a/activerecord/test/cases/adapters/postgresql/datatype_test.rb
+++ b/activerecord/test/cases/adapters/postgresql/datatype_test.rb
@@ -558,6 +558,20 @@ _SQL
assert_raise(ActiveRecord::StatementInvalid) { assert @first_bit_string.save }
end
+ def test_invalid_network_address
+ @first_network_address.cidr_address = 'invalid addr'
+ assert_nil @first_network_address.cidr_address
+ assert_equal 'invalid addr', @first_network_address.cidr_address_before_type_cast
+ assert @first_network_address.save
+
+ @first_network_address.reload
+
+ @first_network_address.inet_address = 'invalid addr'
+ assert_nil @first_network_address.inet_address
+ assert_equal 'invalid addr', @first_network_address.inet_address_before_type_cast
+ assert @first_network_address.save
+ end
+
def test_update_oid
new_value = 567890
@first_oid.obj_id = new_value