Navigation Menu

Skip to content

Commit

Permalink
Rescue invalid ip address exceptions on assign.
Browse files Browse the repository at this point in the history
In order that set attribute should not be bang method
  • Loading branch information
pftg committed Aug 14, 2013
1 parent 41f50be commit 0aa95a7
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 * `add_to_target` now accepts a second optional `skip_callbacks` argument


If truthy, it will skip the :before_add and :after_add callbacks. 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? if string.nil?
nil nil
elsif String === string elsif String === string
IPAddr.new(string) begin
IPAddr.new(string)
rescue ArgumentError
nil
end
else else
string string
end 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 } assert_raise(ActiveRecord::StatementInvalid) { assert @first_bit_string.save }
end 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 def test_update_oid
new_value = 567890 new_value = 567890
assert @first_oid.obj_id = new_value assert @first_oid.obj_id = new_value
Expand Down

0 comments on commit 0aa95a7

Please sign in to comment.