mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
d59dbbf6b0
When an AASM including class is subclassed, a shallow copy is made of the StateMachine object. This means that all subclasses share the same states hash and thus the same set of states, which prevents (among other things) different subclasses from using the same state names. Give StateMachine a smart #clone method that copies the states hash and invoke that rather than #dup upon subclassing.
35 lines
688 B
Ruby
35 lines
688 B
Ruby
require 'ostruct'
|
|
|
|
module AASM
|
|
class StateMachine
|
|
def self.[](*args)
|
|
(@machines ||= {})[args]
|
|
end
|
|
|
|
def self.[]=(*args)
|
|
val = args.pop
|
|
(@machines ||= {})[args] = val
|
|
end
|
|
|
|
attr_accessor :states, :events, :initial_state, :config
|
|
attr_reader :name
|
|
|
|
def initialize(name)
|
|
@name = name
|
|
@initial_state = nil
|
|
@states = []
|
|
@events = {}
|
|
@config = OpenStruct.new
|
|
end
|
|
|
|
def clone
|
|
klone = super
|
|
klone.states = states.clone
|
|
klone
|
|
end
|
|
|
|
def create_state(name, options)
|
|
@states << AASM::SupportingClasses::State.new(name, options) unless @states.include?(name)
|
|
end
|
|
end
|
|
end
|