Skip to content
This repository
Browse code

Merge pull request #6445 from chancancode/3-2-stable_restore_frozen_s…

…tate_on_rollback

Restore the frozen state on rollback. (Backports #6420)
  • Loading branch information...
commit 9ee85281bca899fc521200a087149d7c8c3940d9 2 parents 56b86a3 + a543002
Rafael Mendonça França authored May 22, 2012
4  activerecord/lib/active_record/transactions.rb
@@ -327,7 +327,8 @@ def restore_transaction_record_state(force = false) #:nodoc:
327 327
         @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
328 328
         if @_start_transaction_state[:level] < 1
329 329
           restore_state = remove_instance_variable(:@_start_transaction_state)
330  
-          @attributes = @attributes.dup if @attributes.frozen?
  330
+          was_frozen = @attributes.frozen?
  331
+          @attributes = @attributes.dup if was_frozen
331 332
           @new_record = restore_state[:new_record]
332 333
           @destroyed  = restore_state[:destroyed]
333 334
           if restore_state.has_key?(:id)
@@ -336,6 +337,7 @@ def restore_transaction_record_state(force = false) #:nodoc:
336 337
             @attributes.delete(self.class.primary_key)
337 338
             @attributes_cache.delete(self.class.primary_key)
338 339
           end
  340
+          @attributes.freeze if was_frozen
339 341
         end
340 342
       end
341 343
     end
10  activerecord/test/cases/transactions_test.rb
@@ -362,6 +362,16 @@ def test_rollback_when_commit_raises
362 362
     end
363 363
   end
364 364
 
  365
+  def test_rollback_when_saving_a_frozen_record
  366
+    topic = Topic.new(:title => 'test')
  367
+    topic.freeze
  368
+    e = assert_raise(RuntimeError) { topic.save }
  369
+    assert_equal "can't modify frozen Hash", e.message
  370
+    assert !topic.persisted?, 'not persisted'
  371
+    assert_nil topic.id
  372
+    assert topic.frozen?, 'not frozen'
  373
+  end
  374
+
365 375
   def test_restore_active_record_state_for_all_records_in_a_transaction
366 376
     topic_1 = Topic.new(:title => 'test_1')
367 377
     topic_2 = Topic.new(:title => 'test_2')

2 notes on commit 9ee8528

Francesco Rodríguez

@rafaelfranca

In Ruby 1.9.2 -> the exception message is: "can't modify frozen hash"
In Ruby 1.8.7 -> It raise TypeError instead of RuntimeError

Rafael Mendonça França
Owner

@frodsan thanks. I talked with the author and he will submit a pull request

Please sign in to comment.
Something went wrong with that request. Please try again.