Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First pass at playing nice with multiple state machines

  • Loading branch information...
commit 972709d43ff648e2d6bda0e4446a800c3e60738f 1 parent e0ca91e
@wfarr authored
View
32 lib/active_model/transitions.rb
@@ -27,7 +27,7 @@ module Transitions
included do
include ::Transitions
after_initialize :set_initial_state
- validates_presence_of :state
+ validate :state_presence
validate :state_inclusion
end
@@ -56,20 +56,40 @@ def write_state(state_machine, state)
def write_state_without_persistence(state_machine, state)
ivar = state_machine.current_state_variable
instance_variable_set(ivar, state)
- self.state = state.to_s
+ self.send(:"#{state_machine.name}=", state.to_s)
end
def read_state(state_machine)
- self.state && self.state.to_sym
+ self.send(state_machine.name) && self.send(state_machine.name).to_sym
end
def set_initial_state
- self.state ||= self.class.state_machine.initial_state.to_s if self.has_attribute?(:state)
+ self.class.state_machines.each do |state_machine_array_container|
+ state_machine = state_machine_array_container[1]
+ name = state_machine.name
+ if self.send(name).nil? && self.has_attribute?(name)
+ self.send(:"#{name}=", state_machine.initial_state.to_s)
+ end
+ end
+ end
+
+ def state_presence
+ self.class.state_machines.each do |state_machine_array_container|
+ state_machine = state_machine_array_container[1]
+ name = state_machine.name
+ if self.send(name).nil?
+ self.errors.add(name, :presence)
+ end
+ end
end
def state_inclusion
- unless self.class.state_machine.states.map{|s| s.name.to_s }.include?(self.state.to_s)
- self.errors.add(:state, :inclusion, :value => self.state)
+ self.class.state_machines.each do |state_machine_array_container|
+ state_machine = state_machine_array_container[1]
+ name = state_machine.name
+ unless state_machine.states.map{|s| s.name.to_s }.include?(self.send(name))
+ self.errors.add(name, :inclusion, :value => self.send(name))
+ end
end
end
end
View
12 lib/transitions/event.rb
@@ -27,12 +27,16 @@ class Event
def initialize(machine, name, options = {}, &block)
@machine, @name, @transitions = machine, name, []
if machine
- machine.klass.send(:define_method, "#{name}!") do |*args|
- machine.fire_event(name, self, true, *args)
+ combined_name = name
+ if machine.name != :default
+ combined_name = "#{machine.name}_#{name}"
+ end
+ machine.klass.send(:define_method, "#{combined_name}!") do |*args|
+ machine.fire_event(combined_name, self, true, *args)
end
- machine.klass.send(:define_method, name.to_s) do |*args|
- machine.fire_event(name, self, false, *args)
+ machine.klass.send(:define_method, combined_name) do |*args|
+ machine.fire_event(combined_name, self, false, *args)
end
end
update(options, &block)
View
1  lib/transitions/machine.rb
@@ -44,6 +44,7 @@ def update(options = {}, &block)
end
def fire_event(event, record, persist, *args)
+ event = event.gsub(/^#{self.name}_/, '').to_sym
state_index[record.current_state(@name)].call_action(:exit, record)
begin
if new_state = @events[event].fire(record, nil, *args)
Please sign in to comment.
Something went wrong with that request. Please try again.