module AASM::Persistence::CoreDataQueryPersistence::InstanceMethods

def aasm_ensure_initial_state


foo.aasm_state # => nil
foo.valid?
foo.aasm_state = nil
foo.aasm_state # => 1
foo = Foo.find(:first)


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

that the initial state gets populated before validation on create
Ensures that if the aasm_state column is nil and the record is new
def aasm_ensure_initial_state
  AASM::StateMachineStore.fetch(self.class, true).machine_names.each do |state_machine_name|
    next if !send(self.class.aasm(state_machine_name).attribute_name) || send(self.class.aasm(state_machine_name).attribute_name).empty?
    send("#{self.class.aasm(state_machine_name).attribute_name}=", aasm(state_machine_name).enter_initial_state.to_s)
  end
end

def aasm_write_state(state, name=:default)

NOTE: intended to be called from an event

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

using update_attribute (which bypasses validation)
Writes state to the state column and persists it to the database
def aasm_write_state(state, name=:default)
  raise "Cowardly refusing to save the current CoreDataQuery context"
  aasm_write_state_without_persistence(state, name)
end

def aasm_write_state_without_persistence(state, name=:default)

NOTE: intended to be called from an event

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

Writes state to the state column, but does not persist it to the database
def aasm_write_state_without_persistence(state, name=:default)
  write_attribute(self.class.aasm(name).attribute_name, state.to_s)
end