Module: AASM::Persistence::Base

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

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:


5
6
7
# File 'lib/aasm/persistence/base.rb', line 5

def self.included(base) #:nodoc:
  base.extend ClassMethods
end

Instance Method Details

#aasm_new_record?Boolean

Returns:

  • (Boolean)

44
45
46
# File 'lib/aasm/persistence/base.rb', line 44

def aasm_new_record?
  new_record?
end

#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