module Transitions

Constants

VERSION

Public Class Methods

active_model_descendant?(klazz) click to toggle source
# File lib/transitions.rb, line 85
def self.active_model_descendant?(klazz)
  # Checking directly for "ActiveModel" wouldn't work so we use some arbitrary module close to it.
  defined?(ActiveModel) && klazz.included_modules.include?(ActiveModel::Validations)
end
included(base) click to toggle source
# File lib/transitions.rb, line 37
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

available_transitions() click to toggle source
# File lib/transitions.rb, line 57
def available_transitions
  get_state_machine.events_for(current_state)
end
can_transition?(*events) click to toggle source
# File lib/transitions.rb, line 61
def can_transition?(*events)
  events.all? do |event|
    self.class.get_state_machine.events_for(current_state).include?(event.to_sym)
  end
end
cant_transition?(*events) click to toggle source
# File lib/transitions.rb, line 67
def cant_transition?(*events)
  !can_transition?(*events)
end
current_state() click to toggle source
# File lib/transitions.rb, line 71
def current_state
  sm   = get_state_machine
  ivar = sm.current_state_variable

  value = instance_variable_get(ivar)
  return value if value

  if Transitions.active_model_descendant?(self.class)
    value = instance_variable_set(ivar, read_state)
  end

  !(value.nil? || value.to_s.empty?) ? value : sm.initial_state
end
get_state_machine() click to toggle source
# File lib/transitions.rb, line 41
def get_state_machine
  self.class.get_state_machine
end
update_current_state(new_state, persist = false) click to toggle source
# File lib/transitions.rb, line 45
def update_current_state(new_state, persist = false)
  ivar = get_state_machine.current_state_variable

  if Transitions.active_model_descendant?(self.class)
    write_state(new_state) if persist
    # TODO: This seems like a duplicate, `write_new` already calls `write_state_without_persistence`.
    write_state_without_persistence(new_state)
  end

  instance_variable_set(ivar, new_state)
end