eager_group 0.3.0 → 0.5.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 +4 -4
- data/.travis.yml +1 -1
- data/CHANGELOG.md +9 -1
- data/README.md +8 -0
- data/lib/eager_group.rb +18 -0
- data/lib/eager_group/preloader.rb +6 -6
- data/lib/eager_group/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ddfe7747a2b77e9f26cbbbb0029b41acb03b88b
|
4
|
+
data.tar.gz: 5357c3d44af2542594fc5ea593029cd3e3c5cc7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80419fb36e918d83f69487633330ffd607d806abaed2ff0a10ecd28134402968105c336aa31e90d361511dc717f3d3bfa3deb7a8a2892663e2a7efc7348ad862
|
7
|
+
data.tar.gz: ca82b49a3007370aebc5b3f2db9d85d51b3aeda6ee800a16dc20a3bda5a8b136c96bfbcfc7ffeb6523a110ef4083c20859ed0d31abb3cf6df4fe2b450d3d5c04
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Next Release
|
2
2
|
|
3
|
+
## 0.5.0 (09/22/2016)
|
4
|
+
|
5
|
+
* Add magic method for one record
|
6
|
+
|
7
|
+
## 0.4.0 (05/18/2016)
|
8
|
+
|
9
|
+
* Support scope arguments
|
10
|
+
|
3
11
|
## 0.3.0 (10/11/2015)
|
4
12
|
|
5
13
|
* Support polymorphic association
|
@@ -10,4 +18,4 @@
|
|
10
18
|
|
11
19
|
## 0.1.0 (06/29/2015)
|
12
20
|
|
13
|
-
*
|
21
|
+
* First release
|
data/README.md
CHANGED
@@ -87,6 +87,14 @@ when querying
|
|
87
87
|
EagerGroup will execute `GROUP BY` sqls for you then set the value of
|
88
88
|
attributes.
|
89
89
|
|
90
|
+
`define_eager_group` will define a method in model.
|
91
|
+
You can call the `definition_name` directly for convenience,
|
92
|
+
but it would not help you to fix n+1 aggregate sql issue.
|
93
|
+
|
94
|
+
post = Post.first
|
95
|
+
post.commets_average_rating
|
96
|
+
post.approved_comments_count
|
97
|
+
|
90
98
|
## Benchmark
|
91
99
|
|
92
100
|
I wrote a benchmark script [here][1], it queries approved comments count
|
data/lib/eager_group.rb
CHANGED
@@ -22,8 +22,26 @@ module EagerGroup
|
|
22
22
|
self.send :attr_accessor, attr
|
23
23
|
@eager_group_definations ||= {}
|
24
24
|
@eager_group_definations[attr] = Definition.new association, aggregate_function, column_name, scope
|
25
|
+
|
26
|
+
define_method attr, -> (*args) do
|
27
|
+
query_result_cache = instance_variable_get("@#{attr}")
|
28
|
+
if args.blank? && query_result_cache.present?
|
29
|
+
return query_result_cache
|
30
|
+
end
|
31
|
+
preload_eager_group(attr, *args)
|
32
|
+
instance_variable_get("@#{attr}")
|
33
|
+
end
|
34
|
+
|
35
|
+
define_method "#{attr}=" do |val|
|
36
|
+
instance_variable_set("@#{attr}", val)
|
37
|
+
end
|
25
38
|
end
|
26
39
|
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def preload_eager_group(*eager_group_value)
|
43
|
+
EagerGroup::Preloader.new(self.class, [self], [eager_group_value]).run
|
44
|
+
end
|
27
45
|
end
|
28
46
|
|
29
47
|
ActiveRecord::Base.send :include, EagerGroup
|
@@ -11,11 +11,11 @@ module EagerGroup
|
|
11
11
|
primary_key = @klass.primary_key
|
12
12
|
record_ids = @records.map { |record| record.send primary_key }
|
13
13
|
@eager_group_values.each do |eager_group_value|
|
14
|
-
|
15
|
-
if definition
|
14
|
+
defination_key, arguments = eager_group_value.is_a?(Array) ? [eager_group_value.shift, eager_group_value] : [eager_group_value, nil]
|
15
|
+
if definition = @klass.eager_group_definations[defination_key]
|
16
16
|
reflection = @klass.reflect_on_association(definition.association)
|
17
|
-
association_class = reflection.
|
18
|
-
association_class = association_class.instance_exec(&definition.scope) if definition.scope
|
17
|
+
association_class = reflection.klass
|
18
|
+
association_class = association_class.instance_exec(*arguments, &definition.scope) if definition.scope
|
19
19
|
polymophic_as_condition = lambda {|reflection|
|
20
20
|
if reflection.type
|
21
21
|
["#{reflection.name}.#{reflection.type} = ?", @klass.base_class.name]
|
@@ -23,7 +23,7 @@ module EagerGroup
|
|
23
23
|
[]
|
24
24
|
end
|
25
25
|
}
|
26
|
-
|
26
|
+
|
27
27
|
if reflection.through_reflection
|
28
28
|
foreign_key = "#{reflection.through_reflection.name}.#{reflection.through_reflection.foreign_key}"
|
29
29
|
aggregate_hash = association_class.joins(reflection.through_reflection.name)
|
@@ -39,7 +39,7 @@ module EagerGroup
|
|
39
39
|
end
|
40
40
|
@records.each do |record|
|
41
41
|
id = record.send primary_key
|
42
|
-
record.send "#{
|
42
|
+
record.send "#{defination_key}=", aggregate_hash[id] || 0
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
data/lib/eager_group/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eager_group
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.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:
|
11
|
+
date: 2016-09-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -152,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
152
|
version: '0'
|
153
153
|
requirements: []
|
154
154
|
rubyforge_project:
|
155
|
-
rubygems_version: 2.
|
155
|
+
rubygems_version: 2.5.1
|
156
156
|
signing_key:
|
157
157
|
specification_version: 4
|
158
158
|
summary: Fix n+1 aggregate sql functions
|