Sha256: 536faba761df038ce3e6db794b3f68e2c90203cebfa9e7548903be5337e9f538

Contents?: true

Size: 730 Bytes

Versions: 2

Compression:

Stored size: 730 Bytes

Contents

module Mongoid
  module EdgeStateMachine
    extend ActiveSupport::Concern

    included do
      include ::EdgeStateMachine
      after_initialize :set_initial_state
      validates_presence_of :state
      validate :state_inclusion
    end

    protected

    def write_state(state_machine, state)
      self.state = state.to_s
      save!
    end

    def read_state(state_machine)
      self.state.to_sym
    end

    def set_initial_state
      self.state ||= self.class.state_machine.initial_state.to_s
    end

    def state_inclusion
      unless self.class.state_machine.states.map{|s| s.name.to_s }.include?(self.state.to_s)
        self.errors.add(:state, :inclusion, :value => self.state)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
edge-state-machine-0.0.2 lib/mongoid/edge-state-machine.rb
edge-state-machine-0.0.1 lib/mongoid/edge-state-machine.rb