diff options
author | Alireza Bashiri <azbshiri@gmail.com> | 2018-07-27 11:20:45 +0430 |
---|---|---|
committer | Alireza Bashiri <azbshiri@gmail.com> | 2018-08-02 08:27:03 +0430 |
commit | dc16cdd89a93817203e7c8a49af174fde81402e0 (patch) | |
tree | 249f5b7b45cabe0aed07046f21373acc58d397c5 /activerecord/test | |
parent | 67fa1ad0c3b811aa527ec748e14d95dee2db9f69 (diff) | |
download | rails-dc16cdd89a93817203e7c8a49af174fde81402e0.tar.gz rails-dc16cdd89a93817203e7c8a49af174fde81402e0.tar.bz2 rails-dc16cdd89a93817203e7c8a49af174fde81402e0.zip |
Call build when extend with nested attributes defined
What?
From now on when `accepts_nested_attributes_for` defined and `extend` option
added the overwritten `build` method being called.
[Alireza Bashiri, Martins Polakovs]
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/nested_attributes_test.rb | 12 | ||||
-rw-r--r-- | activerecord/test/models/pirate.rb | 8 |
2 files changed, 19 insertions, 1 deletions
diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb index ec01a2965d..aa519fd332 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -1094,3 +1094,15 @@ class TestHasManyAutosaveAssociationWhichItselfHasAutosaveAssociations < ActiveR assert_equal ["Ship name can't be blank"], part.errors.full_messages end end + +class TestNestedAttributesWithExtend < ActiveRecord::TestCase + setup do + Pirate.accepts_nested_attributes_for :treasures + end + + def test_extend_affects_nested_attributes + pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?") + pirate.treasures_attributes = [{ id: nil }] + assert_equal "from extension", pirate.treasures[0].name + end +end diff --git a/activerecord/test/models/pirate.rb b/activerecord/test/models/pirate.rb index c8617d1cfe..fd5083e597 100644 --- a/activerecord/test/models/pirate.rb +++ b/activerecord/test/models/pirate.rb @@ -17,7 +17,13 @@ class Pirate < ActiveRecord::Base after_remove: proc { |p, pa| p.ship_log << "after_removing_proc_parrot_#{pa.id}" } has_and_belongs_to_many :autosaved_parrots, class_name: "Parrot", autosave: true - has_many :treasures, as: :looter + module PostTreasuresExtension + def build(attributes = {}) + super({ name: "from extension" }.merge(attributes)) + end + end + + has_many :treasures, as: :looter, extend: PostTreasuresExtension has_many :treasure_estimates, through: :treasures, source: :price_estimates has_one :ship |