Module: Statesman::Adapters::ConfigureCachedCurrentState

Defined in:
lib/statesman/adapters/configure_cached_current_state.rb

Overview

Include on an ActiveRecord model configured with Statesman::Adapters::TypeSafeActiveRecordQueries#configure_state_machine (only transition_class: is required) to cache the current state on a column on the model. Including this module does nothing on its own - the model must also call configure_cached_current_state to wire up the write, which also exposes the configured column name via cached_state_column_name, for generic tooling that needs to introspect or repair the cached column (e.g. a cache-repair rake task).

The write itself happens inside Statesman::Adapters::ActiveRecord#create_transition (see maintain_cached_current_state there), not via a callback registered by this module - that single code path is shared by every Machine subclass and every storage adapter instance for a model, so this works even when a model is driven by several different machine classes (e.g. chosen dynamically per scheme), and it fires reliably under Statesman's mysql_gaplock_protection config, where the DB-level most_recent flip happens via a raw SQL UPDATE that bypasses ActiveRecord callbacks entirely.

Caveat: because the write only happens as part of transition_to! (via the adapter), a transition row created directly on transition_class - bypassing the machine entirely, e.g. parent.transitions.create!(...) - does not update the cache. Only real transitions performed through the machine do.

The initial-state seed on create only applies when the column is nil - if it's already been explicitly set (e.g. a test factory building a record straight into a given state, without a real transition history), that value is left alone.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



31
32
33
# File 'lib/statesman/adapters/configure_cached_current_state.rb', line 31

def self.included(base)
  base.extend(ClassMethods)
end