Method: ActiveHistory.encapsulate

Defined in:
lib/activehistory.rb

.encapsulate(attributes_or_event = {}, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/activehistory.rb', line 22

def self.encapsulate(attributes_or_event={}, &block)
  if attributes_or_event.is_a?(ActiveHistory::Event)
    event = attributes_or_event
  else
    event = ActiveHistory::Event.new(attributes_or_event)
  end

  Thread.current[:activehistory_event] = event
  
  yield
ensure
  if configured? && Thread.current[:activehistory_event] && !Thread.current[:activehistory_event].actions.empty?
    Thread.current[:activehistory_event].save!
  end
  Thread.current[:activehistory_event] = nil
end