Sha256: 622964fba1bf57b6655b9dfcc867c035053e496d2c08bdc90fb03c36c72276ea

Contents?: true

Size: 776 Bytes

Versions: 4

Compression:

Stored size: 776 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

4 entries across 4 versions & 1 rubygems

Version Path
stateflow-0.2.3 lib/stateflow/persistence/mongoid.rb
stateflow-0.2.2 lib/stateflow/persistence/mongoid.rb
stateflow-0.2.1 lib/stateflow/persistence/mongoid.rb
stateflow-0.1.2 lib/stateflow/persistence/mongoid.rb