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

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

def aasm_ensure_initial_state
  AASM::StateMachineStore.fetch(self.class, true).machine_names.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_new_record?Boolean

Returns:

  • (Boolean)

25
26
27
# File 'lib/aasm/persistence/sequel_persistence.rb', line 25

def aasm_new_record?
  new?
end

#aasm_raise_invalid_recordObject

Raises:

  • (Sequel::ValidationFailed)

21
22
23
# File 'lib/aasm/persistence/sequel_persistence.rb', line 21

def aasm_raise_invalid_record
  raise Sequel::ValidationFailed.new(self)
end

#aasm_read_attribute(name) ⇒ Object


35
36
37
# File 'lib/aasm/persistence/sequel_persistence.rb', line 35

def aasm_read_attribute(name)
  send(name)
end

#aasm_saveObject


31
32
33
# File 'lib/aasm/persistence/sequel_persistence.rb', line 31

def aasm_save
  !save(raise_on_failure: false).nil?
end

#aasm_transaction(requires_new, requires_lock) ⇒ Object


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

def aasm_transaction(requires_new, requires_lock)
  self.class.db.transaction(savepoint: requires_new) do
    if requires_lock
      # http://sequel.jeremyevans.net/rdoc/classes/Sequel/Model/InstanceMethods.html#method-i-lock-21
      requires_lock.is_a?(String) ? lock!(requires_lock) : lock!
    end
    yield
  end
end

#aasm_update_column(attribute_name, value) ⇒ Object


53
54
55
# File 'lib/aasm/persistence/sequel_persistence.rb', line 53

def aasm_update_column(attribute_name, value)
  this.update(attribute_name => value)
end

#aasm_write_attribute(name, value) ⇒ Object


39
40
41
# File 'lib/aasm/persistence/sequel_persistence.rb', line 39

def aasm_write_attribute(name, value)
  send("#{name}=", value)
end

#before_createObject


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

def before_create
  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