Method: AASM::Persistence::Base#aasm_read_state

Defined in:
lib/aasm/persistence/base.rb

#aasm_read_state(name = :default) ⇒ Object

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



35
36
37
38
39
40
41
42
# 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