activerecord-has_count 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7893887f07e1bbe2bc7da27c608db42bc940882
|
4
|
+
data.tar.gz: ad7e88db1ae15690c451bdbf1d070db28d7ebf57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49e9c595905967d652903f6eb152b5d1f2e95d38976f88ea8a3928fd49db8639a95ebaaaa57d3867abdc7fdf0a2f6d60ab0ac7a80719599f08bf4fa3def6b408
|
7
|
+
data.tar.gz: 2b08a6920e97ce7a60a8faa7e852530276d41ecf87e949518b14d2ddc5a37faa023fa12c2262f60aa827d5259498f04e70aa2d82e18baf8b2500ddc9b796eed2
|
data/benchmarks/benchmark.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "benchmark"
|
2
|
+
require "pry"
|
2
3
|
require "active_record"
|
3
4
|
require "activerecord-import"
|
4
5
|
require "activerecord-has_count"
|
@@ -52,6 +53,12 @@ Benchmark.bmbm do |bench|
|
|
52
53
|
tweets.each { |t| t.replies_count }
|
53
54
|
end
|
54
55
|
|
56
|
+
bench.report("preloaded has_count") do
|
57
|
+
tweets = Tweet.preload(:replies_count).first(TWEET_COUNT)
|
58
|
+
|
59
|
+
tweets.each { |t| t.replies_count }
|
60
|
+
end
|
61
|
+
|
55
62
|
bench.report("size of preloaded association") do
|
56
63
|
tweets = Tweet.preload(:replies).first(TWEET_COUNT)
|
57
64
|
|
@@ -4,7 +4,7 @@ module ActiveRecord
|
|
4
4
|
# Not preloaded behaviour of has_count association
|
5
5
|
# When this method is called, it will be N+1 query
|
6
6
|
def load_target
|
7
|
-
count_target = name_without_count.to_sym
|
7
|
+
count_target = reflection.name_without_count.to_sym
|
8
8
|
@target = owner.association(count_target).count
|
9
9
|
|
10
10
|
loaded! unless loaded?
|
@@ -12,15 +12,6 @@ module ActiveRecord
|
|
12
12
|
rescue ActiveRecord::RecordNotFound
|
13
13
|
reset
|
14
14
|
end
|
15
|
-
|
16
|
-
# This method is used by preloader when grouping preload target's class
|
17
|
-
def klass
|
18
|
-
reflection.active_record.send(:compute_type, name_without_count.singularize.classify)
|
19
|
-
end
|
20
|
-
|
21
|
-
def name_without_count
|
22
|
-
reflection.name.to_s.sub(/_count$/, "")
|
23
|
-
end
|
24
15
|
end
|
25
16
|
end
|
26
17
|
end
|
@@ -13,6 +13,19 @@ module ActiveRecord
|
|
13
13
|
|
14
14
|
class AssociationReflection
|
15
15
|
module HasCount
|
16
|
+
def klass_with_has_count
|
17
|
+
case macro
|
18
|
+
when :has_count
|
19
|
+
@klass ||= active_record.send(:compute_type, name_without_count.singularize.classify)
|
20
|
+
else
|
21
|
+
klass_without_has_count
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def name_without_count
|
26
|
+
name.to_s.sub(/_count$/, "")
|
27
|
+
end
|
28
|
+
|
16
29
|
def association_class_with_has_count
|
17
30
|
case macro
|
18
31
|
when :has_count
|