Module: AASM::Persistence::ActiveRecordPersistence::WriteState

Defined in:
lib/alexrevin-aasm_numerical/persistence/active_record_persistence.rb

Instance Method Summary collapse

Instance Method Details

#aasm_write_state(state) ⇒ Object

Writes state to the state column and persists 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 # => :closed

NOTE: intended to be called from an event



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/alexrevin-aasm_numerical/persistence/active_record_persistence.rb', line 195

def aasm_write_state(state)
  old_value = read_attribute(self.class.aasm_column)
  write_attribute(self.class.aasm_column, state.to_s)

  unless self.save
    write_attribute(self.class.aasm_column, old_value)
    return false
  end

  true
end