Module: AASM::Persistence::ActiveRecordPersistence::ReadState

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

Instance Method Summary collapse

Instance Method Details

#aasm_read_stateObject

Returns the value of the aasm_column - called from aasm_current_state

If it’s a new record, and the aasm state column is blank it returns the initial state:

class Foo < ActiveRecord::Base
  include AASM
  aasm_column :status
  aasm_state :opened
  aasm_state :closed
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



234
235
236
237
238
239
240
# File 'lib/aasm/persistence/active_record_persistence.rb', line 234

def aasm_read_state
  if new_record?
    send(self.class.aasm_column).blank? ? aasm_determine_state_name(self.class.aasm_initial_state) : send(self.class.aasm_column).to_sym
  else
    send(self.class.aasm_column).nil? ? nil : send(self.class.aasm_column).to_sym
  end
end