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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 27ca5cd0d0757f48f272d4349a6dcad1f54992946d960f364bec0086a7646ccf
4
- data.tar.gz: 27d7def8639caca795400ba7912789ba7098315578ec954dfb2610fe5a6262af
3
+ metadata.gz: 516b00c6397d1a6b397b165687091079118a48ed39fc0afce4542d3cf4f7f481
4
+ data.tar.gz: 9bf3083695c819003574f3971d2a592443320af92dc98c1ff36b302c87718889
5
5
  SHA512:
6
- metadata.gz: 464cafc22781b01be11027b832219b1dc551f0b3c300f3b377296690c0b9e7d4e98dde5f87c7e9ad8f4f88792ed4b908e00347ab95042a66412a3ef1d31731b6
7
- data.tar.gz: 81aaa99f6d856239dba7e1a06fe4d8e2fb2781e5cb98557d142eac440a658cb4ea2a90c354ea82b33b91dcd2f2fa9452a64cf6f2e0c749282937d682fee62618
6
+ metadata.gz: 389265b557184c2d3f3c0207340c8bd65086a38fb8e0b2b168a4c14ed37f82b5bcb0ee89ab7a48da528bfbcf2174c1c89dcc0fd917eade1de9c184cad3238e9b
7
+ data.tar.gz: 6e5e9bd946a9d333bda2087578ac47b98d57ab0cb8347adc9577f582124e1ca9a571fdddeaa28da48644aca0f9d0546f1ee323149742b2c0868d5d6c324354da
@@ -1,5 +1,9 @@
1
1
  # Next Release
2
2
 
3
+ ## 0.8.0 (10/21/2019)
4
+
5
+ * Support `has_and_belongs_to_many`
6
+
3
7
  ## 0.7.2 (10/10/2019)
4
8
 
5
9
  * Simplify `association_klass` for `first_object` and `last_object`
@@ -52,4 +52,4 @@ ActiveRecord::Base.class_eval do
52
52
  end
53
53
  end
54
54
  require 'active_record/with_eager_group'
55
- ActiveRecord::Relation.send :prepend, ActiveRecord::WithEagerGroup
55
+ ActiveRecord::Relation.prepend ActiveRecord::WithEagerGroup
@@ -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
- next unless definition = @klass.eager_group_definitions[definition_key]
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.through_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(
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EagerGroup
4
- VERSION = '0.7.2'
4
+ VERSION = '0.8.0'
5
5
  end
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.2
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-10 00:00:00.000000000 Z
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,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class ActiveRecord::Base
4
- class << self
5
- # Post.eager_group(:approved_comments_count, :comments_average_rating)
6
- delegate :eager_group, to: :all
7
- end
8
- end
@@ -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