pluginaweek / state_machine

Adds support for creating state machines for attributes on any Ruby class
http://www.pluginaweek.org
MIT License
3.74k stars 507 forks source link

Inheritance not working as expected #292

Open h0jeZvgoxFepBQ2C opened 10 years ago

h0jeZvgoxFepBQ2C commented 10 years ago

State Machines in inherited classes cannot be overriden in child classes.

See following example:

class TestModel
  attr_accessor :state

  state_machine :initial => :parked do
    event :ignite do
      transition :parked => :idling
    end
    before_transition :parked => :idling do |m|
      puts "hihihi"
    end
  end
end

class TestModel2 < TestModel

  state_machine :initial => :parked do
    event :ignite do
      transition :parked => :idling
    end
    before_transition :parked => :idling do |m|
      puts "asdasdasd"
    end
  end
end

1.9.3p392 :001 > a = TestModel.new
 => #<TestModel:0x007f81751eb768 @state="parked">
1.9.3p392 :002 > a.ignite
hihihi
 => true
1.9.3p392 :003 > a = TestModel2.new
 => #<TestModel2:0x007f817529bcf8 @state="parked">
1.9.3p392 :004 > a.ignite
hihihi
asdasdasd
 => true
1.9.3p392 :005 >

```ruby

As you can see the "hihihi" output is wrong. We should only see "asdasdasd"...
the8472 commented 10 years ago

Your expectations are wrong in this case, not the gem.

Since any number of before and after transition handlers can be registered - nowhere does the documentation state that there is only one singleton handler which gets substituted in place.

You can modify the callbacks array of the machine, or use the :do => :method syntax and override the method instead of trying to replace a nameless proc.