Skip to content

Commit

Permalink
[#2] Support block-style building in has_many
Browse files Browse the repository at this point in the history
Pass through any supplied blocks for build and create in has_many associations so that the block-style stuff still works.  Not actually sure that has_one / belongs_to actually support this, so I've not done the same there.
  • Loading branch information
h-lame committed Jul 27, 2009
1 parent 812cbac commit c809562
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lib/parental_control.rb
Expand Up @@ -133,15 +133,15 @@ def find_target_with_self_control
module HasManyAssociationMethods
def self.included(base)
base.class_eval do
def create_with_self_control(attributes = {})
record = create_without_self_control(attributes)
def create_with_self_control(attributes = {}, &block)
record = create_without_self_control(attributes, &block)
set_reciprocal_instance(record, @owner)
record
end
alias_method_chain :create, :self_control

def build_with_self_control(attributes = {})
record = build_without_self_control(attributes)
def build_with_self_control(attributes = {}, &block)
record = build_without_self_control(attributes, &block)
set_reciprocal_instance(record, @owner)
record
end
Expand Down
25 changes: 24 additions & 1 deletion test/parental_control_test.rb
Expand Up @@ -61,9 +61,21 @@ def test_parent_instance_should_be_shared_with_newly_built_child
assert_equal m.name, i.man.name, "Name of man should be the same after changes to just-built-child-owned instance"
end

def test_parent_instance_should_be_shared_with_newly_block_style_built_child
m = Man.find(:first)
i = m.interests.build {|ii| ii.topic = 'Industrial Revolution Re-enactment'}
assert_not_nil i.topic, "Child attributes supplied to build via blocks should be populated"
assert_not_nil i.man
assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
m.name = 'Bongo'
assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
i.man.name = 'Mungo'
assert_equal m.name, i.man.name, "Name of man should be the same after changes to just-built-child-owned instance"
end

def test_parent_instance_should_be_shared_with_newly_created_child
m = Man.find(:first)
i = m.interests.build(:topic => 'Industrial Revolution Re-enactment')
i = m.interests.create(:topic => 'Industrial Revolution Re-enactment')
assert_not_nil i.man
assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
m.name = 'Bongo'
Expand All @@ -72,6 +84,17 @@ def test_parent_instance_should_be_shared_with_newly_created_child
assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
end

def test_parent_instance_should_be_shared_with_newly_block_style_created_child
m = Man.find(:first)
i = m.interests.create {|ii| ii.topic = 'Industrial Revolution Re-enactment'}
assert_not_nil i.topic, "Child attributes supplied to create via blocks should be populated"
assert_not_nil i.man
assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
m.name = 'Bongo'
assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
i.man.name = 'Mungo'
assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
end
end

class ParentalControlBelongsToTests < Test::Unit::TestCase
Expand Down

0 comments on commit c809562

Please sign in to comment.