module AASM::ClassMethods

Public Instance Methods

aasm(*args, &block) click to toggle source

this is the entry point for all state and event definitions

# File lib/aasm/aasm.rb, line 28
def aasm(*args, &block)
  if args[0].is_a?(Symbol) || args[0].is_a?(String)
    # using custom name
    state_machine_name = args[0].to_sym
    options = args[1] || {}
  else
    # using the default state_machine_name
    state_machine_name = :default
    options = args[0] || {}
  end

  AASM::StateMachineStore.fetch(self, true).register(state_machine_name, AASM::StateMachine.new(state_machine_name))

  # use a default despite the DSL configuration default.
  # this is because configuration hasn't been setup for the AASM class but we are accessing a DSL option already for the class.
  aasm_klass = options[:with_klass] || AASM::Base

  raise ArgumentError, "The class #{aasm_klass} must inherit from AASM::Base!" unless aasm_klass.ancestors.include?(AASM::Base)

  @aasm ||= Concurrent::Map.new
  if @aasm[state_machine_name]
    # make sure to use provided options
    options.each do |key, value|
      @aasm[state_machine_name].state_machine.config.send("#{key}=", value)
    end
  else
    # create a new base
    @aasm[state_machine_name] = aasm_klass.new(
      self,
      state_machine_name,
      AASM::StateMachineStore.fetch(self, true).machine(state_machine_name),
      options
    )
  end
  @aasm[state_machine_name].instance_eval(&block) if block # new DSL
  @aasm[state_machine_name]
end
inherited(base) click to toggle source

make sure inheritance (aka subclassing) works with AASM

Calls superclass method
# File lib/aasm/aasm.rb, line 21
def inherited(base)
  AASM::StateMachineStore.register(base, self)

  super
end