mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Ensure assign_attributes and update_attributes do not fail on nil, closes #478.
This commit is contained in:
@@ -1688,6 +1688,8 @@ MSG
|
||||
# user.name # => "Josh"
|
||||
# user.is_admin? # => true
|
||||
def assign_attributes(new_attributes, options = {})
|
||||
return unless new_attributes
|
||||
|
||||
attributes = new_attributes.stringify_keys
|
||||
role = options[:as] || :default
|
||||
|
||||
|
||||
@@ -87,6 +87,10 @@ class MassAssignmentSecurityTest < ActiveRecord::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
def test_mass_assigning_does_not_choke_on_nil
|
||||
Firm.new.assign_attributes(nil)
|
||||
end
|
||||
|
||||
def test_assign_attributes_uses_default_role_when_no_role_is_provided
|
||||
p = LoosePerson.new
|
||||
p.assign_attributes(attributes_hash)
|
||||
|
||||
@@ -336,6 +336,10 @@ class PersistencesTest < ActiveRecord::TestCase
|
||||
assert !Topic.find(1).approved?
|
||||
end
|
||||
|
||||
def test_update_attribute_does_not_choke_on_nil
|
||||
assert Topic.find(1).update_attributes(nil)
|
||||
end
|
||||
|
||||
def test_update_attribute_for_readonly_attribute
|
||||
minivan = Minivan.find('m1')
|
||||
assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_attribute(:color, 'black') }
|
||||
|
||||
Reference in New Issue
Block a user