Sha256: 7d5ce6c25c911968393261afe01599c2a253e63d0287ec2962ede931c576edb4

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

require 'workflow'

module Workflow
  module MongoidInstanceMethods
    def load_workflow_state
      send(self.class.workflow_column)
    end

    def persist_workflow_state(new_value)
      self.update_attributes!(self.class.workflow_column => new_value)
    end    
    
    private
      def write_initial_state
        update_attributes(self.class.workflow_column => current_state.to_s) if load_workflow_state.blank?
      end    
  end

  def self.included(klass)
    klass.send :include, WorkflowInstanceMethods
    klass.extend WorkflowClassMethods
    if Object.const_defined?(:ActiveRecord) && klass < ActiveRecord::Base
      klass.send :include, ActiveRecordInstanceMethods
      klass.before_validation :write_initial_state
    elsif Object.const_defined?(:Remodel) && klass < Remodel::Entity
      klass.send :include, RemodelInstanceMethods
    elsif Object.const_defined?(:Mongoid) && klass < Mongoid::Document
      klass.class_eval do
        klass.send :field, klass.workflow_column
        klass.send :include, MongoidInstanceMethods
        klass.before_validation :write_initial_state        
      end
    end
  end  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
workflow_on_mongoid-0.8.0.1 lib/workflow_on_mongoid/workflow.rb
workflow_on_mongoid-0.8.0 lib/workflow_on_mongoid/workflow.rb
workflow_on_mongoid-0.7.0.1 lib/workflow_on_mongoid/workflow.rb
workflow_on_mongoid-0.7.0 lib/workflow_on_mongoid/workflow.rb