makandra/rails_state_machine

state_event raises error when called with an unavailable transition

triskweline opened this issue · 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

Valid point. We should change this as suggested.