Sha256: 1e843175be177fcf399995529dc3d8563cbfa90058272d4569929abf300b977b

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require "state_machinable/version"
require 'active_support'
require "state_machinable/base_state_machine"

module StateMachinable
  extend ActiveSupport::Concern

  class_methods do
    def state_machine_class
      "#{self}StateMachine".constantize
    end

    def transition_class
      "#{self}Transition".constantize
    end
  end

  included do
    before_save :send_ev_before_save, :if => Proc.new { |obj| obj.changed? }
    after_save :send_ev_after_save, :if => Proc.new { |obj| !obj.id_changed? && obj.changed? }
    after_save :transition_to_initial_state, :if => Proc.new { |obj| obj.id_changed? }
    delegate :can_transition_to?, :transition_to!, :transition_to, :to => :state_machine

    def state_machine
      @state_machine ||= self.class.state_machine_class.new(self, :transition_class => self.class.transition_class)
    end

    def send_ev_before_save
      self.state_machine.send('ev_before_save')
    end

    def send_ev_after_save
      self.state_machine.send('ev_after_save')
    end

    private def transition_to_initial_state
      if !self.respond_to?(:skip_state_machine?) || !self.skip_state_machine?
        self.transition_to!(self.state_machine.class.successors['initial'].first)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
state_machinable-0.0.3 lib/state_machinable.rb