Refactoring: Transaction more readable and ensures cleanup even if block exits early
This commit is contained in:
parent
c9ab28b81f
commit
41efbf41f5
1 changed files with 94 additions and 18 deletions
|
@ -1,26 +1,102 @@
|
||||||
class Transaction
|
class Transaction
|
||||||
def self.execute(options = {})
|
attr_reader :options
|
||||||
if options[:reset_user_id] == true
|
attr_accessor :original_user_id, :original_interface_handle
|
||||||
UserInfo.current_user_id = 1
|
|
||||||
end
|
def initialize(options = {})
|
||||||
if options[:bulk] == true
|
@options = options
|
||||||
BulkImportInfo.enable
|
end
|
||||||
end
|
|
||||||
original_interface_handle = ApplicationHandleInfo.current
|
def execute
|
||||||
if options[:interface_handle]
|
start_execute
|
||||||
ApplicationHandleInfo.current = options[:interface_handle]
|
|
||||||
end
|
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
PushMessages.init
|
start_transaction
|
||||||
|
|
||||||
yield
|
yield
|
||||||
if options[:interface_handle]
|
ensure
|
||||||
ApplicationHandleInfo.current = original_interface_handle
|
finish_transaction
|
||||||
end
|
|
||||||
TransactionDispatcher.commit(options)
|
|
||||||
PushMessages.finish
|
|
||||||
end
|
end
|
||||||
return if options[:bulk] != true
|
ensure
|
||||||
|
finish_execute
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.execute(options = {}, &block)
|
||||||
|
Transaction.new(options).execute(&block)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def start_execute
|
||||||
|
reset_user_id_start
|
||||||
|
bulk_import_start
|
||||||
|
interface_handle_start
|
||||||
|
end
|
||||||
|
|
||||||
|
def start_transaction
|
||||||
|
PushMessages.init
|
||||||
|
end
|
||||||
|
|
||||||
|
def finish_execute
|
||||||
|
reset_user_id_finish
|
||||||
|
bulk_import_finish
|
||||||
|
end
|
||||||
|
|
||||||
|
def finish_transaction
|
||||||
|
interface_handle_finish
|
||||||
|
|
||||||
|
TransactionDispatcher.commit(options)
|
||||||
|
PushMessages.finish
|
||||||
|
end
|
||||||
|
|
||||||
|
def reset_user_id?
|
||||||
|
options[:reset_user_id] == true
|
||||||
|
end
|
||||||
|
|
||||||
|
def reset_user_id_start
|
||||||
|
return if !reset_user_id?
|
||||||
|
|
||||||
|
self.original_user_id = UserInfo.current_user_id
|
||||||
|
|
||||||
|
UserInfo.current_user_id = 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def reset_user_id_finish
|
||||||
|
return if !reset_user_id?
|
||||||
|
|
||||||
|
UserInfo.current_user_id = original_user_id
|
||||||
|
end
|
||||||
|
|
||||||
|
def bulk_import?
|
||||||
|
options[:bulk] == true
|
||||||
|
end
|
||||||
|
|
||||||
|
def bulk_import_start
|
||||||
|
return if !bulk_import?
|
||||||
|
|
||||||
|
BulkImportInfo.enable
|
||||||
|
end
|
||||||
|
|
||||||
|
def bulk_import_finish
|
||||||
|
return if !bulk_import?
|
||||||
|
|
||||||
BulkImportInfo.disable
|
BulkImportInfo.disable
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def interface_handle?
|
||||||
|
options[:interface_handle].present?
|
||||||
|
end
|
||||||
|
|
||||||
|
def interface_handle_start
|
||||||
|
return if !interface_handle?
|
||||||
|
|
||||||
|
self.original_interface_handle = ApplicationHandleInfo.current
|
||||||
|
|
||||||
|
ApplicationHandleInfo.current = options[:interface_handle]
|
||||||
|
end
|
||||||
|
|
||||||
|
def interface_handle_finish
|
||||||
|
return if !interface_handle?
|
||||||
|
|
||||||
|
ApplicationHandleInfo.current = original_interface_handle
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue