geekq / workflow

Ruby finite-state-machine-inspired API for modeling workflow
MIT License
1.75k stars 207 forks source link

Define workflow with a method in Rails #174

Closed emclab closed 2 years ago

emclab commented 8 years ago

In our Rails 4.2 app. model payment_request has a workflow which is defined within the model. The workflow is defined as:

module PaymentRequestx require 'workflow' class PaymentRequest < ActiveRecord::Base include Workflow
workflow do

workflow definition

end  

end

The above code worked fine. In order to pass a session variable into model, a method add_wf is defined around the workflow in model payment_request:

module PaymentRequestx require 'workflow' class PaymentRequest < ActiveRecord::Base include Workflow

def self.add_wf(session_token)
  workflow do
    #workflow definition
  end
end    

end

The method add_wf is called in before_action in application_controller.

PaymentRequest.add_wf(session_token)

The problem is workflow is not defined after execution. I also tried the active record base:

def self.add_wf(session_token)
  def self.included(base)
    base.workflow do
      #workflow definition
    end
  end
end    

The workflow is still not defined. I guess problem is that the workflow do loop is not executed within the context of model payment request. How to force the execution of workflow do loop within the context of model PaymentRequest?