Sha256: 079065f60bd14032f79e387fe6a49b6518f3f8dfcff1e0660ff4a39aa13a6ba9

Contents?: true

Size: 983 Bytes

Versions: 4

Compression:

Stored size: 983 Bytes

Contents

module StateMachinable
  module Model
    extend ActiveSupport::Concern

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

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

    included do
      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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
state_machinable-3.1.0 lib/state_machinable/model.rb
state_machinable-3.1.0.pre lib/state_machinable/model.rb
state_machinable-3.0.0 lib/state_machinable/model.rb
state_machinable-3.0.0.pre.rc0 lib/state_machinable/model.rb