Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong behavior of ActiveModel::Errors#dup is causing regressions on applications using Rails 3-2-stable #4492

Merged
merged 1 commit into from Jan 17, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion activemodel/lib/active_model/errors.rb
Expand Up @@ -79,6 +79,19 @@ def initialize(base)
@messages = ActiveSupport::OrderedHash.new @messages = ActiveSupport::OrderedHash.new
end end


def initialize_dup(other)
@messages = other.messages.dup
end

# Backport dup from 1.9 so that #initialize_dup gets called
unless Object.respond_to?(:initialize_dup)
def dup # :nodoc:
copy = super
copy.initialize_dup(self)
copy
end
end

# Clear the messages # Clear the messages
def clear def clear
messages.clear messages.clear
Expand Down Expand Up @@ -119,7 +132,7 @@ def [](attribute)
# p.errors[:name] = "must be set" # p.errors[:name] = "must be set"
# p.errors[:name] # => ['must be set'] # p.errors[:name] # => ['must be set']
def []=(attribute, error) def []=(attribute, error)
self[attribute.to_sym] << error self[attribute] << error
end end


# Iterates through each error key, value pair in the error messages hash. # Iterates through each error key, value pair in the error messages hash.
Expand Down
8 changes: 8 additions & 0 deletions activemodel/test/cases/errors_test.rb
Expand Up @@ -46,6 +46,14 @@ def test_has_key?
assert errors.has_key?(:foo), 'errors should have key :foo' assert errors.has_key?(:foo), 'errors should have key :foo'
end end


def test_dup
errors = ActiveModel::Errors.new(self)
errors[:foo] = 'bar'
errors_dup = errors.dup
errors_dup[:bar] = 'omg'
assert_not_same errors_dup.messages, errors.messages
end

test "should return true if no errors" do test "should return true if no errors" do
person = Person.new person = Person.new
person.errors[:foo] person.errors[:foo]
Expand Down