Sha256: 7ce5dff68a6a82446e9ac4db0e6e057bda6618bf870298c8d842ad5dade029c2

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 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_attribute(self.class.workflow_column, new_value)
    end

    private
      def write_initial_state
        update_attribute(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

1 entries across 1 versions & 1 rubygems

Version Path
workflow_on_mongoid-0.8.0.2 lib/workflow_on_mongoid/workflow.rb