Module: AASM::Persistence::MongoidPersistence

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

Defined Under Namespace

Modules: ClassMethods, InstanceMethods, NamedScopeMethods, 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 Mongoid::Document
  def aasm_write_state(state)
    "bar"
  end
  include AASM
end

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


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/aasm/persistence/mongoid_persistence.rb', line 36

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

  # if base.respond_to?(:named_scope)
  #   base.extend(AASM::Persistence::MongoidPersistence::NamedScopeMethods)
  # 
  #   base.class_eval do
  #     class << self
  #       unless method_defined?(:aasm_state_without_named_scope)
  #         alias_method :aasm_state_without_named_scope, :aasm_state
  #         alias_method :aasm_state, :aasm_state_with_named_scope
  #       end
  #     end
  #   end
  # end

  # 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