-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix force_reload for loaded ActiveRecord transitions
The current implementation [1] for force reloading will use the in-memory transitions if they're available instead of forcing a DB query like you'd expect when passing this flag. This means that `force_reload: true` currently forces a cache-level reload (an instance variable holding the most recent state in memory) of the most recent transition, but there is no way to force a DB-level reload when the transitions are in memory. In most cases the current behaviour works as expected – accessing `model.state_machine.current_state` will issue a SELECT with LIMIT 1, so the full set of the model's transitions will not be loaded into memory and subsequent calls to `current_state` will issue fresh queries. This breaks when you actually load the association, e.g. with `model.history.inspect`. Once this has happened, the adapter's `history` (and thus also methods such as `model.state_machine.current_state`) will use the in-memory transitions whether `force_reload` is true or false, and never issue fresh queries. In practice, you often want a way to reload the transitions from the DB to ensure that you pick up on any new transitions from other processes, or other instances of the same model in the same process. This is what is expected when you explicitly ask statesman to `force_reload`! This change simply passes `force_reload` along to `history`, and checks that it's false before going ahead and using in-memory transitions. This does not affect the memory adapter, which naturally has the correct state in memory, or the Mongoid adapter (verified with a new spec). [1]: https://github.com/gocardless/statesman/blob/14250521d38449244e9bcb8f5b31b76e26614c39/lib/statesman/adapters/active_record.rb#L44-L60
- Loading branch information
Jacob Pargin
committed
Jan 5, 2018
1 parent
b962ed2
commit a409866
Showing
5 changed files
with
61 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters