Permalink
Browse files

Adding optional logging to state transitions

  • Loading branch information...
jasonfox committed Aug 15, 2011
1 parent 2feec2a commit 2d1e33966010ea60db683c234e9e7a35a7ba78be
Showing with 6 additions and 0 deletions.
  1. +6 −0 lib/aasm/event.rb
View
@@ -17,6 +17,7 @@ def fire(obj, to_state=nil, *args)
if transition.perform(obj, *args)
next_state = to_state || Array(transition.to).first
transition.execute(obj, *args)
+ log_event(obj, transition) if options[:log]
break
end
end
@@ -106,4 +107,9 @@ def transitions(trans_opts)
@transitions << AASM::SupportingClasses::StateTransition.new(trans_opts.merge({:from => s.to_sym}))
end
end
+
+ def log_event(obj, trans)
+ table_name = ActiveSupport::Inflector.singularize(obj.class.table_name)
+ obj.connection.execute("insert into #{table_name}_events (#{table_name}_id, event, from_state, to_state, created_at) values (#{obj.id}, '#{name}', '#{trans.from}', '#{trans.to}', '#{Time.now.to_s(:db)}')")
+ end
end

0 comments on commit 2d1e339

Please sign in to comment.