eager_group 0.9.1 → 0.10.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: 119ff48ee90c0324a8e7f55e521a3606485ad1728ec27eaefea3df1f7c153173
4
- data.tar.gz: 7415661b2a4ced20c7673c8d90bfb977fbef22c203062f0bb60a8be5fdd1970a
3
+ metadata.gz: 3717871f8324a6ec0a0886745b12c17f7443398a8658bcb60ce008fa1e924df1
4
+ data.tar.gz: 0eccab385f9d6ec341ace42258672f663b03598ee164ef22b1b25fb7bf17779a
5
5
  SHA512:
6
- metadata.gz: f3b85835d577315809f1a699c7a717d17b1f61c14ae0e0371312c12615a5088c45a266028fc3f091801639298e9dd6a1ca1e8d6beb37889a6f5e63aacbdc8fe6
7
- data.tar.gz: 2475f41755b2b0161bcc4fe01fd04a5cb1b9f69728edd0e165e8596773306e421426efc191aa34eba812912c1e6b8cc608386a3fc29ca490a068c15b2335e337
6
+ metadata.gz: c1fb52a045e4f2005a78ef698ff915195e705a9729e1fb6a25275d2516c48e7f016d3249946d3a337b07b70a822f75c0ffe1f2acbb130c78a2d13df03107449b
7
+ data.tar.gz: 0a7393ea354afaaf1c1002c7641c3a4bd94e26b2407eea9b6cb701da243b67733c34d226711650964251aaa2f5d117db0d0e418e5702ebb4b75f59323c3b6c49
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Next Release
2
2
 
3
+ ## 0.10.0 (12/28/2022)
4
+
5
+ * Add STI support, use `class_attribute` to prevent subclasses affect each other
6
+
3
7
  ## 0.9.1 (12/15/2022)
4
8
 
5
9
  * Fix eager group fetch multi definitions
@@ -13,6 +13,7 @@ module ActiveRecord
13
13
  # which would cause `[:eager_group_definition, scope_arg1, scope_arg2]` not able to preload together with other `eager_group_definitions`.
14
14
  # e.g. `Post.eager_group(:approved_comments_count, [:comments_average_rating_by_author, students[0], true])`
15
15
  check_argument_not_blank!(args)
16
+ check_argument_valid!(args)
16
17
 
17
18
  spawn.eager_group!(*args)
18
19
  end
@@ -38,5 +39,31 @@ module ActiveRecord
38
39
  raise ArgumentError, "The method .eager_group() must contain arguments." if args.blank?
39
40
  args.compact_blank!
40
41
  end
42
+
43
+ def check_argument_valid!(args)
44
+ args.each do |eager_group_value|
45
+ check_eager_group_definitions_exists!(klass, eager_group_value)
46
+ end
47
+ end
48
+
49
+ def check_eager_group_definitions_exists!(klass, eager_group_value)
50
+ case eager_group_value
51
+ when Symbol, String
52
+ raise ArgumentError, "Unknown eager group definition :#{eager_group_value}" unless klass.eager_group_definitions.has_key?(eager_group_value)
53
+ when Array
54
+ definition_name = eager_group_value.first
55
+ raise ArgumentError, "Unknown eager group definition :#{definition_name}" unless klass.eager_group_definitions.has_key?(definition_name)
56
+ when Hash
57
+ eager_group_value.each do |association_name, association_eager_group_values|
58
+ association_klass = klass.reflect_on_association(association_name).klass
59
+
60
+ Array.wrap(association_eager_group_values).each do |association_eager_group_value|
61
+ check_eager_group_definitions_exists!(association_klass, association_eager_group_value)
62
+ end
63
+ end
64
+ else
65
+ raise ArgumentError, "Unknown eager_group argument :#{eager_group_value.inspect}"
66
+ end
67
+ end
41
68
  end
42
69
  end
@@ -22,10 +22,13 @@ module EagerGroup
22
22
 
23
23
  if definition_key.is_a?(Hash)
24
24
  association_name, definition_key = *definition_key.first
25
+ next if @records.empty?
26
+ @klass = @records.first.class.reflect_on_association(association_name).klass
27
+
25
28
  @records = @records.flat_map { |record| record.send(association_name) }
26
29
  next if @records.empty?
27
30
 
28
- @klass = @records.first.class
31
+
29
32
  self.class.new(@klass, @records, Array.wrap(definition_key)).run
30
33
  end
31
34
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EagerGroup
4
- VERSION = '0.9.1'
4
+ VERSION = '0.10.0'
5
5
  end
data/lib/eager_group.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support/core_ext/module/attribute_accessors'
4
+ require 'active_support/core_ext/class/attribute'
5
+ require 'active_support/core_ext/hash'
4
6
  require 'eager_group/version'
5
7
 
6
8
  module EagerGroup
@@ -9,30 +11,39 @@ module EagerGroup
9
11
 
10
12
  def self.included(base)
11
13
  base.extend ClassMethods
14
+ base.class_eval do
15
+ class_attribute :eager_group_definitions, instance_writer: false, default: {}.with_indifferent_access
16
+ end
12
17
  end
13
18
 
14
19
  module ClassMethods
15
- mattr_accessor :eager_group_definitions, default: {}
20
+ #mattr_accessor :eager_group_definitions, default: {}
21
+
22
+ def add_eager_group_definition(ar, definition_name, definition)
23
+ ar.eager_group_definitions = self.eager_group_definitions.except(definition_name).merge!(definition_name => definition)
24
+ end
16
25
 
17
26
  # class Post
18
27
  # define_eager_group :comments_avergage_rating, :comments, :average, :rating
19
28
  # define_eager_group :approved_comments_count, :comments, :count, :*, -> { approved }
20
29
  # end
21
30
  def define_eager_group(attr, association, aggregate_function, column_name, scope = nil)
22
- send :attr_accessor, attr
23
- eager_group_definitions[attr] = Definition.new(association, aggregate_function, column_name, scope)
31
+ add_eager_group_definition(self, attr, Definition.new(association, aggregate_function, column_name, scope))
32
+ define_definition_accessor(attr)
33
+ end
24
34
 
25
- define_method attr,
35
+ def define_definition_accessor(definition_name)
36
+ define_method definition_name,
26
37
  lambda { |*args|
27
- query_result_cache = instance_variable_get("@#{attr}")
38
+ query_result_cache = instance_variable_get("@#{definition_name}")
28
39
  return query_result_cache if args.blank? && query_result_cache.present?
29
40
 
30
- preload_eager_group(attr, *args)
31
- instance_variable_get("@#{attr}")
41
+ preload_eager_group(definition_name, *args)
42
+ instance_variable_get("@#{definition_name}")
32
43
  }
33
44
 
34
- define_method "#{attr}=" do |val|
35
- instance_variable_set("@#{attr}", val)
45
+ define_method "#{definition_name}=" do |val|
46
+ instance_variable_set("@#{definition_name}", val)
36
47
  end
37
48
  end
38
49
  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.9.1
4
+ version: 0.10.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: 2022-12-15 00:00:00.000000000 Z
11
+ date: 2022-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -185,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
185
185
  - !ruby/object:Gem::Version
186
186
  version: '0'
187
187
  requirements: []
188
- rubygems_version: 3.3.26
188
+ rubygems_version: 3.4.1
189
189
  signing_key:
190
190
  specification_version: 4
191
191
  summary: Fix n+1 aggregate sql functions