rails_state_machine icon indicating copy to clipboard operation
rails_state_machine copied to clipboard

state_event raises error when called with an unavailable transition

Open triskweline opened this issue 3 years ago • 1 comments

When setting record.state_event to a transition that is not available for the current state, rails_state_machine immediately raises an error.

I would expect the setter of ActiveRecord attributes to always store a value without errors, even when set to an invalid value. Validating a record with an unavailable state_event attribute should add an ActiveRecord error on the :state_event attribute.

Example

class Model < ApplicationRecord

  state_machine do
    state :step1, initial: true
    state :step2
    state :step3
  end
  
  event :finish_step1 do
    transitions from: :step1, to: :step2
  end
  
  event :finish_step2 do
    transitions from: :step2, to: :step3
  end
    
end

Model.new.state # => 'step1'
Model.new.state_event = 'finish_step2' # throws RailsStateMachine::Event::TransitionNotFoundError (finish_Step2 does not transition from step1

triskweline avatar Mar 03 '21 22:03 triskweline

Valid point. We should change this as suggested.

foobear avatar Mar 24 '21 12:03 foobear