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


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

def aasm_ensure_initial_state
  aasm.enter_initial_state if
  send(self.class.aasm.attribute_name).to_s.strip.empty?
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



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

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) ⇒ 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



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

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

#aasm_write_state_without_persistence(state) ⇒ Object

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

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



101
102
103
# File 'lib/aasm/persistence/redis_persistence.rb', line 101

def aasm_write_state_without_persistence(state)
  send("#{self.class.aasm.attribute_name}=", state)
end

#initialize(*args) ⇒ Object

Add the inital value to intiializer

redis-objects removed the key from redis when set to nil



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

def initialize(*args)
  super
  state = send(self.class.aasm.attribute_name)
  state.value = aasm.determine_state_name(self.class.aasm.initial_state)
end