Module: AASM::Persistence::RedisPersistence::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#aasm_ensure_initial_stateObject

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

69
70
71
72
73
74
# File 'lib/aasm/persistence/redis_persistence.rb', line 69

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

#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

class Foo
  include Redis::Objects
  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[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


44
45
46
47
48
49
50
51
52
# File 'lib/aasm/persistence/redis_persistence.rb', line 44

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

#aasm_write_state(state, name = :default) ⇒ Object

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

foo = Foo[1]
foo.aasm.current_state # => :opened
foo.close!
foo.aasm.current_state # => :closed
Foo[1].aasm.current_state # => :closed

NOTE: intended to be called from an event


85
86
87
88
# File 'lib/aasm/persistence/redis_persistence.rb', line 85

def aasm_write_state(state, name=:default)
  aasm_column = self.class.aasm(name).attribute_name
  send("#{aasm_column}").value = state
end

#aasm_write_state_without_persistence(state, name = :default) ⇒ Object

Writes state to the state column, but does not persist it to the database (but actually it still does)

With Redis::Objects it's not possible to skip persisting - it's not an ORM, it does not operate like an AR model and does not know how to postpone changes.

foo = Foo[1]
foo.aasm.current_state # => :opened
foo.close
foo.aasm.current_state # => :closed
Foo[1].aasm.current_state # => :opened
foo.save
foo.aasm.current_state # => :closed
Foo[1].aasm.current_state # => :closed

NOTE: intended to be called from an event


106
107
108
# File 'lib/aasm/persistence/redis_persistence.rb', line 106

def aasm_write_state_without_persistence(state, name=:default)
  aasm_write_state(state, name)
end

#initialize(*args) ⇒ Object

Initialize with default values

Redis::Objects removes the key from Redis when set to `nil`


14
15
16
17
# File 'lib/aasm/persistence/redis_persistence.rb', line 14

def initialize(*args)
  super
  aasm_ensure_initial_state
end