Module: AASM::Persistence::ActiveRecordPersistence

Defined in:
lib/aasm/persistence/active_record_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, :on => :create

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

class Foo < ActiveRecord::Base
  def aasm_write_state(state)
    "bar"
  end
  include AASM
end

class Foo < ActiveRecord::Base
  include AASM
  def aasm_write_state(state)
    "bar"
  end
end


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/aasm/persistence/active_record_persistence.rb', line 34

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

  if base.respond_to?(:named_scope)
    base.extend(AASM::Persistence::ActiveRecordPersistence::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
  base.respond_to?(:before_validation_on_create) ? base.before_validation_on_create(:aasm_ensure_initial_state) : base.before_validation(:aasm_ensure_initial_state, :on => :create)
end