Add autosave tests for association with error

Pulled from #48413

"Fix autosave associations with validations added on `:base` of the associated objects"

Co-authored-by: fatkodima <fatkodima123@gmail.com>
This commit is contained in:
zzak 2023-08-02 18:57:03 +09:00 committed by Rafael Mendonça França
parent b2c53f8f9f
commit f5f9446478
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948

@ -570,6 +570,37 @@ def test_errors_details_should_be_indexed_when_passed_as_array
assert_equal [], guitar.errors.details[:"tuning_pegs.pitch"]
end
def test_errors_details_with_error_on_base_should_be_indexed_when_passed_as_array
reference = Class.new(ActiveRecord::Base) do
self.table_name = "references"
def self.name; "Reference"; end
validate :should_be_favorite
private
def should_be_favorite
errors.add(:base, "should be favorite") unless favorite?
end
end
person = Class.new(ActiveRecord::Base) do
self.table_name = "people"
has_many :references, autosave: true, index_errors: true, anonymous_class: reference
def self.name; "Person"; end
end
p = person.new
reference_valid = reference.new(favorite: true)
reference_invalid = reference.new(favorite: false)
p.references = [reference_valid, reference_invalid]
assert_predicate reference_valid, :valid?
assert_not_predicate reference_invalid, :valid?
assert_not_predicate p, :valid?
assert_equal [{ error: "should be favorite" }], p.errors.details[:"references[1].base"]
assert_equal "should be favorite", p.errors[:"references[1].base"].first
end
def test_errors_details_should_be_indexed_when_global_flag_is_set
old_attribute_config = ActiveRecord.index_nested_attribute_errors
ActiveRecord.index_nested_attribute_errors = true