Module: AASM::Persistence::MongoMapperPersistence

Defined in:
lib/aasm/persistence/mongo_mapper_persistence.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods, ReadState, WriteState, WriteStateWithoutPersistence

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

This method:

  • extends the model with ClassMethods

  • includes InstanceMethods

Unless the corresponding methods are already defined, it includes

  • ReadState

  • WriteState

  • WriteStateWithoutPersistence

Adds

before_validation :aasm_ensure_initial_state

As a result, it doesn’t matter when you define your methods - the following 2 are equivalent

class Foo
  include MongoMapper::Document
  def aasm_write_state(state)
    "bar"
  end
  include AASM
end

class Foo
  include MongoMapper::Document
  include AASM
  def aasm_write_state(state)
    "bar"
  end
end


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/aasm/persistence/mongo_mapper_persistence.rb', line 36

def self.included(base)
  base.extend AASM::Persistence::MongoMapperPersistence::ClassMethods
  base.send(:include, AASM::Persistence::MongoMapperPersistence::InstanceMethods)
  base.send(:include, AASM::Persistence::MongoMapperPersistence::ReadState) unless base.method_defined?(:aasm_read_state)
  base.send(:include, AASM::Persistence::MongoMapperPersistence::WriteState) unless base.method_defined?(:aasm_write_state)
  base.send(:include, AASM::Persistence::MongoMapperPersistence::WriteStateWithoutPersistence) unless base.method_defined?(:aasm_write_state_without_persistence)

  # Mongoid's Validatable gem dependency goes not have a before_validation_on_xxx hook yet.
  # base.before_validation_on_create :aasm_ensure_initial_state
  base.before_validation :aasm_ensure_initial_state
end