Class: StateFu::Persistence::ActiveRecord

Inherits:
Base show all
Defined in:
lib/persistence/active_record.rb

Instance Attribute Summary

Attributes inherited from Base

#binding, #current_state, #field_name

Class Method Summary collapse

Methods inherited from Base

#find_current_state, #initialize, #klass, #machine, #object, #persist!, #reload, #value

Constructor Details

This class inherits a constructor from StateFu::Persistence::Base

Class Method Details

.prepare_field(klass, field_name) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/persistence/active_record.rb', line 5

def self.prepare_field( klass, field_name )
  _field_name = field_name
  Logging.debug("Preparing ActiveRecord field #{klass}.#{field_name}")

  # this adds a before_save hook to ensure that the field is initialized 
  # (and the initial state set) before create.
  klass.send :before_validation_on_create, :state_fu!
          
  # it's usually a good idea to do this:
  # validates_presence_of _field_name
          
  klass.class_eval do
    # this is a hack to ensure that when you use the same field for the database
    # column and the machine name, you don't end up with an unserializable 
    # StateFu::Binding in record#changes() ...
    def attribute_change(column)
      change = super
      if self.class.respond_to?(:machines) && self.class.machines.keys.include?(column.to_sym) 
        change[1] = read_attribute(column)
      end
      change
    end                
  end
end