Permalink
Browse files

Merge pull request #9860 from wangjohn/update_attributes_throws_error…

…_with_nil

Raising an error when nil is passed to update_attributes.

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
2 parents e029e02 + 926c4b9 commit 16c7873c44cbced87b153bcbc7ac7ecfe950b53c @rafaelfranca rafaelfranca committed Sep 24, 2013
View
@@ -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(nil) # => raises ArgumentError
+
+ *John Wang*
+
* Deprecate `quoted_locking_column` method, which isn't used anywhere.
*kennyj*
@@ -12,7 +12,9 @@ module AttributeAssignment
# 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 = []
@@ -92,7 +92,7 @@ def test_set_attributes
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
@@ -419,10 +419,6 @@ def test_update_attribute
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 @@ def test_update_attributes
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)

0 comments on commit 16c7873

Please sign in to comment.