module Workflow::MongoidPersistence

Public Class Methods

happy_to_be_included_in?(klass) click to toggle source
# File lib/workflow/mongoid_persistence.rb, line 4
def self.happy_to_be_included_in?(klass)
  Object.const_defined?(:Mongoid) and klass.include? Mongoid::Document
end
included(klass) click to toggle source
# File lib/workflow/mongoid_persistence.rb, line 8
def self.included(klass)
  klass.after_initialize :write_initial_state
end

Public Instance Methods

load_workflow_state() click to toggle source
# File lib/workflow/mongoid_persistence.rb, line 12
def load_workflow_state
  read_attribute(self.class.workflow_column)
end
persist_workflow_state(new_value) click to toggle source

implementation of abstract method: saves new workflow state to DB

# File lib/workflow/mongoid_persistence.rb, line 17
def persist_workflow_state(new_value)
  self.write_attribute(self.class.workflow_column, new_value.to_s)
  self.save!
end

Private Instance Methods

write_initial_state() click to toggle source

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.

# File lib/workflow/mongoid_persistence.rb, line 29
def write_initial_state
  write_attribute self.class.workflow_column, current_state.to_s
end