Module: StateMachineHistory
- Included in:
- StateMachine::Machine
- Defined in:
- lib/state_machine_history.rb
Defined Under Namespace
Classes: InvalidState
Class Method Summary collapse
Class Method Details
.included(base) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/state_machine_history.rb', line 11 def self.included(base) base.class_eval do def track_history self.owner_class.class_eval do has_one :machine_log, :as => :loggable end before_transition(any => any) do |object, transition| MachineHistory.create(:loggable => object, :from_state=>transition.from_name.to_s, :to_state=>transition.to_name.to_s, :event=>transition.event.to_s ) end end alias_method :define_event_helpers_original, :define_event_helpers def define_event_helpers define_event_helpers_original # Define the method which determines whether we visited a state before current or particular one define_instance_method(:was_there?) do |machine, object, from_state, to_state| to_state ||= object.state from_state = from_state.to_s to_state = to_state.to_s is_from_state = machine.states.detect {|item| item.name.to_s == from_state} raise InvalidState, "\"#{from_state}\" is an unknown state machine state" unless is_from_state is_to_state = machine.states.detect {|item| item.name.to_s == to_state} raise InvalidState, "\"#{to_state}\" is an unknown state machine state" unless is_to_state history = MachineHistory.find(:last, :conditions => {:loggable_id => object.id, :to_state => to_state}) next false if history.nil? date = history.created_at is_state = MachineHistory.find(:last, :conditions => ["loggable_id = ? and from_state = ? and created_at <= ?", object.id, from_state, date]) is_state ? true : false end end end end |