eager_group 0.7.2 → 0.8.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/lib/eager_group.rb +1 -1
- data/lib/eager_group/preloader.rb +13 -2
- data/lib/eager_group/version.rb +1 -1
- metadata +2 -4
- data/lib/eager_group/active_record_base.rb +0 -8
- data/lib/eager_group/active_record_relation.rb +0 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 516b00c6397d1a6b397b165687091079118a48ed39fc0afce4542d3cf4f7f481
|
4
|
+
data.tar.gz: 9bf3083695c819003574f3971d2a592443320af92dc98c1ff36b302c87718889
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 389265b557184c2d3f3c0207340c8bd65086a38fb8e0b2b168a4c14ed37f82b5bcb0ee89ab7a48da528bfbcf2174c1c89dcc0fd917eade1de9c184cad3238e9b
|
7
|
+
data.tar.gz: 6e5e9bd946a9d333bda2087578ac47b98d57ab0cb8347adc9577f582124e1ca9a571fdddeaa28da48644aca0f9d0546f1ee323149742b2c0868d5d6c324354da
|
data/CHANGELOG.md
CHANGED
data/lib/eager_group.rb
CHANGED
@@ -14,6 +14,7 @@ module EagerGroup
|
|
14
14
|
@eager_group_values.each do |eager_group_value|
|
15
15
|
definition_key, arguments =
|
16
16
|
eager_group_value.is_a?(Array) ? [eager_group_value.shift, eager_group_value] : [eager_group_value, nil]
|
17
|
+
|
17
18
|
if definition_key.is_a?(Hash)
|
18
19
|
association_name, definition_key = *definition_key.first
|
19
20
|
@records = @records.flat_map { |record| record.send(association_name) }
|
@@ -22,13 +23,23 @@ module EagerGroup
|
|
22
23
|
@klass = @records.first.class
|
23
24
|
end
|
24
25
|
record_ids = @records.map { |record| record.send(primary_key) }
|
25
|
-
|
26
|
+
unless definition = @klass.eager_group_definitions[definition_key]
|
27
|
+
next
|
28
|
+
end
|
26
29
|
|
27
30
|
reflection = @klass.reflect_on_association(definition.association)
|
28
31
|
association_class = reflection.klass
|
29
32
|
association_class = association_class.instance_exec(*arguments, &definition.scope) if definition.scope
|
30
33
|
|
31
|
-
if reflection.
|
34
|
+
if reflection.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection)
|
35
|
+
foreign_key = "#{reflection.join_table}.#{reflection.foreign_key}"
|
36
|
+
aggregate_hash =
|
37
|
+
@klass.joins(reflection.name).where(foreign_key => record_ids).where(
|
38
|
+
polymophic_as_condition(reflection)
|
39
|
+
)
|
40
|
+
.group(foreign_key)
|
41
|
+
.send(definition.aggregation_function, definition.column_name)
|
42
|
+
elsif reflection.through_reflection
|
32
43
|
foreign_key = "#{reflection.through_reflection.name}.#{reflection.through_reflection.foreign_key}"
|
33
44
|
aggregate_hash =
|
34
45
|
association_class.joins(reflection.through_reflection.name).where(foreign_key => record_ids).where(
|
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.8.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: 2019-10-
|
11
|
+
date: 2019-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -142,8 +142,6 @@ files:
|
|
142
142
|
- eager_group.gemspec
|
143
143
|
- lib/active_record/with_eager_group.rb
|
144
144
|
- lib/eager_group.rb
|
145
|
-
- lib/eager_group/active_record_base.rb
|
146
|
-
- lib/eager_group/active_record_relation.rb
|
147
145
|
- lib/eager_group/definition.rb
|
148
146
|
- lib/eager_group/preloader.rb
|
149
147
|
- lib/eager_group/version.rb
|
@@ -1,33 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class ActiveRecord::Relation
|
4
|
-
# Post.all.eager_group(:approved_comments_count, :comments_average_rating)
|
5
|
-
|
6
|
-
def exec_queries_with_eager_group
|
7
|
-
records = exec_queries_without_eager_group
|
8
|
-
EagerGroup::Preloader.new(klass, records, eager_group_values).run if eager_group_values.present?
|
9
|
-
records
|
10
|
-
end
|
11
|
-
|
12
|
-
alias_method_chain :exec_queries, :eager_group
|
13
|
-
|
14
|
-
def eager_group(*args)
|
15
|
-
check_if_method_has_arguments!('eager_group', args)
|
16
|
-
spawn.eager_group!(*args)
|
17
|
-
end
|
18
|
-
|
19
|
-
def eager_group!(*args)
|
20
|
-
self.eager_group_values += args
|
21
|
-
self
|
22
|
-
end
|
23
|
-
|
24
|
-
def eager_group_values
|
25
|
-
@values[:eager_group] || []
|
26
|
-
end
|
27
|
-
|
28
|
-
def eager_group_values=(values)
|
29
|
-
raise ImmutableRelation if @loaded
|
30
|
-
|
31
|
-
@values[:eager_group] = values
|
32
|
-
end
|
33
|
-
end
|