mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Make nested attributes behave like in 2.3.5 and add a sanity test for it with I18n.
This commit is contained in:
@@ -267,7 +267,7 @@ module ActiveRecord
|
||||
unless valid = association.valid?
|
||||
if reflection.options[:autosave]
|
||||
association.errors.each do |attribute, message|
|
||||
attribute = "#{reflection.name}_#{attribute}"
|
||||
attribute = "#{reflection.name}.#{attribute}"
|
||||
errors[attribute] << message if errors[attribute].empty?
|
||||
end
|
||||
else
|
||||
|
||||
@@ -786,14 +786,14 @@ class TestAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase
|
||||
def test_should_automatically_validate_the_associated_model
|
||||
@pirate.ship.name = ''
|
||||
assert @pirate.invalid?
|
||||
assert @pirate.errors[:ship_name].any?
|
||||
assert @pirate.errors[:"ship.name"].any?
|
||||
end
|
||||
|
||||
def test_should_merge_errors_on_the_associated_models_onto_the_parent_even_if_it_is_not_valid
|
||||
@pirate.ship.name = nil
|
||||
@pirate.catchphrase = nil
|
||||
assert @pirate.invalid?
|
||||
assert @pirate.errors[:ship_name].any?
|
||||
assert @pirate.errors[:"ship.name"].any?
|
||||
assert @pirate.errors[:catchphrase].any?
|
||||
end
|
||||
|
||||
@@ -886,7 +886,7 @@ class TestAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
|
||||
def test_should_automatically_validate_the_associated_model
|
||||
@ship.pirate.catchphrase = ''
|
||||
assert @ship.invalid?
|
||||
assert @ship.errors[:pirate_catchphrase].any?
|
||||
assert @ship.errors[:"pirate.catchphrase"].any?
|
||||
end
|
||||
|
||||
def test_should_merge_errors_on_the_associated_model_onto_the_parent_even_if_it_is_not_valid
|
||||
@@ -894,7 +894,7 @@ class TestAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
|
||||
@ship.pirate.catchphrase = nil
|
||||
assert @ship.invalid?
|
||||
assert @ship.errors[:name].any?
|
||||
assert @ship.errors[:pirate_catchphrase].any?
|
||||
assert @ship.errors[:"pirate.catchphrase"].any?
|
||||
end
|
||||
|
||||
def test_should_still_allow_to_bypass_validations_on_the_associated_model
|
||||
@@ -961,7 +961,7 @@ module AutosaveAssociationOnACollectionAssociationTests
|
||||
@pirate.send(@association_name).each { |child| child.name = '' }
|
||||
|
||||
assert !@pirate.valid?
|
||||
assert_equal ["can't be blank"], @pirate.errors["#{@association_name}_name"]
|
||||
assert_equal ["can't be blank"], @pirate.errors["#{@association_name}.name"]
|
||||
assert @pirate.errors[@association_name].empty?
|
||||
end
|
||||
|
||||
@@ -969,16 +969,32 @@ module AutosaveAssociationOnACollectionAssociationTests
|
||||
@pirate.send(@association_name).build(:name => '')
|
||||
|
||||
assert !@pirate.valid?
|
||||
assert_equal ["can't be blank"], @pirate.errors["#{@association_name}_name"]
|
||||
assert_equal ["can't be blank"], @pirate.errors["#{@association_name}.name"]
|
||||
assert @pirate.errors[@association_name].empty?
|
||||
end
|
||||
|
||||
def test_should_default_invalid_error_from_i18n
|
||||
I18n.backend.store_translations(:en, :activerecord => { :errors => { :models =>
|
||||
{ @association_name.to_s.singularize.to_sym => { :blank => "cannot be blank" } }
|
||||
}})
|
||||
|
||||
@pirate.send(@association_name).build(:name => '')
|
||||
|
||||
assert !@pirate.valid?
|
||||
assert_equal ["cannot be blank"], @pirate.errors["#{@association_name}.name"]
|
||||
assert @pirate.errors[@association_name].empty?
|
||||
ensure
|
||||
I18n.backend.store_translations(:en, :activerecord => { :errors => { :models =>
|
||||
{ @association_name.to_s.singularize.to_sym => nil }
|
||||
}})
|
||||
end
|
||||
|
||||
def test_should_merge_errors_on_the_associated_models_onto_the_parent_even_if_it_is_not_valid
|
||||
@pirate.send(@association_name).each { |child| child.name = '' }
|
||||
@pirate.catchphrase = nil
|
||||
|
||||
assert !@pirate.valid?
|
||||
assert_equal ["can't be blank"], @pirate.errors["#{@association_name}_name"]
|
||||
assert_equal ["can't be blank"], @pirate.errors["#{@association_name}.name"]
|
||||
assert @pirate.errors[:catchphrase].any?
|
||||
end
|
||||
|
||||
|
||||
@@ -592,7 +592,7 @@ module NestedAttributesOnACollectionAssociationTests
|
||||
assert_no_difference ['Man.count', 'Interest.count'] do
|
||||
man = Man.create(:name => 'John',
|
||||
:interests_attributes => [{:topic=>'Cars'}, {:topic=>'Sports'}])
|
||||
assert !man.errors[:interests_man].empty?
|
||||
assert !man.errors[:"interests.man"].empty?
|
||||
end
|
||||
end
|
||||
# restore :inverse_of
|
||||
|
||||
Reference in New Issue
Block a user