diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-11-23 14:58:05 -0700 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-11-23 14:58:05 -0700 |
commit | de9b870f4026ce0eb40bf2a7dacbd137700910ef (patch) | |
tree | c708ceaedd5dbcf24f85e9ec187a10be6d2555fd /activemodel | |
parent | 68207cd4839d6c76d805e88aef19db5a61aa9f94 (diff) | |
parent | 85f7d955f31209605cccd4cca64be93eec9782f1 (diff) | |
download | rails-de9b870f4026ce0eb40bf2a7dacbd137700910ef.tar.gz rails-de9b870f4026ce0eb40bf2a7dacbd137700910ef.tar.bz2 rails-de9b870f4026ce0eb40bf2a7dacbd137700910ef.zip |
Merge pull request #21000 from twalpole/find_or_parameter_issues
Update and fix forbidden attributes test issues caused by AC::Parameters change
Diffstat (limited to 'activemodel')
3 files changed, 31 insertions, 6 deletions
diff --git a/activemodel/lib/active_model/attribute_assignment.rb b/activemodel/lib/active_model/attribute_assignment.rb index 087d11f708..62014cd1cd 100644 --- a/activemodel/lib/active_model/attribute_assignment.rb +++ b/activemodel/lib/active_model/attribute_assignment.rb @@ -27,7 +27,7 @@ module ActiveModel if !new_attributes.respond_to?(:stringify_keys) raise ArgumentError, "When assigning attributes, you must pass a hash as an argument." end - return if new_attributes.blank? + return if new_attributes.nil? || new_attributes.empty? attributes = new_attributes.stringify_keys _assign_attributes(sanitize_for_mass_assignment(attributes)) diff --git a/activemodel/test/cases/attribute_assignment_test.rb b/activemodel/test/cases/attribute_assignment_test.rb index 3336691841..12e007292a 100644 --- a/activemodel/test/cases/attribute_assignment_test.rb +++ b/activemodel/test/cases/attribute_assignment_test.rb @@ -23,13 +23,32 @@ class AttributeAssignmentTest < ActiveModel::TestCase class ErrorFromAttributeWriter < StandardError end - class ProtectedParams < ActiveSupport::HashWithIndifferentAccess + class ProtectedParams + attr_accessor :permitted + alias :permitted? :permitted + + delegate :keys, :key?, :has_key?, :empty?, to: :@parameters + + def initialize(attributes) + @parameters = attributes.with_indifferent_access + @permitted = false + end + def permit! @permitted = true + self + end + + def [](key) + @parameters[key] + end + + def to_h + @parameters end - def permitted? - @permitted ||= false + def stringify_keys + dup end def dup diff --git a/activemodel/test/cases/forbidden_attributes_protection_test.rb b/activemodel/test/cases/forbidden_attributes_protection_test.rb index 3cb204a2c5..d8d757f52a 100644 --- a/activemodel/test/cases/forbidden_attributes_protection_test.rb +++ b/activemodel/test/cases/forbidden_attributes_protection_test.rb @@ -2,12 +2,14 @@ require 'cases/helper' require 'active_support/core_ext/hash/indifferent_access' require 'models/account' -class ProtectedParams < ActiveSupport::HashWithIndifferentAccess +class ProtectedParams attr_accessor :permitted alias :permitted? :permitted + delegate :keys, :key?, :has_key?, :empty?, to: :@parameters + def initialize(attributes) - super(attributes) + @parameters = attributes @permitted = false end @@ -15,6 +17,10 @@ class ProtectedParams < ActiveSupport::HashWithIndifferentAccess @permitted = true self end + + def to_h + @parameters + end end class ActiveModelMassUpdateProtectionTest < ActiveSupport::TestCase |