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

Revert changes made to machine find_or_create method in #32 #60

Open jonathan-wheeler opened 7 years ago

jonathan-wheeler commented 7 years ago

PR #32 introduced functionality to always return a machine if one is present and no key is supplied to the state_machine call.

This broke having multiple state machines on the same class unless both have explicit names eg.using a slightly modified example from the readme.

class Vehicle
  state_machine initial: :parked do
    event :crash do
      transition all => :parked
    end
  end

  state_machine :alarm_state, initial: :active, namespace: :'alarm' do
    event :enable do
      transition all => :active
    end
  end
end

The second machine would not be created and enable would be declared on state, without the alarm namespace.

Tests were lacking to detect this behaviour so have added them.

An alternative fix would be to insist that classes with multiple state machines always have a state key on each machine including the default :state but I would argue that if you have a state machine with a non default key then you would always supply the key when interacting with the machine and have updated test/functional/driver_default_nonstandard_test.rb to reflect this.

Happy to make further amendments as you see fit. This is partially an issue with a proposed fix. As I encountered a situation which was caught out by this change and was unable to find other references to it. In the mean time I have just added an explicit :state key on the default machine but am unsure how is best to tackle it, so am open to discussion.