Sha256: 56ab6b45b3df88fd32c6017f37bdcd81faaeea67f8607475a8ee57eeb6da7a7c
Contents?: true
Size: 936 Bytes
Versions: 1
Compression:
Stored size: 936 Bytes
Contents
module Mongoid module SortedRelations extend ActiveSupport::Concern def self.included(base) base.after_initialize :freeze_relation_ids end def freeze_relation_ids @cache_sorted_documents = {} @cache_sorted_ids = {} self.relations.each do |k,v| if [:references_many, :references_and_referenced_in_many].include? v.macro @cache_sorted_ids[v.key] = [self.send(v.key)].flatten.map{ |rid| rid.to_s } self.class.send(:define_method, "sorted_#{v.name}") { sorted_relation v } end end end def sorted_relation(relation) if not @cache_sorted_documents[relation.name] documents = self.send(relation.name).sort_by { |x| @cache_sorted_ids[relation.key].index(x.id.to_s) } @cache_sorted_documents[relation.name] = Mongoid::FakeCriteria.new(documents) end return @cache_sorted_documents[relation.name] end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mongoid-sorted-relations-0.0.3 | lib/mongoid/sorted_relations.rb |