Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #11574 from jetthoughts/11552_rescue_on_invalid_in…
…et_assign

Fix assign ip address with invalid values raise exception
  • Loading branch information
senny committed Aug 14, 2013
2 parents f04b75f + 0aa95a7 commit a4d4af4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
18 changes: 18 additions & 0 deletions 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.
Expand Down
Expand Up @@ -100,7 +100,11 @@ def string_to_cidr(string)
if string.nil?
nil
elsif String === string
IPAddr.new(string)
begin
IPAddr.new(string)
rescue ArgumentError
nil
end
else
string
end
Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/adapters/postgresql/datatype_test.rb
Expand Up @@ -558,6 +558,20 @@ def test_invalid_hex_string
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
Expand Down

0 comments on commit a4d4af4

Please sign in to comment.