Sha256: 0c4d202205b123cae25b81fb32c23d226335afc9bcb9d8669ca1bf779cbf6a48

Contents?: true

Size: 819 Bytes

Versions: 2

Compression:

Stored size: 819 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, options = {})
          self.send("#{machine.state_column}=".to_sym, new_state)
          self.save if options[:save]
        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

2 entries across 2 versions & 1 rubygems

Version Path
stateflow-0.4.0 lib/stateflow/persistence/mongoid.rb
stateflow-0.3.0 lib/stateflow/persistence/mongoid.rb