Permalink
Browse files

Wrap cache transaction with AR transaction (instead of the other way)…

… so that AR::Rollback exceptions can be handled
  • Loading branch information...
1 parent 0672ecf commit f9b48554d65fd41dcb89f9661c9e99b8646c92ce @betamatt betamatt committed with ashleym1972 Jan 26, 2010
Showing with 28 additions and 4 deletions.
  1. +1 −1 UNSUPPORTED_FEATURES
  2. +5 −3 lib/cache_money.rb
  3. +22 −0 spec/cash/write_through_spec.rb
View
@@ -6,7 +6,7 @@
* update_all, delete, update_counter, increment_counter, decrement_counter, counter_caches in general - counter caches are replaced by this gem, bear that in mind.
* attr_readonly - no technical obstacle, just not yet supported
* attributes before typecast behave unpredictably - hard to support
-* ActiveRecord::Rollback is unsupported - the exception gets swallowed so there isn't an opportunity to rollback the cache transaction - not hard to support
+* Named bind variables :conditions => ["name = :name", { :name => "37signals!" }] - not hard to support
* printf style binds: :conditions => ["name = '%s'", "37signals!"] - not too hard to support
* objects as attributes that are serialized. story.title = {:foo => :bar}; customer.balance = Money.new(...) - these could be coerced using Column#type_cast?
View
@@ -57,11 +57,13 @@ class << active_record_class
end
end
- def transaction_with_cache_transaction(&block)
+ def transaction_with_cache_transaction(*args)
if cache_config
- repository.transaction { transaction_without_cache_transaction(&block) }
+ transaction_without_cache_transaction(*args) do
+ repository.transaction { yield }
+ end
else
- transaction_without_cache_transaction(&block)
+ transaction_without_cache_transaction(*args)
end
end
@@ -219,5 +219,27 @@ def locks_should_be_acquired_and_released_in_order(lock, keys)
end
end
end
+
+ describe 'Transactions' do
+ def create_story_and_update
+ @story = Story.create!(:title => original_title = "original title")
+
+ Story.transaction do
+ @story.title = "new title"
+ @story.save
+ yield if block_given?
+ end
+ end
+
+ it 'should commit on success' do
+ create_story_and_update
+ @story.reload.title.should == "new title"
+ end
+
+ it 'should roll back transactions when ActiveRecord::Rollback is raised' do
+ create_story_and_update { raise ActiveRecord::Rollback }
+ @story.reload.title.should == "original title"
+ end
+ end
end
end

0 comments on commit f9b4855

Please sign in to comment.