Sha256: 0d0e0a407f241a758ecdfee7d20e97ebeceda810d07b92ec6fa1b466ffb197a3

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

require_relative "../exceptions"

module Statesman
  module Adapters
    class ActiveRecord
      attr_reader :transition_class
      attr_reader :parent_model

      def initialize(transition_class, parent_model, observer)
        unless transition_class.serialized_attributes.include?("metadata")
          raise UnserializedMetadataError,
                "#{transition_class.name}#metadata is not serialized"
        end
        @transition_class = transition_class
        @parent_model = parent_model
        @observer = observer
      end

      def create(from, to, metadata = {})
        transition = transitions_for_parent.build(to_state: to,
                                                  sort_key: next_sort_key,
                                                  metadata: metadata)

        ::ActiveRecord::Base.transaction do
          @observer.execute(:before, from, to, transition)
          transition.save!
          @observer.execute(:after, from, to, transition)
          @last_transition = nil
        end
        @observer.execute(:after_commit, from, to, transition)

        transition
      end

      def history
        transitions_for_parent.order(:sort_key)
      end

      def last
        @last_transition ||= transitions_for_parent.order(:sort_key).last
      end

      private

      def transitions_for_parent
        @parent_model.send(@transition_class.table_name)
      end

      def next_sort_key
        (last && last.sort_key + 10) || 0
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
statesman-0.5.0 lib/statesman/adapters/active_record.rb
statesman-0.4.0 lib/statesman/adapters/active_record.rb