-
Notifications
You must be signed in to change notification settings - Fork 21.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48727 from lulalala/24390-fix-3
Fix index_errors and provide :nested_attributes_order mode
- Loading branch information
Showing
8 changed files
with
220 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
activerecord/lib/active_record/associations/nested_error.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
# Validation error class to wrap association records' errors, | ||
# with index_errors support. | ||
module ActiveRecord | ||
module Associations | ||
class NestedError < ::ActiveModel::NestedError | ||
def initialize(association, inner_error) | ||
@base = association.owner | ||
@association = association | ||
@inner_error = inner_error | ||
super(@base, inner_error, { attribute: compute_attribute(inner_error) }) | ||
end | ||
|
||
private | ||
attr_reader :association | ||
|
||
def compute_attribute(inner_error) | ||
association_name = association.reflection.name | ||
|
||
if index_errors_setting && index | ||
"#{association_name}[#{index}].#{inner_error.attribute}".to_sym | ||
else | ||
"#{association_name}.#{inner_error.attribute}".to_sym | ||
end | ||
end | ||
|
||
def index_errors_setting | ||
@index_errors_setting ||= | ||
association.options.fetch(:index_errors, ActiveRecord.index_nested_attribute_errors) | ||
end | ||
|
||
def index | ||
@index ||= ordered_records&.find_index(inner_error.base) | ||
end | ||
|
||
def ordered_records | ||
case index_errors_setting | ||
when true # default is association order | ||
association.target | ||
when :nested_attributes_order | ||
association.nested_attributes_target | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
require "cases/helper" | ||
require "models/guitar" | ||
require "models/tuning_peg" | ||
|
||
class AssociationsNestedErrorInAssociationOrderTest < ActiveRecord::TestCase | ||
test "index in association order" do | ||
guitar = Guitar.create! | ||
guitar.tuning_pegs.create!(pitch: 1) | ||
peg2 = guitar.tuning_pegs.create!(pitch: 2) | ||
peg2.pitch = nil | ||
guitar.valid? | ||
|
||
error = guitar.errors.objects.first | ||
|
||
assert_equal ActiveRecord::Associations::NestedError, error.class | ||
assert_equal peg2.errors.objects.first, error.inner_error | ||
assert_equal :'tuning_pegs[1].pitch', error.attribute | ||
assert_equal :not_a_number, error.type | ||
assert_equal "is not a number", error.message | ||
assert_equal guitar, error.base | ||
end | ||
end | ||
|
||
class AssociationsNestedErrorInNestedAttributesOrderTest < ActiveRecord::TestCase | ||
def setup | ||
tuning_peg_class = Class.new(ActiveRecord::Base) do | ||
self.table_name = "tuning_pegs" | ||
def self.name; "TuningPeg"; end | ||
|
||
validates_numericality_of :pitch | ||
end | ||
|
||
@guitar_class = Class.new(ActiveRecord::Base) do | ||
has_many :tuning_pegs, index_errors: :nested_attributes_order, anonymous_class: tuning_peg_class | ||
accepts_nested_attributes_for :tuning_pegs, reject_if: lambda { |attrs| attrs[:pitch]&.odd? } | ||
|
||
def self.name; "Guitar"; end | ||
end | ||
end | ||
|
||
test "index in nested attributes order" do | ||
guitar = @guitar_class.create! | ||
guitar.tuning_pegs.create!(pitch: 1) | ||
peg2 = guitar.tuning_pegs.create!(pitch: 2) | ||
guitar.update(tuning_pegs_attributes: [{ id: peg2.id, pitch: nil }]) | ||
|
||
error = guitar.errors.objects.first | ||
|
||
assert_equal ActiveRecord::Associations::NestedError, error.class | ||
assert_equal peg2.errors.objects.first, error.inner_error | ||
assert_equal :'tuning_pegs[0].pitch', error.attribute | ||
assert_equal :not_a_number, error.type | ||
assert_equal "is not a number", error.message | ||
assert_equal guitar, error.base | ||
end | ||
|
||
test "index unaffected by reject_if" do | ||
guitar = @guitar_class.create! | ||
|
||
guitar.update( | ||
tuning_pegs_attributes: [ | ||
{ pitch: 1 }, # rejected | ||
{ pitch: nil }, | ||
] | ||
) | ||
|
||
error = guitar.errors.objects.first | ||
|
||
assert_equal ActiveRecord::Associations::NestedError, error.class | ||
assert_equal :'tuning_pegs[1].pitch', error.attribute | ||
assert_equal :not_a_number, error.type | ||
assert_equal "is not a number", error.message | ||
assert_equal guitar, error.base | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters