eager_group 0.7.1 → 0.7.2
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/benchmark.rb +10 -9
- data/lib/eager_group.rb +8 -7
- data/lib/eager_group/preloader.rb +13 -11
- data/lib/eager_group/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27ca5cd0d0757f48f272d4349a6dcad1f54992946d960f364bec0086a7646ccf
|
4
|
+
data.tar.gz: 27d7def8639caca795400ba7912789ba7098315578ec954dfb2610fe5a6262af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 464cafc22781b01be11027b832219b1dc551f0b3c300f3b377296690c0b9e7d4e98dde5f87c7e9ad8f4f88792ed4b908e00347ab95042a66412a3ef1d31731b6
|
7
|
+
data.tar.gz: 81aaa99f6d856239dba7e1a06fe4d8e2fb2781e5cb98557d142eac440a658cb4ea2a90c354ea82b33b91dcd2f2fa9452a64cf6f2e0c749282937d682fee62618
|
data/CHANGELOG.md
CHANGED
data/benchmark.rb
CHANGED
@@ -30,11 +30,11 @@ class Comment < ActiveRecord::Base
|
|
30
30
|
end
|
31
31
|
|
32
32
|
# create database eager_group_benchmark;
|
33
|
-
ActiveRecord::Base.establish_connection(
|
33
|
+
ActiveRecord::Base.establish_connection(
|
34
|
+
adapter: 'mysql2', database: 'eager_group_benchmark', server: '/tmp/mysql.socket', username: 'root'
|
35
|
+
)
|
34
36
|
|
35
|
-
ActiveRecord::Base.connection.tables.each
|
36
|
-
ActiveRecord::Base.connection.drop_table(table)
|
37
|
-
end
|
37
|
+
ActiveRecord::Base.connection.tables.each { |table| ActiveRecord::Base.connection.drop_table(table) }
|
38
38
|
|
39
39
|
ActiveRecord::Schema.define do
|
40
40
|
self.verbose = false
|
@@ -55,18 +55,19 @@ ActiveRecord::Schema.define do
|
|
55
55
|
end
|
56
56
|
|
57
57
|
posts_size = 100
|
58
|
-
comments_size =
|
58
|
+
comments_size = 1_000
|
59
59
|
|
60
60
|
posts = []
|
61
|
-
posts_size.times
|
62
|
-
posts << Post.new(title: "Title #{i}", body: "Body #{i}")
|
63
|
-
end
|
61
|
+
posts_size.times { |i| posts << Post.new(title: "Title #{i}", body: "Body #{i}") }
|
64
62
|
Post.import posts
|
65
63
|
post_ids = Post.all.pluck(:id)
|
66
64
|
|
67
65
|
comments = []
|
68
66
|
comments_size.times do |i|
|
69
|
-
comments <<
|
67
|
+
comments <<
|
68
|
+
Comment.new(
|
69
|
+
body: "Comment #{i}", post_id: post_ids[i % 100], status: %w[approved deleted][i % 2], rating: i % 5 + 1
|
70
|
+
)
|
70
71
|
end
|
71
72
|
Comment.import comments
|
72
73
|
|
data/lib/eager_group.rb
CHANGED
@@ -20,15 +20,16 @@ module EagerGroup
|
|
20
20
|
# end
|
21
21
|
def define_eager_group(attr, association, aggregate_function, column_name, scope = nil)
|
22
22
|
send :attr_accessor, attr
|
23
|
-
|
23
|
+
eager_group_definitions[attr] = Definition.new(association, aggregate_function, column_name, scope)
|
24
24
|
|
25
|
-
define_method attr,
|
26
|
-
|
27
|
-
|
25
|
+
define_method attr,
|
26
|
+
lambda { |*args|
|
27
|
+
query_result_cache = instance_variable_get("@#{attr}")
|
28
|
+
return query_result_cache if args.blank? && query_result_cache.present?
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
preload_eager_group(attr, *args)
|
31
|
+
instance_variable_get("@#{attr}")
|
32
|
+
}
|
32
33
|
|
33
34
|
define_method "#{attr}=" do |val|
|
34
35
|
instance_variable_set("@#{attr}", val)
|
@@ -12,7 +12,8 @@ module EagerGroup
|
|
12
12
|
def run
|
13
13
|
primary_key = @klass.primary_key
|
14
14
|
@eager_group_values.each do |eager_group_value|
|
15
|
-
definition_key, arguments =
|
15
|
+
definition_key, arguments =
|
16
|
+
eager_group_value.is_a?(Array) ? [eager_group_value.shift, eager_group_value] : [eager_group_value, nil]
|
16
17
|
if definition_key.is_a?(Hash)
|
17
18
|
association_name, definition_key = *definition_key.first
|
18
19
|
@records = @records.flat_map { |record| record.send(association_name) }
|
@@ -29,19 +30,20 @@ module EagerGroup
|
|
29
30
|
|
30
31
|
if reflection.through_reflection
|
31
32
|
foreign_key = "#{reflection.through_reflection.name}.#{reflection.through_reflection.foreign_key}"
|
32
|
-
aggregate_hash =
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
33
|
+
aggregate_hash =
|
34
|
+
association_class.joins(reflection.through_reflection.name).where(foreign_key => record_ids).where(
|
35
|
+
polymophic_as_condition(reflection.through_reflection)
|
36
|
+
)
|
37
|
+
.group(foreign_key)
|
38
|
+
.send(definition.aggregation_function, definition.column_name)
|
37
39
|
else
|
38
|
-
aggregate_hash =
|
39
|
-
|
40
|
-
|
41
|
-
|
40
|
+
aggregate_hash =
|
41
|
+
association_class.where(reflection.foreign_key => record_ids).where(polymophic_as_condition(reflection))
|
42
|
+
.group(reflection.foreign_key)
|
43
|
+
.send(definition.aggregation_function, definition.column_name)
|
42
44
|
end
|
43
45
|
if definition.need_load_object
|
44
|
-
aggregate_objects =
|
46
|
+
aggregate_objects = reflection.klass.find(aggregate_hash.values).each_with_object({}) { |o, h| h[o.id] = o }
|
45
47
|
aggregate_hash.keys.each { |key| aggregate_hash[key] = aggregate_objects[aggregate_hash[key]] }
|
46
48
|
end
|
47
49
|
@records.each do |record|
|
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.7.
|
4
|
+
version: 0.7.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|