module AASM::Persistence::MongoidPersistence::InstanceMethods

Private Instance Methods

aasm_ensure_initial_state() click to toggle source

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
# File lib/aasm/persistence/mongoid_persistence.rb, line 102
def aasm_ensure_initial_state
  AASM::StateMachineStore.fetch(self.class, true).machine_names.each do |state_machine_name|
    attribute_name = self.class.aasm(state_machine_name).attribute_name.to_s
    # Do not load initial state when object attributes are not loaded,
    # mongoid has_many relationship does not load child object attributes when
    # only ids are loaded, for example parent.child_ids will not load child object attributes.
    # This feature is introduced in mongoid > 4.
    if attribute_names.include?(attribute_name) && !attributes[attribute_name] || attributes[attribute_name].empty?
      # attribute_missing? is defined in mongoid > 4
      return if Mongoid::VERSION.to_f >= 4 && attribute_missing?(attribute_name)
      send("#{self.class.aasm(state_machine_name).attribute_name}=", aasm(state_machine_name).enter_initial_state.to_s)
    end
  end
end
aasm_raise_invalid_record() click to toggle source
# File lib/aasm/persistence/mongoid_persistence.rb, line 61
def aasm_raise_invalid_record
  raise Mongoid::Errors::Validations.new(self)
end
aasm_read_attribute(name) click to toggle source
# File lib/aasm/persistence/mongoid_persistence.rb, line 79
def aasm_read_attribute(name)
  read_attribute(name)
end
aasm_save() click to toggle source
# File lib/aasm/persistence/mongoid_persistence.rb, line 57
def aasm_save
  self.save
end
aasm_supports_transactions?() click to toggle source
# File lib/aasm/persistence/mongoid_persistence.rb, line 65
def aasm_supports_transactions?
  false
end
aasm_update_column(attribute_name, value) click to toggle source
# File lib/aasm/persistence/mongoid_persistence.rb, line 69
def aasm_update_column(attribute_name, value)
  if Mongoid::VERSION.to_f >= 4
    set(Hash[attribute_name, value])
  else
    set(attribute_name, value)
  end

  true
end
aasm_write_attribute(name, value) click to toggle source
# File lib/aasm/persistence/mongoid_persistence.rb, line 83
def aasm_write_attribute(name, value)
  write_attribute(name, value)
end