Permalink
Browse files

Make AssociationCollection start transactions in the correct database.

AssociationCollection now starts transactions by calling
AssociationCollection#transaction instead of @owner.transaction or
@reflection.klass.transaction.

Signed-off-by: Michael Koziarski <michael@koziarski.com>

[#1081 state:committed]
  • Loading branch information...
FooBarWidget authored and NZKoz committed Sep 20, 2008
1 parent 2e75bd0 commit 70b8ea4fa6f432919340345ae0d5af6aa8f87ec8
@@ -108,7 +108,7 @@ def <<(*records)
result = true
load_target if @owner.new_record?
- @owner.transaction do
+ transaction do
flatten_deeper(records).each do |record|
raise_on_type_mismatch(record)
add_record_to_target_with_callbacks(record) do |r|
@@ -123,6 +123,21 @@ def <<(*records)
alias_method :push, :<<
alias_method :concat, :<<
+ # Starts a transaction in the association class's database connection.
+ #
+ # class Author < ActiveRecord::Base
+ # has_many :books
+ # end
+ #
+ # Author.find(:first).books.transaction do
+ # # same effect as calling Book.transaction
+ # end
+ def transaction(*args)
+ @reflection.klass.transaction(*args) do
+ yield
+ end
+ end
+
# Remove all records from this association
def delete_all
load_target
@@ -173,7 +188,7 @@ def delete(*records)
records = flatten_deeper(records)
records.each { |record| raise_on_type_mismatch(record) }
- @owner.transaction do
+ transaction do
records.each { |record| callback(:before_remove, record) }
old_records = records.reject {|r| r.new_record? }
@@ -200,7 +215,7 @@ def clear
end
def destroy_all
- @owner.transaction do
+ transaction do
each { |record| record.destroy }
end
@@ -292,7 +307,7 @@ def replace(other_array)
other = other_array.size < 100 ? other_array : other_array.to_set
current = @target.size < 100 ? @target : @target.to_set
- @owner.transaction do
+ transaction do
delete(@target.select { |v| !other.include?(v) })
concat(other_array.select { |v| !current.include?(v) })
end
@@ -9,14 +9,14 @@ def initialize(owner, reflection)
alias_method :new, :build
def create!(attrs = nil)
- @reflection.klass.transaction do
+ transaction do
self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association! } : @reflection.create_association!)
object
end
end
def create(attrs = nil)
- @reflection.klass.transaction do
+ transaction do
self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association } : @reflection.create_association)
object
end
@@ -738,4 +738,13 @@ def test_counting_on_habtm_association_and_not_array
# Array#count in Ruby >=1.8.7, which would raise an ArgumentError
assert_nothing_raised { david.projects.count(:all, :conditions => '1=1') }
end
+
+ uses_mocha 'mocking Post.transaction' do
+ def test_association_proxy_transaction_method_starts_transaction_in_association_class
+ Post.expects(:transaction)
+ Category.find(:first).posts.transaction do
+ # nothing
+ end
+ end
+ end
end
@@ -1071,4 +1071,13 @@ def test_joins_with_namespaced_model_should_use_correct_type
ActiveRecord::Base.store_full_sti_class = old
end
+ uses_mocha 'mocking Comment.transaction' do
+ def test_association_proxy_transaction_method_starts_transaction_in_association_class
+ Comment.expects(:transaction)
+ Post.find(:first).comments.transaction do
+ # nothing
+ end
+ end
+ end
+
end
@@ -220,4 +220,13 @@ def test_get_ids_for_unloaded_associations_does_not_load_them
assert_equal [posts(:welcome).id, posts(:authorless).id].sort, person.post_ids.sort
assert !person.posts.loaded?
end
+
+ uses_mocha 'mocking Tag.transaction' do
+ def test_association_proxy_transaction_method_starts_transaction_in_association_class
+ Tag.expects(:transaction)
+ Post.find(:first).tags.transaction do
+ # nothing
+ end
+ end
+ end
end

0 comments on commit 70b8ea4

Please sign in to comment.