eager_group 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/CHANGELOG.md +4 -0
- data/README.md +6 -2
- data/eager_group.gemspec +1 -1
- data/lib/eager_group/preloader.rb +13 -5
- data/lib/eager_group/version.rb +1 -1
- metadata +16 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4192329fcb4dca939b7cd36ba3685a5357012c2
|
4
|
+
data.tar.gz: 9401d2152d653e43f1fa5a38fc67f69609fa28cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b69e18e374729d70ea2e6256688418e85667e136c2fa8f04ae861806eef55621267b27ab1842c98b704eef1522dce585ee07f89abdbd726fffaade8eb6205149
|
7
|
+
data.tar.gz: 1adc08def5c93538bc39349ab44d8e662fa23399284c1a860eb206afa7306e6496d5963f976d06ce1e6e162ac4748b55f804c0ae0b8aac5fa767d54074f335e6
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://secure.travis-ci.org/xinminlabs/eager_group.png)](http://travis-ci.org/xinminlabs/eager_group)
|
4
4
|
|
5
|
+
[More explaination on our blog](http://blog.xinminlabs.com/2015/06/29/eager_group/)
|
6
|
+
|
5
7
|
Fix n+1 aggregate sql functions for rails, like
|
6
8
|
|
7
9
|
SELECT "posts".* FROM "posts";
|
@@ -26,6 +28,8 @@ or
|
|
26
28
|
SELECT "posts".* FROM "posts";
|
27
29
|
SELECT AVG("comments"."rating") AS average_comments_rating, post_id AS post_id FROM "comments" WHERE "comments"."post_id" IN (1, 2, 3) GROUP BY post_id;
|
28
30
|
|
31
|
+
It only supports Rails 4.x so far.
|
32
|
+
|
29
33
|
## Installation
|
30
34
|
|
31
35
|
Add this line to your application's Gemfile:
|
@@ -47,14 +51,14 @@ Or install it yourself as:
|
|
47
51
|
First you need to define what aggregate function you want to eager
|
48
52
|
load.
|
49
53
|
|
50
|
-
class Post
|
54
|
+
class Post < ActiveRecord::Base
|
51
55
|
has_many :comments
|
52
56
|
|
53
57
|
define_eager_group :comments_average_rating, :comments, :average, :rating
|
54
58
|
define_eager_group :approved_comments_count, :comments, :count, :*, -> { approved }
|
55
59
|
end
|
56
60
|
|
57
|
-
class Comment
|
61
|
+
class Comment < ActiveRecord::Base
|
58
62
|
belongs_to :post
|
59
63
|
|
60
64
|
scope :approved, -> { where(status: 'approved') }
|
data/eager_group.gemspec
CHANGED
@@ -20,11 +20,11 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
21
|
spec.require_paths = ["lib"]
|
22
22
|
|
23
|
-
spec.add_dependency "activerecord"
|
24
23
|
spec.add_development_dependency "bundler"
|
25
24
|
spec.add_development_dependency "rake", "~> 10.0"
|
26
25
|
spec.add_development_dependency "rspec", "~> 3.3"
|
27
26
|
spec.add_development_dependency "sqlite3", "~> 1.3"
|
27
|
+
spec.add_development_dependency "activerecord"
|
28
28
|
spec.add_development_dependency "activerecord-import"
|
29
29
|
spec.add_development_dependency "benchmark-ips"
|
30
30
|
end
|
@@ -13,12 +13,20 @@ module EagerGroup
|
|
13
13
|
@eager_group_values.each do |eager_group_value|
|
14
14
|
definition = @klass.eager_group_definations[eager_group_value]
|
15
15
|
if definition
|
16
|
-
|
17
|
-
association_class =
|
16
|
+
reflection = @klass.reflect_on_association(definition.association)
|
17
|
+
association_class = reflection.class_name.constantize
|
18
18
|
association_class = association_class.instance_exec(&definition.scope) if definition.scope
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
if reflection.through_reflection
|
20
|
+
foreign_key = "#{reflection.through_reflection.name}.#{reflection.through_reflection.foreign_key}"
|
21
|
+
aggregate_hash = association_class.joins(reflection.through_reflection.name)
|
22
|
+
.where("#{foreign_key} IN (?)", record_ids)
|
23
|
+
.group("#{foreign_key}")
|
24
|
+
.send(definition.aggregate_function, definition.column_name)
|
25
|
+
else
|
26
|
+
aggregate_hash = association_class.where(reflection.foreign_key => record_ids)
|
27
|
+
.group(reflection.foreign_key)
|
28
|
+
.send(definition.aggregate_function, definition.column_name)
|
29
|
+
end
|
22
30
|
@records.each do |record|
|
23
31
|
id = record.send primary_key
|
24
32
|
record.send "#{eager_group_value}=", aggregate_hash[id]
|
data/lib/eager_group/version.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eager_group
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: activerecord
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: bundler
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,6 +66,20 @@ dependencies:
|
|
80
66
|
- - "~>"
|
81
67
|
- !ruby/object:Gem::Version
|
82
68
|
version: '1.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activerecord
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: activerecord-import
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|