大概代码是这样:
# app/models/concerns/member_state.rb
module MemberState
extend ActiveSupport::Concern
included do
state_machine :member_state, initial: :initial, namespace: 'member' do
event :event1 do
transition :initial => :state1
end
event :event2 do
transition :state1 => :state2
end
event :event3 do
transition :state2 => :state3
end
...
end
end
end
# app/models/concerns/admin_state.rb
module AdminState
extend ActiveSupport::Concern
included do
state_machine :admin_state, initial: :initial, namespace: 'admin' do
event :event1 do
transition :initial => :state1
end
event :event2 do
transition :state1 => :state2
end
event :event3 do
transition :state2 => :state3
end
...
end
end
end
# app/models/order.rb
class Order < ActiveRecord::Base
include MemberState
include AdminState
...
end
想要重用相同的 event 定义该如何做呢?