Skip to content

Commit

Permalink
Handle inheritance properly, there was different behavior depending o…
Browse files Browse the repository at this point in the history
…n whether or not AASM was included in the child class or not.

(cherry picked from commit 48ff9a5)
  • Loading branch information
Scott Barron authored and ttilley committed Aug 8, 2009
1 parent 10a1f8f commit 904b6b4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
7 changes: 3 additions & 4 deletions lib/aasm.rb
Expand Up @@ -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
Expand Down
23 changes: 11 additions & 12 deletions spec/unit/aasm_spec.rb
Expand Up @@ -27,6 +27,11 @@ def exit
end
end

class FooTwo < Foo
include AASM
aasm_state :foo
end

class Bar
include AASM

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 904b6b4

Please sign in to comment.