Module: AASM::ClassMethods

Defined in:
lib/aasm/aasm.rb

Instance Method Summary collapse

Instance Method Details

#aasm(*args, &block) ⇒ Object

this is the entry point for all state and event definitions



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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::StateMachine[self][state_machine_name] ||= AASM::StateMachine.new(state_machine_name)

  @aasm ||= {}
  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::Base.new(
      self,
      state_machine_name,
      AASM::StateMachine[self][state_machine_name],
      options
    )
  end
  @aasm[state_machine_name].instance_eval(&block) if block # new DSL
  @aasm[state_machine_name]
end

#inherited(base) ⇒ Object

make sure inheritance (aka subclassing) works with AASM



19
20
21
22
23
24
25
# File 'lib/aasm/aasm.rb', line 19

def inherited(base)
  AASM::StateMachine[base] = {}
  AASM::StateMachine[self].keys.each do |state_machine_name|
    AASM::StateMachine[base][state_machine_name] = AASM::StateMachine[self][state_machine_name].clone
  end
  super
end