flyerhzm-bullet 1.5.3 → 1.5.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.5.3
1
+ 1.5.4
data/bullet.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{bullet}
8
- s.version = "1.5.3"
8
+ s.version = "1.5.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Richard Huang"]
@@ -12,8 +12,10 @@ module Bullet
12
12
  if records
13
13
  if records.size > 1
14
14
  Bullet::Association.add_possible_objects(records)
15
+ Bullet::Counter.add_possible_objects(records)
15
16
  elsif records.size == 1
16
17
  Bullet::Association.add_impossible_object(records.first)
18
+ Bullet::Counter.add_impossible_object(records)
17
19
  end
18
20
  end
19
21
 
@@ -8,6 +8,8 @@ module Bullet
8
8
 
9
9
  def end_request
10
10
  @@klazz_associations = nil
11
+ @@possible_objects = nil
12
+ @@impossible_objects = nil
11
13
  end
12
14
 
13
15
  def need_counter_caches?
@@ -36,10 +38,26 @@ module Bullet
36
38
 
37
39
  def add_counter_cache(object, associations)
38
40
  klazz = object.class
39
- klazz_associations[klazz] ||= []
40
- klazz_associations[klazz] << associations
41
- klazz_associations[klazz].flatten!
42
- klazz_associations[klazz].uniq!
41
+ if (!possible_objects[klazz].nil? and possible_objects[klazz].include?(object)) and
42
+ (impossible_objects[klazz].nil? or !impossible_objects[klazz].include?(object))
43
+ klazz_associations[klazz] ||= []
44
+ klazz_associations[klazz] << associations
45
+ unique(klazz_associations[klazz])
46
+ end
47
+ end
48
+
49
+ def add_possible_objects(objects)
50
+ klazz= objects.first.class
51
+ possible_objects[klazz] ||= []
52
+ possible_objects[klazz] << objects
53
+ unique(possible_objects[klazz])
54
+ end
55
+
56
+ def add_impossible_object(object)
57
+ klazz = object.class
58
+ impossible_objects[klazz] ||= []
59
+ impossible_objects[klazz] << object
60
+ impossible_objects[klazz].uniq!
43
61
  end
44
62
 
45
63
  private
@@ -53,6 +71,11 @@ module Bullet
53
71
  end
54
72
  messages
55
73
  end
74
+
75
+ def unique(array)
76
+ array.flatten!
77
+ array.uniq!
78
+ end
56
79
 
57
80
  def call_stack_messages
58
81
  []
@@ -61,6 +84,14 @@ module Bullet
61
84
  def klazz_associations
62
85
  @@klazz_associations ||= {}
63
86
  end
87
+
88
+ def possible_objects
89
+ @@possible_objects ||= {}
90
+ end
91
+
92
+ def impossible_objects
93
+ @@impossible_objects ||= {}
94
+ end
64
95
  end
65
96
  end
66
97
  end
@@ -60,12 +60,17 @@ describe Bullet::Counter do
60
60
  end
61
61
  Bullet::Counter.should be_need_counter_caches
62
62
  end
63
+
64
+ it "should not need coounter cache with only one object" do
65
+ Country.first.cities.size
66
+ Bullet::Counter.should_not be_need_counter_caches
67
+ end
63
68
 
64
69
  it "should not need counter cache with part of cities" do
65
70
  Country.all.each do |country|
66
71
  country.cities(:conditions => ["name = ?", 'first']).size
67
72
  end
68
- Bullet::Counter.should_not be_need_counter_cache
73
+ Bullet::Counter.should_not be_need_counter_caches
69
74
  end
70
75
  end
71
76
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flyerhzm-bullet
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.3
4
+ version: 1.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
@@ -42,7 +42,6 @@ files:
42
42
  - tasks/bullet_tasks.rake
43
43
  has_rdoc: false
44
44
  homepage: http://github.com/flyerhzm/bullet
45
- licenses:
46
45
  post_install_message:
47
46
  rdoc_options:
48
47
  - --charset=UTF-8
@@ -63,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
62
  requirements: []
64
63
 
65
64
  rubyforge_project:
66
- rubygems_version: 1.3.5
65
+ rubygems_version: 1.2.0
67
66
  signing_key:
68
67
  specification_version: 3
69
68
  summary: A plugin to kill N+1 queries and unused eager loading