From 904b6b43d63c90528d6295bba88e0ca0fc0142ba Mon Sep 17 00:00:00 2001 From: Scott Barron Date: Sat, 8 Aug 2009 09:36:19 -0400 Subject: [PATCH] Handle inheritance properly, there was different behavior depending on whether or not AASM was included in the child class or not. (cherry picked from commit 48ff9a50dc9e1d605fce67e5de85286d63a69229) --- lib/aasm.rb | 7 +++---- spec/unit/aasm_spec.rb | 23 +++++++++++------------ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/lib/aasm.rb b/lib/aasm.rb index fb6648f8..2e1de3b5 100644 --- a/lib/aasm.rb +++ b/lib/aasm.rb @@ -15,12 +15,11 @@ class UndefinedState < RuntimeError end def self.included(base) #:nodoc: - # TODO - need to ensure that a machine is being created because - # AASM was either included or arrived at via inheritance. It - # cannot be both. base.extend AASM::ClassMethods AASM::Persistence.set_persistence(base) - AASM::StateMachine[base] = AASM::StateMachine.new('') + unless AASM::StateMachine[base] + AASM::StateMachine[base] = AASM::StateMachine.new('') + end end module ClassMethods diff --git a/spec/unit/aasm_spec.rb b/spec/unit/aasm_spec.rb index cb79b2a8..e0066d1a 100644 --- a/spec/unit/aasm_spec.rb +++ b/spec/unit/aasm_spec.rb @@ -27,6 +27,11 @@ def exit end end +class FooTwo < Foo + include AASM + aasm_state :foo +end + class Bar include AASM @@ -82,20 +87,14 @@ def rich?; self.balance >= RICH; end describe AASM, '- subclassing' do - before(:each) do - @parent = Class.new do - include AASM + it 'should have the parent states' do + Foo.aasm_states.each do |state| + FooTwo.aasm_states.should include(state) end end - - it 'should invoke the original inherited callback' do - @parent.should_receive(:inherited) - Class.new(@parent) - end - - it 'should have a unique states hash' do - child = Class.new(@parent) - child.aasm_states.equal?(@parent.aasm_states).should be_false + + it 'should not add the child states to the parent machine' do + Foo.aasm_states.should_not include(:foo) end end