module AASM::Persistence::Base

Public Instance Methods

aasm_new_record?() click to toggle source
# File lib/aasm/persistence/base.rb, line 44
def aasm_new_record?
  new_record?
end
aasm_read_state(name=:default) click to toggle source

Returns the value of the aasm.attribute_name - called from aasm.current_state

If it's a new record, and the aasm state column is blank it returns the initial state (example provided here for ActiveRecord, but it's true for Mongoid as well):

class Foo < ActiveRecord::Base
  include AASM
  aasm :column => :status do
    state :opened
    state :closed
  end
end

foo = Foo.new
foo.current_state # => :opened
foo.close
foo.current_state # => :closed

foo = Foo.find(1)
foo.current_state # => :opened
foo.aasm_state = nil
foo.current_state # => nil

NOTE: intended to be called from an event

This allows for nil aasm states - be sure to add validation to your model

# File lib/aasm/persistence/base.rb, line 35
def aasm_read_state(name=:default)
  state = send(self.class.aasm(name).attribute_name)
  if !state || state.empty?
    aasm_new_record? ? aasm(name).determine_state_name(self.class.aasm(name).initial_state) : nil
  else
    state.to_sym
  end
end