提交 503bee01 编写于 作者: A Aaron Patterson

initialize instance variables for transactions to remove conditionals

上级 075b9145
...@@ -393,6 +393,7 @@ def init_internals ...@@ -393,6 +393,7 @@ def init_internals
@marked_for_destruction = false @marked_for_destruction = false
@new_record = true @new_record = true
@mass_assignment_options = nil @mass_assignment_options = nil
@_start_transaction_state = {}
end end
end end
end end
...@@ -308,7 +308,6 @@ def with_transaction_returning_status ...@@ -308,7 +308,6 @@ def with_transaction_returning_status
# Save the new record state and id of a record so it can be restored later if a transaction fails. # Save the new record state and id of a record so it can be restored later if a transaction fails.
def remember_transaction_record_state #:nodoc: def remember_transaction_record_state #:nodoc:
@_start_transaction_state ||= {}
@_start_transaction_state[:id] = id if has_attribute?(self.class.primary_key) @_start_transaction_state[:id] = id if has_attribute?(self.class.primary_key)
@_start_transaction_state[:new_record] = @new_record @_start_transaction_state[:new_record] = @new_record
@_start_transaction_state[:destroyed] = @destroyed @_start_transaction_state[:destroyed] = @destroyed
...@@ -317,18 +316,16 @@ def remember_transaction_record_state #:nodoc: ...@@ -317,18 +316,16 @@ def remember_transaction_record_state #:nodoc:
# Clear the new record state and id of a record. # Clear the new record state and id of a record.
def clear_transaction_record_state #:nodoc: def clear_transaction_record_state #:nodoc:
if defined?(@_start_transaction_state) @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1 @_start_transaction_state.clear if @_start_transaction_state[:level] < 1
remove_instance_variable(:@_start_transaction_state) if @_start_transaction_state[:level] < 1
end
end end
# Restore the new record state and id of a record that was previously saved by a call to save_record_state. # Restore the new record state and id of a record that was previously saved by a call to save_record_state.
def restore_transaction_record_state(force = false) #:nodoc: def restore_transaction_record_state(force = false) #:nodoc:
if defined?(@_start_transaction_state) unless @_start_transaction_state.empty?
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1 @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
if @_start_transaction_state[:level] < 1 || force if @_start_transaction_state[:level] < 1 || force
restore_state = remove_instance_variable(:@_start_transaction_state) restore_state = @_start_transaction_state
was_frozen = @attributes.frozen? was_frozen = @attributes.frozen?
@attributes = @attributes.dup if was_frozen @attributes = @attributes.dup if was_frozen
@new_record = restore_state[:new_record] @new_record = restore_state[:new_record]
...@@ -340,13 +337,14 @@ def restore_transaction_record_state(force = false) #:nodoc: ...@@ -340,13 +337,14 @@ def restore_transaction_record_state(force = false) #:nodoc:
@attributes_cache.delete(self.class.primary_key) @attributes_cache.delete(self.class.primary_key)
end end
@attributes.freeze if was_frozen @attributes.freeze if was_frozen
@_start_transaction_state.clear
end end
end end
end end
# Determine if a record was created or destroyed in a transaction. State should be one of :new_record or :destroyed. # Determine if a record was created or destroyed in a transaction. State should be one of :new_record or :destroyed.
def transaction_record_state(state) #:nodoc: def transaction_record_state(state) #:nodoc:
@_start_transaction_state[state] if defined?(@_start_transaction_state) @_start_transaction_state[state]
end end
# Determine if a transaction included an action for :create, :update, or :destroy. Used in filtering callbacks. # Determine if a transaction included an action for :create, :update, or :destroy. Used in filtering callbacks.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册