Skip to content

Commit

Permalink
unbraindeadify addition to has_many :through
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4791 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Aug 19, 2006
1 parent 81b05fd commit e96c582
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ def find_target
end

private
# Array#flatten has problems with recursive arrays. Going one level deeper solves the majority of the problems.
def flatten_deeper(array)
array.collect { |element| element.respond_to?(:flatten) ? element.flatten : element }.flatten
end

def callback(method, record)
callbacks_for(method).each do |callback|
case callback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ def raise_on_type_mismatch(record)
raise ActiveRecord::AssociationTypeMismatch, "#{@reflection.class_name} expected, got #{record.class}"
end
end

# Array#flatten has problems with recursive arrays. Going one level deeper solves the majority of the problems.
def flatten_deeper(array)
array.collect { |element| element.respond_to?(:flatten) ? element.flatten : element }.flatten
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,26 @@ def reset
# must have ids in order to create records associating them, so this
# will raise ActiveRecord::HasManyThroughCantAssociateNewRecords if
# either is a new record. Calls create! so you can rescue errors.
def <<(*args)
return if args.empty?
#
# The :before_add and :after_add callbacks are not yet supported.
def <<(*records)
return if records.empty?
through = @reflection.through_reflection
raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) if @owner.new_record?

load_target

klass = through.klass
klass.transaction do
args.each do |associate|
flatten_deeper(records).each do |associate|
raise_on_type_mismatch(associate)
raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) unless associate.respond_to?(:new_record?) && !associate.new_record?
klass.with_scope(:create => construct_join_attributes(associate)) { klass.create! }

@target << klass.with_scope(:create => construct_join_attributes(associate)) { klass.create! }
end
end

self
end

[:push, :concat].each { |method| alias_method method, :<< }
Expand Down
13 changes: 13 additions & 0 deletions activerecord/test/associations_join_model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,28 @@ def test_create_associate_when_adding_to_has_many_through
count = posts(:thinking).tags.count
push = Tag.create!(:name => 'pushme')
assert_nothing_raised { posts(:thinking).tags << push }
assert_equal(count + 1, posts(:thinking).tags.size)
assert_equal(count + 1, posts(:thinking).tags(true).size)

assert_nothing_raised { posts(:thinking).tags.create!(:name => 'foo') }
assert_equal(count + 2, posts(:thinking).tags.size)
assert_equal(count + 2, posts(:thinking).tags(true).size)

assert_nothing_raised { posts(:thinking).tags.concat(Tag.create!(:name => 'abc'), Tag.create!(:name => 'def')) }
assert_equal(count + 4, posts(:thinking).tags.size)
assert_equal(count + 4, posts(:thinking).tags(true).size)
end

def test_adding_junk_to_has_many_through_should_raise_type_mismatch
assert_raise(ActiveRecord::AssociationTypeMismatch) { posts(:thinking).tags << "Uhh what now?" }
end

def test_adding_to_has_many_through_should_return_self
tags = posts(:thinking).tags
assert_equal tags, posts(:thinking).tags.push(tags(:general))
end


def test_has_many_through_sum_uses_calculations
assert_nothing_raised { authors(:david).comments.sum(:post_id) }
end
Expand Down

0 comments on commit e96c582

Please sign in to comment.