module Workflow::ActiveModelPersistence
Public Class Methods
happy_to_be_included_in?(klass)
click to toggle source
# File lib/workflow/active_model_persistence.rb, line 4 def self.happy_to_be_included_in?(klass) Object.const_defined?(:ActiveRecord) and klass.is_a? ActiveRecord::Base end
included(klass)
click to toggle source
# File lib/workflow/active_model_persistence.rb, line 8 def self.included(klass) klass.before_validation :write_initial_state end
Public Instance Methods
load_workflow_state()
click to toggle source
# File lib/workflow/active_model_persistence.rb, line 12 def load_workflow_state read_attribute(self.class.workflow_column) end
persist_workflow_state(new_value)
click to toggle source
On transition the new workflow state is immediately saved in the database.
# File lib/workflow/active_model_persistence.rb, line 18 def persist_workflow_state(new_value) update_attribute self.class.workflow_column, new_value 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/active_model_persistence.rb, line 29 def write_initial_state write_attribute self.class.workflow_column, current_state.to_s end