module StateManager::Adapters::ActiveRecord::ManagerMethods

Attributes

pending_transition[RW]
uncommitted_transitions[RW]

Public Class Methods

included(base) click to toggle source
# File lib/state_manager/adapters/active_record.rb, line 63
def self.included(base)
  base.class_eval do
    alias_method :_run_before_callbacks, :run_before_callbacks
    alias_method :_run_after_callbacks, :run_after_callbacks

    # In the AR use case, we don't want to run any callbacks
    # until the model has been saved
    def run_before_callbacks(*args)
      self.pending_transition = args
    end

    def run_after_callbacks(*args)
    end

  end
end
new(*) click to toggle source
Calls superclass method
# File lib/state_manager/adapters/active_record.rb, line 80
def initialize(*)
  super
  self.uncommitted_transitions = []
end

Public Instance Methods

after_commit() click to toggle source
# File lib/state_manager/adapters/active_record.rb, line 105
def after_commit
  transitions = self.uncommitted_transitions.dup

  self.uncommitted_transitions.clear

  transitions.each{ |t| run_commit_callbacks(*t) }
end
after_save() click to toggle source
# File lib/state_manager/adapters/active_record.rb, line 95
def after_save
  return unless pending_transition
  transition = pending_transition

  self.uncommitted_transitions << self.pending_transition
  self.pending_transition = nil

  _run_after_callbacks(*transition)
end
before_save() click to toggle source
# File lib/state_manager/adapters/active_record.rb, line 90
def before_save
  return unless pending_transition
  _run_before_callbacks(*pending_transition)
end
perform_initial_transition?() click to toggle source
# File lib/state_manager/adapters/active_record.rb, line 126
def perform_initial_transition?
  !current_state || resource.new_record?
end
persist_state() click to toggle source
# File lib/state_manager/adapters/active_record.rb, line 122
def persist_state
  resource.save!
end
run_after_callbacks(*args) click to toggle source
# File lib/state_manager/adapters/active_record.rb, line 74
def run_after_callbacks(*args)
end
run_before_callbacks(*args) click to toggle source

In the AR use case, we don't want to run any callbacks until the model has been saved

# File lib/state_manager/adapters/active_record.rb, line 70
def run_before_callbacks(*args)
  self.pending_transition = args
end
run_commit_callbacks(from_state, to_state, current_event, enter_states, exit_states) click to toggle source
# File lib/state_manager/adapters/active_record.rb, line 113
def run_commit_callbacks(from_state, to_state, current_event, enter_states, exit_states)
  exit_states.each{ |s| s.exit_committed if s.respond_to? :exit_committed }
  enter_states.each{ |s| s.enter_committed if s.respond_to? :enter_committed }
end
transition_to(*args) click to toggle source
Calls superclass method
# File lib/state_manager/adapters/active_record.rb, line 85
def transition_to(*args)
  raise(DirtyTransition, "Only one state transition may be performed before saving a record. This error could be caused by the record being initialized to a default state.") if pending_transition
  super
end
write_state(value) click to toggle source
# File lib/state_manager/adapters/active_record.rb, line 118
def write_state(value)
  resource.send :write_attribute, self.class._state_property, value.path
end