title: AASM: Separating the state machine definition from the class definition
body: <p>suppose I have this class (taken directly from the aasm documentation):</p>
  
  <pre><code>class Job &lt; ActiveRecord::Base
    include AASM
  
    aasm do
      state :sleeping, :initial =&gt; true
      state :running
      state :cleaning
  
      event :run do
        transitions :from =&gt; :sleeping, :to =&gt; :running
      end
  
      event :clean do
        transitions :from =&gt; :running, :to =&gt; :cleaning
      end
  
      event :sleep do
        transitions :from =&gt; [:running, :cleaning], :to =&gt; :sleeping
      end
    end
  
  end
  </code></pre>
  
  <p>I don't like a lot the fact that I have my state machine definition mixed with my class definition (since of course in a real project I will add more methods to the Job class).</p>
  
  <p>I would like to separate the state machine definition in a module so that the Job class can be something along the line of:</p>
  
  <pre><code>class Job &lt; ActiveRecord::Base
    include StateMachines::JobStateMachine
  end
  </code></pre>
  
  <p>I then created a job_state_machine.rb file in app/models/state_machines with a content similar to:</p>
  
  <pre><code>module StateMachines::JobStateMachine
    include AASM
  
    aasm do
      state :sleeping, :initial =&gt; true
      state :running
      state :cleaning
  
      event :run do
        transitions :from =&gt; :sleeping, :to =&gt; :running
      end
  
      event :clean do
        transitions :from =&gt; :running, :to =&gt; :cleaning
      end
  
      event :sleep do
        transitions :from =&gt; [:running, :cleaning], :to =&gt; :sleeping
      end
    end
  
  end
  </code></pre>
  
  <p>but this is not working because AASM is being included in the module not in the Job class... I even tried changing the module to:</p>
  
  <pre><code>module StateMachines::JobStateMachine
    def self.included(base)
    include AASM
  
    aasm do
      state :sleeping, :initial =&gt; true
      state :running
      state :cleaning
  
      event :run do
        transitions :from =&gt; :sleeping, :to =&gt; :running
      end
  
      event :clean do
        transitions :from =&gt; :running, :to =&gt; :cleaning
      end
  
      event :sleep do
        transitions :from =&gt; [:running, :cleaning], :to =&gt; :sleeping
      end
    end
    end
  end
  </code></pre>
  
  <p>but still it is not working... any hint or suggestion is very appreciated.</p>
  
  <p>Thanks,
  Ignazio</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p><strong>Thanks to Alto, the correct solution is this:</strong></p>
  
  <pre><code>module StateMachine::JobStateMachine
  
    def self.included(base)
      base.send(:include, AASM)
  
      base.send(:aasm, column: 'status') do
      ....
      end
    end
  end
  </code></pre>
  
  <p>and obviously remember to include the state machine definition in the main class like this:</p>
  
  <pre><code>include StateMachine::JobStateMachine
  </code></pre>