benzelano / test_lh_import

1 stars 0 forks source link

2nd state_machine with targeted attribute doesn't pass validation #192

Closed benzelano closed 13 years ago

benzelano commented 13 years ago

Alex Tan opened this issue

In a vanilla Rails 3.1.0rc4 application, the following code does not pass validation.

@@@ ruby class Alarm < ActiveRecord::Base

state_machine :state, initial: :off do
    state :off
    state :on

    event :stop do
        transition all => :off
    end

end

state_machine :public_state, attribute: :state do

    event :start do
        transition all => :on
    end

end

end @@@

This is the result:

@@@ ruby alarm = Alarm.new alarm.save alarm.errors #<ActiveModel::Errors:0x00000104331148 @base=#<Alarm id: nil, state: "off">, @messages={:state=>["is invalid"]}> @@@

I think I’m following the "documentation":http://rdoc.info/github/pluginaweek/state_machine/master/StateMachine/Integrations/ActiveRecord properly, but would love to know if I’m wrong.

original LH ticket

This ticket has 0 attachment(s).

benzelano commented 13 years ago

Hey Alex -

Nice find! It looks like this is an oversight on my part. The validations that are in place don’t take into account the fact that multiple state machines can point to the same attribute but still have states that they don’t both know about. In this case, your "public_state" machine doesn’t know about the "off" state since it wasn’t referenced anywhere in its definition. You can work around this by just defining the state in that machine like so:

@@@ ruby state_machine :public_state, attribute: :state do ... state :off end @@@

However, I think I can change state_machine so that machines can be aware of each other and properly validate. I’ll start to take a look into this.

Thanks again!

benzelano commented 13 years ago

(from [4297235f09a8af604a7612f979fd94787319b10b]) Fix multiple machines not being able to target the same attribute if all possible states aren’t defined in each [#92 state:resolved] https://github.com/pluginaweek/state_machine/commit/4297235f09a8af604a7612f979fd94787319b10b