Sha256: 1a173de1442de9c0659bb43469614a79551ab150f446b9e0515a286e6e77cf4c

Contents?: true

Size: 789 Bytes

Versions: 1

Compression:

Stored size: 789 Bytes

Contents

module Stateflow
  module Persistence
    module Mongoid
      def self.install(base)
        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
      
      module InstanceMethods
        def load_from_persistence
          self.send machine.state_column.to_sym
        end

        def save_to_persistence(new_state)
          self.update_attributes(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.1.0 lib/stateflow/persistence/mongoid.rb