activerecord-precounter 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/ci/travis.rb +0 -0
- data/lib/active_record/precounter/version.rb +1 -1
- data/lib/active_record/precounter.rb +6 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c85fec9116becd14760b8726df821d422085a4c
|
4
|
+
data.tar.gz: 5c216798df88c22adfe212b5695e4667600ded38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
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
|
@@ -5,19 +5,20 @@ module ActiveRecord
|
|
5
5
|
class MissingInverseOf < StandardError; end
|
6
6
|
|
7
7
|
# @param [ActiveRecord::Relation] relation - Parent resources relation.
|
8
|
-
|
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
|
-
|
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}`. "\
|