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: 69e774d016a1d4b507eb55f8f0f9504cbf198a3b
4
- data.tar.gz: abbd2d57baa8ebdf82d65ce8f6080febafaaaf57
3
+ metadata.gz: a7893887f07e1bbe2bc7da27c608db42bc940882
4
+ data.tar.gz: ad7e88db1ae15690c451bdbf1d070db28d7ebf57
5
5
  SHA512:
6
- metadata.gz: 7ef3dacda31025b0d12def2faa3b14a75b43ab114a0389489a7962bc0e1c53542402991e0809198192ba8c28cc77c0bfd56c7533f79ca05a5cc6fe0406c65b94
7
- data.tar.gz: 30460a520bfd15c0f131ec19091415976834fb0b876bc4c362148bc604f34a71827b39e76e33f00d1f07d03f8e4148e1e480191829e007ad4daad26dad74a73c
6
+ metadata.gz: 49e9c595905967d652903f6eb152b5d1f2e95d38976f88ea8a3928fd49db8639a95ebaaaa57d3867abdc7fdf0a2f6d60ab0ac7a80719599f08bf4fa3def6b408
7
+ data.tar.gz: 2b08a6920e97ce7a60a8faa7e852530276d41ecf87e949518b14d2ddc5a37faa023fa12c2262f60aa827d5259498f04e70aa2d82e18baf8b2500ddc9b796eed2
@@ -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
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module HasCount
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -21,6 +21,7 @@ module ActiveRecord
21
21
 
22
22
  class AssociationReflection
23
23
  include HasCount
24
+ alias_method_chain :klass, :has_count
24
25
  alias_method_chain :association_class, :has_count
25
26
  end
26
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-has_count
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Kokubun