Sha256: a073f9d0b25f2e1e18ddb6915f5f7a4601a7556b2e935536a5d1b4c215654512

Contents?: true

Size: 684 Bytes

Versions: 1

Compression:

Stored size: 684 Bytes

Contents

module Mongoid
  module SortedRelations
    extend ActiveSupport::Concern

    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      def init_sorted_relations
        self.relations.each do |k,v|
          if [:references_many, :references_and_referenced_in_many].include? v.macro
            send(:define_method, "sorted_#{v.name}") { sorted_relation v }
          end
        end
      end
    end

    def sorted_relation(relation)
      ids = self.send(relation.key).map{ |rid| rid.to_s }

      documents = self.send(relation.name).sort_by { |x| ids.index(x.id.to_s) }

      return Mongoid::FakeCriteria.new(documents)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-sorted-relations-0.0.1 lib/mongoid/sorted_relations.rb