All notable changes to this project will be documented in this file.
This project adheres to Semantic Versioning.
- Fix bug where duplicated model instances referenced shared state machine state managers.
- Added support for Ruby 3.4
- Changed: Setting the
<state_name>_eventto an invalid event adds an error to the attribute instead of raising aTransitionNotFoundErrorerror. - Changed: Calling
<event_name>with an invalid event adds an error to the<state_name>_eventattribute instead of raising aTransitionNotFoundErrorerror. - Changed: Calling
<event_name>!with an invalid event adds an error to the<state_name>_eventattribute an raises aActiveRecord::RecordInvaliderror instead of aTransitionNotFoundErrorerror. - Changed: The
<state_name>_eventtype was changed toActiveModel::Attributes. - Changed:
#find_eventreturnsnilin case the event is missing, previously it raised aKeyErrorerror. - Changed: The transition of a
<state_name>_eventis executed in a prependedbefore_validationcallback. If the transition is possible, the<state_attribute>is changed and the<state_name>_eventis set tonilafterwards. If the transition is not possible, the<state_attribute>is not changed and the<state_name>_eventkeeps the set value.
Before:
order.update(state_event: 'finish') # => RailsStateMachine::Event::TransitionNotFoundError
order.finish # => RailsStateMachine::Event::TransitionNotFoundError
order.finish! # => RailsStateMachine::Event::TransitionNotFoundErrorAfter:
order.update(state_event: 'finish') # => false (state_event has the error :invalid)
order.finish # => false (state_event has the error :invalid)
order.finish! # => ActiveRecord::RecordInvalid (state_event has the error :invalid)Upgrade examples:
# Before the upgrade you might have rescued RailsStateMachine::Event::TransitionNotFoundError in e.g. a controller
def update
build_user
if @user.save
flash[:success] = 'User saved!'
redirect_to @user
else
flash.now[:error] = 'User could not be saved!'
render(:edit, status: :unprocessable_entity)
end
rescue RailsStateMachine::Event::TransitionNotFoundError
flash.now[:error] = 'State event not valid anymore, maybe reload the page?'
render(:edit, status: :unprocessable_entity)
end
# After upgrade you can either show a flash message or show an error message in your view for the <state>_event attribute
def update
build_user
if @user.save
flash[:success] = 'User saved!'
redirect_to @user
else
flash.now[:error] = @user.errors.include?(:state_event) ? 'State event not valid anymore, maybe reload the page?' : 'User could not be saved!'
render(:edit, status: :unprocessable_entity)
end
end- Added: State machine can now use the
:prefixoption to avoid name collision if you define multiple state machines on the same model, and use state names more than once - Fix bug where additional inclusions of
RailsStateMachine::Modelwould reset previous defined state machines
- Enabled MFA for RubyGems
- Added support for Ruby 3.0.
- Added: State machine can now use an attribute other than
stateto represent the machine's state. - Added: It is now possible to define multiple state machines on the same model. States and event names have to differ, though.
- Removed: Dropped support for adding a state machine to a model without including
RailsStateMachine::Model.
- Fix a bug sometimes causing unsaved changes to be lost on state transitions.
- Fix bug where state was set to an older state when making a record invalid after successfully transitioning to a new state.
- Fix bug where state was set to
nilby callingvalid?on invalid records without making a state transition.
- Fix bug when accessing autosaved association within transition
- First version.