Sha256: b523253f08137cfcf314cc5fc0ecbe81b094186aeec46a71c9c08d828114e567

Contents?: true

Size: 1.02 KB

Versions: 4

Compression:

Stored size: 1.02 KB

Contents

module Workflow
  module ActiveModelPersistence

    def self.happy_to_be_included_in?(klass)
      Object.const_defined?(:ActiveRecord) and klass.is_a? ActiveRecord::Base
    end

    def self.included(klass)
      klass.before_validation :write_initial_state
    end

    def load_workflow_state
      read_attribute(self.class.workflow_column)
    end

    # On transition the new workflow state is immediately saved in the
    # database.
    def persist_workflow_state(new_value)
      update_attribute self.class.workflow_column, new_value
    end

    private

    # Motivation: even if NULL is stored in the workflow_state database column,
    # the current_state is correctly recognized in the Ruby code. The problem
    # arises when you want to SELECT records filtering by the value of initial
    # state. That's why it is important to save the string with the name of the
    # initial state in all the new records.
    def write_initial_state
      write_attribute self.class.workflow_column, current_state.to_s
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
validating-workflow-0.7.12 lib/workflow/active_model_persistence.rb
validating-workflow-0.7.11 lib/workflow/active_model_persistence.rb
validating-workflow-0.7.10 lib/workflow/active_model_persistence.rb
validating-workflow-0.7.9 lib/workflow/active_model_persistence.rb