mongoid-sorted-relations 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/mongoid/sorted_relations.rb +18 -7
  2. metadata +11 -11
@@ -4,24 +4,35 @@ module Mongoid
4
4
 
5
5
  def self.included(base)
6
6
  base.extend(ClassMethods)
7
+ base.after_initialize :freeze_relation_ids
7
8
  end
8
9
 
9
10
  module ClassMethods
10
11
  def init_sorted_relations
11
12
  self.relations.each do |k,v|
12
- if [:references_many, :references_and_referenced_in_many].include? v.macro
13
- send(:define_method, "sorted_#{v.name}") { sorted_relation v }
14
- end
13
+ send(:define_method, "sorted_#{v.name}") { sorted_relation v } if [:references_many, :references_and_referenced_in_many].include? v.macro
15
14
  end
16
15
  end
17
16
  end
18
17
 
19
- def sorted_relation(relation)
20
- ids = self.send(relation.key).map{ |rid| rid.to_s }
18
+ def freeze_relation_ids
19
+ @cache_sorted_documents = {}
20
+ @cache_sorted_ids = {}
21
21
 
22
- documents = self.send(relation.name).sort_by { |x| ids.index(x.id.to_s) }
22
+ self.relations.each do |k,v|
23
+ if [:references_many, :references_and_referenced_in_many].include? v.macro
24
+ @cache_sorted_ids[v.key] = [self.send(v.key)].flatten.map{ |rid| rid.to_s }
25
+ end
26
+ end
27
+ end
28
+
29
+ def sorted_relation(relation)
30
+ if not @cache_sorted_documents[relation.name]
31
+ documents = self.send(relation.name).sort_by { |x| @cache_sorted_ids[relation.key].index(x.id.to_s) }
32
+ @cache_sorted_documents[relation.name] = Mongoid::FakeCriteria.new(documents)
33
+ end
23
34
 
24
- return Mongoid::FakeCriteria.new(documents)
35
+ return @cache_sorted_documents[relation.name]
25
36
  end
26
37
  end
27
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-sorted-relations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-05-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
16
- requirement: &2168955220 !ruby/object:Gem::Requirement
16
+ requirement: &2160751500 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '2.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2168955220
24
+ version_requirements: *2160751500
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bson_ext
27
- requirement: &2168680680 !ruby/object:Gem::Requirement
27
+ requirement: &2160750920 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '1.6'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2168680680
35
+ version_requirements: *2160750920
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &2168678240 !ruby/object:Gem::Requirement
38
+ requirement: &2160750080 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.8.7
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2168678240
46
+ version_requirements: *2160750080
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &2168675460 !ruby/object:Gem::Requirement
49
+ requirement: &2160539920 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '2.0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2168675460
57
+ version_requirements: *2160539920
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rspec-aspic
60
- requirement: &2168674340 !ruby/object:Gem::Requirement
60
+ requirement: &2160538360 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: 0.0.2
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2168674340
68
+ version_requirements: *2160538360
69
69
  description: Preserve the order of the mongoid documents in a referenced one-to-many
70
70
  or many-to-many relationship.
71
71
  email: