module AASM::Persistence::RedisPersistence::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 |name| aasm_column = self.class.aasm(name).attribute_name aasm(name).enter_initial_state if !send(aasm_column).value || send(aasm_column).value.empty? end end
def aasm_read_state(name=:default)
NOTE: intended to be called from an event
foo.current_state # => nil
foo.aasm_state = nil
foo.current_state # => :opened
foo = Foo[1]
foo.current_state # => :closed
foo.close
foo.current_state # => :opened
foo = Foo.new
end
end
state :closed
state :opened
aasm :column => :status do
include AASM
include Redis::Objects
class Foo
If it's a new record, and the aasm state column is blank it returns the initial state
Returns the value of the aasm.attribute_name - called from aasm.current_state
def aasm_read_state(name=:default) state = send(self.class.aasm(name).attribute_name) if state.value.nil? nil else state.value.to_sym end end
def aasm_write_state(state, name=:default)
Foo[1].aasm.current_state # => :closed
foo.aasm.current_state # => :closed
foo.close!
foo.aasm.current_state # => :opened
foo = Foo[1]
Writes state to the state column and persists it to the database
def aasm_write_state(state, name=:default) aasm_column = self.class.aasm(name).attribute_name send("#{aasm_column}").value = state end
def aasm_write_state_without_persistence(state, name=:default)
Foo[1].aasm.current_state # => :closed
foo.aasm.current_state # => :closed
foo.save
Foo[1].aasm.current_state # => :opened
foo.aasm.current_state # => :closed
foo.close
foo.aasm.current_state # => :opened
foo = Foo[1]
it does not operate like an AR model and does not know how to postpone changes.
With Redis::Objects it's not possible to skip persisting - it's not an ORM,
(but actually it still does)
Writes state to the state column, but does not persist it to the database
def aasm_write_state_without_persistence(state, name=:default) aasm_write_state(state, name) end
def initialize(*args)
Initialize with default values
def initialize(*args) super aasm_ensure_initial_state end