module AASM::Persistence::OhmPersistence::InstanceMethods

Public Instance Methods

aasm_write_state(state) click to toggle source

Writes state to the state column and persists it to the database

foo = Foo.find(1)
foo.aasm.current_state # => :opened
foo.close!
foo.aasm.current_state # => :closed
Foo.find(1).aasm.current_state # => :closed

NOTE: intended to be called from an event

# File lib/aasm/persistence/ohm_persistence.rb, line 63
def aasm_write_state(state)
  old_value = self.send(self.class.aasm_column.to_sym)
  aasm_write_state_without_persistence(state)

  success = self.save

  unless success
    aasm_write_state_without_persistence(old_value)
    return false
  end

  true
end
aasm_write_state_without_persistence(state) click to toggle source

Writes state to the state column, but does not persist it to the database

foo = Foo.find(1)
foo.aasm.current_state # => :opened
foo.close
foo.aasm.current_state # => :closed
Foo.find(1).aasm.current_state # => :opened
foo.save
foo.aasm.current_state # => :closed
Foo.find(1).aasm.current_state # => :closed

NOTE: intended to be called from an event

# File lib/aasm/persistence/ohm_persistence.rb, line 89
def aasm_write_state_without_persistence(state)
  self.send(:"#{self.class.aasm_column}=", state.to_s)
end
before_create() click to toggle source
Calls superclass method
# File lib/aasm/persistence/ohm_persistence.rb, line 48
def before_create
  super
  aasm_ensure_initial_state
end

Private Instance Methods

aasm_ensure_initial_state() click to toggle source

Ensures that if the aasm_state column is nil and the record is new that the initial state gets populated before validation on create

foo = Foo.new
foo.aasm_state # => nil
foo.valid?
foo.aasm_state # => "open" (where :open is the initial state)

foo = Foo.find(:first)
foo.aasm_state # => 1
foo.aasm_state = nil
foo.valid?
foo.aasm_state # => nil
# File lib/aasm/persistence/ohm_persistence.rb, line 110
def aasm_ensure_initial_state
  aasm.enter_initial_state if send(self.class.aasm_column).blank?
end