Following are System Specifications :=
- Language Ruby
- Framework Padrino
- ORM Mongoid
- Database MongoDB
Now I have a Person model with two fields tat maintain states separately.
class Person
include Mongoid::Document
include Mongoid::Timestamps
# Field Names
#-----------------
field :name, :type => String
field :sms_state, :type => String, :default => 'main_menu'
field :ivr_state, :type => String, :default => 'main_menu'
end
I add State Machines using Monkey Patching, as I need to Update the State as per request controller. ie, If SMS Controller is requested then only SMS State Machine for sms_state should Monkey Patched and similarly for IVR Controller I do Monkey Patching this way, (Reason for Monkey PAtching as the entire flow and states of State Machine are same except the State Field, and Events Change...)
Person.class_eval do
state_machine :state_field, :initial => :main_menu do
# State Machine Flow
# Events Details
#===============================================================
event :state_reset do
transition all - [:country_confirmation, :topup_confirmation] => :main_menu
end
event :country_list do
transition [:main_menu, :did_purchased, :country_probed] => :country_listed
end
# States Details
#===============================================================================
state :main_menu do
define_method (:get_msg) {|obj| current_module.get_main_menu_msg(obj.dids.count, obj.spokn_id) }
end
state :country_listed do
define_method (:get_msg) {|obj| current_module.get_country_list_msg Country.all }
end
end
end
But It Works Fine for State Machine only. Suppose it was working Fine for SMS Controller, and If request comes from IVR Controller the Ivr State Machine is Monkey Patched. But State Field Doesn't and I get following ERROR
StateMachine::InvalidTransition - Cannot transition sms_state via :ivr_country_list from :country_listed