Sha256: edaac6a3af6a56140b50ebdd6f957d3b28813de573bf38a45d576c31986139d5

Contents?: true

Size: 763 Bytes

Versions: 2

Compression:

Stored size: 763 Bytes

Contents

module BigMachine
  module ActiveRecord
    extend ActiveSupport::Concern

    included do
      class_attribute :state_attribute
      after_initialize :set_current_state_from_db
    end

    module ClassMethods
      def big_machine(options = {})
        super options
        self.state_attribute = options[:state_attribute] || 'state'
      end
    end

    def set_current_state_from_db
      return unless self.class.initial_state_class

      attribute = send state_attribute

      state_class = attribute ? attribute.constantize : self.class.initial_state_class
      set_current_state(state_class)
    end

    def set_current_state(new_state_class)
      super(new_state_class)
      send "#{state_attribute}=", new_state_class.name
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
big_machine-1.2.0 lib/big_machine/active_record.rb
big_machine-1.1.1 lib/big_machine/active_record.rb