Sha256: 12eaf5f084f5fd215881db4b461e2d2ca38b6ba3d35c27cc0ca0a5b4d75e0381

Contents?: true

Size: 853 Bytes

Versions: 1

Compression:

Stored size: 853 Bytes

Contents

module Stateflow
  module Persistence
    module ActiveRecord
      def self.install(base)
        ActiveSupport::Deprecation.silence do
          base.respond_to?(:before_validation_on_create) ? base.before_validation_on_create(:ensure_initial_state) : base.before_validation(:ensure_initial_state, :on => :create)
          base.send :include, InstanceMethods
        end
      end
      
      module InstanceMethods
        def load_from_persistence
          self.send machine.state_column.to_sym
        end

        def save_to_persistence(new_state)
          self.update_attribute(machine.state_column.to_sym, new_state)
        end
        
        def ensure_initial_state
          send("#{self.machine.state_column.to_s}=", self.current_state.name.to_s) if send(self.machine.state_column.to_s).blank?
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stateflow-0.2.3 lib/stateflow/persistence/active_record.rb