# encoding: utf-8
require 'libis/workflow/mongoid/work_item_base'

module Libis
  module Workflow
    module Mongoid

      module WorkItem

        def self.included(klass)
          klass.class_eval do
            include Libis::Workflow::Mongoid::WorkItemBase

            store_in collection: 'workflow_items'

            has_many :items, inverse_of: :parent, class_name: klass.to_s,
                     dependent: :destroy, autosave: true, order: :created_at.asc
            belongs_to :parent, inverse_of: :items, class_name: klass.to_s

            def klass.run_class(run_klass)
              belongs_to :run, inverse_of: :items, class_name: run_klass.to_s
            end


          end
        end

        def parent
          self[:parent] || self[:run]
        end

        def get_run
          p = self[:parent]
          p ? p.get_run : self[:run]
        end

        def get_root
          self[:parent].get_root rescue self
        end

      end

    end
  end
end

# encoding: utf-8
require 'libis/workflow/mongoid/work_item_base'

module Libis
  module Workflow
    module Mongoid

      module WorkItem

        def self.included(klass)
          klass.class_eval do
            include Libis::Workflow::Mongoid::WorkItemBase

            store_in collection: 'workflow_items'

            has_many :items, inverse_of: :parent, class_name: klass.to_s,
                     dependent: :destroy, autosave: true, order: :_id.asc
            belongs_to :parent, inverse_of: :items, class_name: klass.to_s

            def klass.run_class(run_klass)
              belongs_to :run, inverse_of: :items, class_name: run_klass.to_s
            end


          end
        end

        def parent
          self[:parent] || self[:run]
        end

        def get_run
          p = self[:parent]
          p ? p.get_run : self[:run]
        end

        def get_root
          self[:parent].get_root rescue self
        end

      end

    end
  end
end

Fix for Mongoid 3 reference sorting. Removal of created_by field caused workaround for Mongoid 3 bug to stop working.
Fix for Mongoid 3 reference sorting. Removal of created_by field caused workaround for Mongoid 3 bug to stop working.
