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

Defined in:
lib/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



249
250
251
252
253
254
255
# File 'lib/persistence/active_record_persistence.rb', line 249

def aasm_read_state
  state = if new_record?
    aasm_column_value.blank? ? self.class.aasm_initial_state : self.class.aasm_state_name(aasm_column_value).to_sym
  else
    aasm_column_value.nil? ? nil : self.class.aasm_state_name(aasm_column_value).to_sym
  end          
end