module AASM::Persistence::DynamoidPersistence::InstanceMethods

Public Instance Methods

aasm_write_state(state, name=:default) click to toggle source

Writes state to the state column and persists it to the database using update_attribute (which bypasses validation)

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/dynamoid_persistence.rb, line 39
def aasm_write_state(state, name=:default)
  old_value = read_attribute(self.class.aasm(name).attribute_name)
  write_attribute(self.class.aasm(name).attribute_name, state.to_s)

  unless self.save(:validate => false)
    write_attribute(self.class.aasm(name).attribute_name, old_value)
    return false
  end

  true
end
aasm_write_state_without_persistence(state, name=:default) 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/dynamoid_persistence.rb, line 63
def aasm_write_state_without_persistence(state, name=:default)
  write_attribute(self.class.aasm(name).attribute_name, state.to_s)
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/dynamoid_persistence.rb, line 84
def aasm_ensure_initial_state
  AASM::StateMachineStore.fetch(self.class, true).machine_names.each do |state_machine_name|
    aasm(state_machine_name).enter_initial_state if !send(self.class.aasm(state_machine_name).attribute_name) || send(self.class.aasm(state_machine_name).attribute_name).empty?
  end
end