state-machines / state_machines

Adds support for creating state machines for attributes on any Ruby class
https://github.com/state-machines/state_machines
MIT License
814 stars 91 forks source link

Fix states being evaluated with wrong `owner_class` context #44

Closed tonymarklove closed 8 years ago

tonymarklove commented 8 years ago

When a sub-class clones a new copy of the machine the states get updated but the cached StateContext was still pointing to the old machine instance. Meaning that each state rule gets evaluated in the context of the parent class.

We first noticed this with ActiveRecord objects using single table inheritance. Adding validation rules in states on the sub-class would cause the parent class to also apply those validations.

The added test case actually passes without the fix, but I can’t quite figure out why. Something to do with the default definee for def in the context of a class_eval, I think.

In any case, example_class_method will cause an undefined method error if it is called on Vehicle, but executes correctly on Motorcycle with the fix applied.

mattzollinhofer commented 8 years ago

Ran into this exact problem, validations added to sub-classed model using STI being applied too broadly. We used this fix and it resolved the issue. Thanks!

muzfuz commented 8 years ago

Any chance on getting this merged?

snowblink commented 8 years ago

@seuros any news?