1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Initialization block is a part of build_record

Should be done before `before_add` callbacks.
This commit is contained in:
Ryuta Kamizono 2018-06-04 16:08:40 +09:00
parent 69d91cd6a6
commit c256d02f01
7 changed files with 22 additions and 14 deletions

View file

@ -275,6 +275,7 @@ module ActiveRecord
def build_record(attributes) def build_record(attributes)
reflection.build_association(attributes) do |record| reflection.build_association(attributes) do |record|
initialize_attributes(record, attributes) initialize_attributes(record, attributes)
yield(record) if block_given?
end end
end end

View file

@ -105,9 +105,7 @@ module ActiveRecord
if attributes.is_a?(Array) if attributes.is_a?(Array)
attributes.collect { |attr| build(attr, &block) } attributes.collect { |attr| build(attr, &block) }
else else
add_to_target(build_record(attributes)) do |record| add_to_target(build_record(attributes, &block))
yield(record) if block_given?
end
end end
end end
@ -361,8 +359,7 @@ module ActiveRecord
attributes.collect { |attr| _create_record(attr, raise, &block) } attributes.collect { |attr| _create_record(attr, raise, &block) }
else else
transaction do transaction do
add_to_target(build_record(attributes)) do |record| add_to_target(build_record(attributes, &block)) do |record|
yield(record) if block_given?
insert_record(record, true, raise) { insert_record(record, true, raise) {
@_was_loaded = loaded? @_was_loaded = loaded?
@association_ids = nil @association_ids = nil

View file

@ -90,7 +90,7 @@ module ActiveRecord
def build_record(attributes) def build_record(attributes)
ensure_not_nested ensure_not_nested
record = super(attributes) record = super
inverse = source_reflection.inverse_of inverse = source_reflection.inverse_of
if inverse if inverse

View file

@ -106,7 +106,7 @@ module ActiveRecord
end end
end end
def _create_record(attributes, raise_error = false) def _create_record(attributes, raise_error = false, &block)
unless owner.persisted? unless owner.persisted?
raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved" raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved"
end end

View file

@ -17,9 +17,8 @@ module ActiveRecord
replace(record) replace(record)
end end
def build(attributes = {}) def build(attributes = {}, &block)
record = build_record(attributes) record = build_record(attributes, &block)
yield(record) if block_given?
set_new_record(record) set_new_record(record)
record record
end end
@ -62,9 +61,8 @@ module ActiveRecord
replace(record) replace(record)
end end
def _create_record(attributes, raise_error = false) def _create_record(attributes, raise_error = false, &block)
record = build_record(attributes) record = build_record(attributes, &block)
yield(record) if block_given?
saved = record.save saved = record.save
set_new_record(record) set_new_record(record)
raise RecordInvalid.new(record) if !saved && raise_error raise RecordInvalid.new(record) if !saved && raise_error

View file

@ -114,7 +114,7 @@ module ActiveRecord
attributes[inverse.foreign_key] = target.id attributes[inverse.foreign_key] = target.id
end end
super(attributes) super
end end
end end
end end

View file

@ -737,6 +737,18 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
[:added, :before, "Roger"], [:added, :before, "Roger"],
[:added, :after, "Roger"] [:added, :after, "Roger"]
], log.last(4) ], log.last(4)
post.people_with_callbacks.build { |person| person.first_name = "Ted" }
assert_equal [
[:added, :before, "Ted"],
[:added, :after, "Ted"]
], log.last(2)
post.people_with_callbacks.create { |person| person.first_name = "Sam" }
assert_equal [
[:added, :before, "Sam"],
[:added, :after, "Sam"]
], log.last(2)
end end
def test_dynamic_find_should_respect_association_include def test_dynamic_find_should_respect_association_include