Module: AASM::Persistence::SequelPersistence::InstanceMethods

Defined in:
lib/aasm/persistence/sequel_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


73
74
75
76
77
78
79
# File 'lib/aasm/persistence/sequel_persistence.rb', line 73

def aasm_ensure_initial_state
  AASM::StateMachine[self.class].keys.each do |state_machine_name|
    aasm(state_machine_name).enter_initial_state if
      (new? || values.key?(self.class.aasm(state_machine_name).attribute_name)) &&
      send(self.class.aasm(state_machine_name).attribute_name).to_s.strip.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 < Sequel::Model
  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



47
48
49
50
51
52
53
54
55
56
# File 'lib/aasm/persistence/sequel_persistence.rb', line 47

def aasm_read_state(name=:default)
  state = send(self.class.aasm(name).attribute_name)
  if new? && state.to_s.strip.empty?
    aasm(name).determine_state_name(self.class.aasm(name).initial_state)
  elsif state.nil?
    nil
  else
    state.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



90
91
92
93
# File 'lib/aasm/persistence/sequel_persistence.rb', line 90

def aasm_write_state state, name=:default
  aasm_column = self.class.aasm(name).attribute_name
  update_only({aasm_column => state.to_s}, aasm_column)
end

#aasm_write_state_without_persistence(state, name = :default) ⇒ 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



107
108
109
# File 'lib/aasm/persistence/sequel_persistence.rb', line 107

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

#before_createObject



17
18
19
20
# File 'lib/aasm/persistence/sequel_persistence.rb', line 17

def before_create
  aasm_ensure_initial_state
  super
end

#before_validationObject



12
13
14
15
# File 'lib/aasm/persistence/sequel_persistence.rb', line 12

def before_validation
  aasm_ensure_initial_state
  super
end