1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Raising an error when nil or non-hash is passed to update_attributes.

This commit is contained in:
wangjohn 2013-03-21 17:10:17 -04:00
parent 9218770daa
commit 926c4b95e4
4 changed files with 25 additions and 6 deletions

View file

@ -1,3 +1,13 @@
* Calling `update_attributes` will now throw an `ArgumentError` whenever it
gets a `nil` argument. More specifically, it will throw an error if the
argument that it gets passed does not respond to to `stringify_keys`.
`Example:`
@my_comment.update_attributes() # => raises ArgumentError
*John Wang*
* Remove Oracle / Sqlserver / Firebird database tasks that were deprecated in 4.0.
*kennyj*

View file

@ -13,7 +13,9 @@ module ActiveRecord
# of this method is +false+ an <tt>ActiveModel::ForbiddenAttributesError</tt>
# exception is raised.
def assign_attributes(new_attributes)
return if new_attributes.blank?
if !new_attributes.respond_to?(:stringify_keys)
raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
end
attributes = new_attributes.stringify_keys
multi_parameter_attributes = []

View file

@ -92,7 +92,7 @@ class AttributeMethodsTest < ActiveRecord::TestCase
def test_set_attributes_without_hash
topic = Topic.new
assert_nothing_raised { topic.attributes = '' }
assert_raise(ArgumentError) { topic.attributes = '' }
end
def test_integers_as_nil

View file

@ -419,10 +419,6 @@ class PersistenceTest < ActiveRecord::TestCase
assert !Topic.find(1).approved?
end
def test_update_attribute_does_not_choke_on_nil
assert Topic.find(1).update(nil)
end
def test_update_attribute_for_readonly_attribute
minivan = Minivan.find('m1')
assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_attribute(:color, 'black') }
@ -701,6 +697,17 @@ class PersistenceTest < ActiveRecord::TestCase
assert_equal topic.title, Topic.find(1234).title
end
def test_update_attributes_parameters
topic = Topic.find(1)
assert_nothing_raised do
topic.update_attributes({})
end
assert_raises(ArgumentError) do
topic.update_attributes(nil)
end
end
def test_update!
Reply.validates_presence_of(:title)
reply = Reply.find(2)