piotrmurach / finite_machine

A minimal finite state machine with a straightforward syntax.
https://piotrmurach.github.io/finite_machine/
MIT License
808 stars 38 forks source link

alias_target doesn't seem to work #66

Closed sigfrid closed 4 years ago

sigfrid commented 4 years ago

Describe the problem

I'm upgrading a Rails app from FiniteState 0.11.3 to 0.13.0 (the upgrade seems to be needed to work with Ruby 2.7.1).

Currently, I have the following code:

#app/models/concerns/with_state.rb
MyStateMachine.state_machine.new(self)

#app/state_machines/my_state_machine.rb
class MyStateMachine < FiniteMachine::Definition
  alias_target :model

  callbacks do
    on_transition do |event|
      model.state = event.to
    end
  end
end

Which, due the change in the API, has been updated as follows (based on https://github.com/piotrmurach/finite_machine#291-alias_target)

Steps to reproduce the problem

#app/models/concerns/with_state.rb
MyStateMachine.state_machine.new(self, alias_target: :model)

#app/state_machines/my_state_machine.rb
class MyStateMachine < FiniteMachine::Definition
  on_transition do |event|
    model.state = event.to
  end
end

Actual behavior

But the change raises the following error

NameError: undefined local variable or method `model' for #

Expected behavior

This is not really a big issue for me since it can be fixed with target.state = event.to but perhaps it can cause some problems for other users.

Is the error something expected?

Describe your environment

piotrmurach commented 4 years ago

Hi Sigfrid 👋

Thank you for using finite_machine and reporting this issue.

I removed target and alias_target from the DSL in version v0.12.0 and made them into initialization options. As much as removing target made for better API and reduced potentially troublesome scope issues, the alias_target was a bit of a moot point. Reading through your issue, I decided to bring back alias_target to DSL as it makes it much easier and more readable to have it together with the definition. I will release v0.14.0 shortly.

piotrmurach commented 4 years ago

Released v0.14.0.