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 +1 -1
- data/bullet.gemspec +1 -1
- data/lib/bullet/active_record.rb +2 -0
- data/lib/bullet/counter.rb +35 -4
- data/spec/bullet_counter_spec.rb +6 -1
- metadata +2 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.5.
|
1
|
+
1.5.4
|
data/bullet.gemspec
CHANGED
data/lib/bullet/active_record.rb
CHANGED
@@ -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
|
|
data/lib/bullet/counter.rb
CHANGED
@@ -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
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
data/spec/bullet_counter_spec.rb
CHANGED
@@ -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
|
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.
|
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.
|
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
|