activerecord-precounter 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e486607a409dc25b776267ae02f8bd0ef8f801f5
4
- data.tar.gz: 38667cea1241dc1820e77289185a6102b031af3e
3
+ metadata.gz: 8c85fec9116becd14760b8726df821d422085a4c
4
+ data.tar.gz: 5c216798df88c22adfe212b5695e4667600ded38
5
5
  SHA512:
6
- metadata.gz: 98e0d0d94a90f233250936bb69ce3e5f9407f30687097c10afff7168710a05bb249de0611ca916700ac04400690f2c5187b694bd14b476a11d13a776a4530d03
7
- data.tar.gz: 8c9bdebda2f932c1f7ed4b699020b4528d918a24b5335a7954a4a24bec269ababa1d6cb24484aeff4b0a6ede03f7ea17a50736b9c9b7742cf3de6c8d62f74cc6
6
+ metadata.gz: a472ba502fb21376132e0c8742a50d83c2ac4dc09b39115b592071d26fd2ef5ab20fe94cd2f76db66039e00ebea05504bb36e376d7222e49a1bc335d394cc95f
7
+ data.tar.gz: 5c6f1140da169bb55a909fb35b1cee56e5371a772b7c729324c353a992f646bd85730e1c225b3a613701bdd4aa51dc1d0ac1f0a38d7b2bbf52596f7e9bc03495
data/README.md CHANGED
@@ -36,7 +36,7 @@ Like `preload`, it loads counts by multiple queries
36
36
 
37
37
  ```rb
38
38
  tweets = Tweet.all
39
- ActiveRecord::Precounter.new(tweets, :favorites).precount
39
+ ActiveRecord::Precounter.new(tweets).precount(:favorites)
40
40
  tweets.each do |tweet|
41
41
  p tweet.favorites_count
42
42
  end
@@ -55,7 +55,7 @@ gem 'activerecord-precounter'
55
55
  ## Limitation
56
56
 
57
57
  Target `has_many` association must have inversed `belongs_to`.
58
- i.e. `ActiveRecord::Precounter.new(tweets, :favorites).precount` needs both `Tweet.has_many(:favorites)` and `Favorite.belongs_to(:tweet)`.
58
+ i.e. `ActiveRecord::Precounter.new(tweets).precount(:favorites)` needs both `Tweet.has_many(:favorites)` and `Favorite.belongs_to(:tweet)`.
59
59
 
60
60
  Unlike [activerecord-precount](https://github.com/k0kubun/activerecord-precount), the cache store is not ActiveRecord association and it does not utilize ActiveRecord preloader.
61
61
  Thus you can't use `preload` to eager load counts for nested associations. And currently there's no JOIN support.
data/ci/travis.rb CHANGED
File without changes
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  class Precounter
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -5,19 +5,20 @@ module ActiveRecord
5
5
  class MissingInverseOf < StandardError; end
6
6
 
7
7
  # @param [ActiveRecord::Relation] relation - Parent resources relation.
8
- # @param [Array<String,Symbol>] association_names - Eager loaded association names. e.g. `[:users, :likes]`
9
- def initialize(relation, *association_names)
8
+ def initialize(relation)
10
9
  @relation = relation
11
- @association_names = association_names.map(&:to_s)
12
10
  end
13
11
 
12
+ # @param [Array<String,Symbol>] association_names - Eager loaded association names. e.g. `[:users, :likes]`
14
13
  # @return [Array<ActiveRecord::Base>]
15
- def precount
14
+ def precount(*association_names)
16
15
  records = @relation.to_a
17
16
  return if records.empty?
18
17
 
19
- @association_names.each do |association_name|
18
+ association_names.each do |association_name|
19
+ association_name = association_name.to_s
20
20
  reflection = @relation.klass.reflections.fetch(association_name)
21
+
21
22
  if reflection.inverse_of.nil?
22
23
  raise MissingInverseOf.new(
23
24
  "`#{reflection.klass}` does not have inverse of `#{@relation.klass}##{reflection.name}`. "\
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-precounter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Kokubun