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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 559e8eeb3768f38a63b7ec19a0201dfc583073e37cc145ffabd19886bfe4ba59
4
- data.tar.gz: 72fa7fd007ac0f9de02490cde602b25ad6bbb3ef48b8d61b1b13976721d89fcf
3
+ metadata.gz: 27ca5cd0d0757f48f272d4349a6dcad1f54992946d960f364bec0086a7646ccf
4
+ data.tar.gz: 27d7def8639caca795400ba7912789ba7098315578ec954dfb2610fe5a6262af
5
5
  SHA512:
6
- metadata.gz: 39e828e5c8e1315d2a3083907c172373c7f6026d3397a7f0eecbf6722408e87af71c3c22226e271d7a7369806227184513a68df24cf865d2dcf6ea8e23f63652
7
- data.tar.gz: 559307e3dac7da634ed9a3e28d93a7dbf0f2b16eb422887f308bfd01f1241806485ce34b62a3d6c66fe9a601f63128fe14a2ce47f729c92508516bd60e637e3d
6
+ metadata.gz: 464cafc22781b01be11027b832219b1dc551f0b3c300f3b377296690c0b9e7d4e98dde5f87c7e9ad8f4f88792ed4b908e00347ab95042a66412a3ef1d31731b6
7
+ data.tar.gz: 81aaa99f6d856239dba7e1a06fe4d8e2fb2781e5cb98557d142eac440a658cb4ea2a90c354ea82b33b91dcd2f2fa9452a64cf6f2e0c749282937d682fee62618
@@ -1,5 +1,9 @@
1
1
  # Next Release
2
2
 
3
+ ## 0.7.2 (10/10/2019)
4
+
5
+ * Simplify `association_klass` for `first_object` and `last_object`
6
+
3
7
  ## 0.7.1 (08/23/2019)
4
8
 
5
9
  * Set `eager_group_definitions` by `mattr_accessor`
@@ -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(adapter: 'mysql2', database: 'eager_group_benchmark', server: '/tmp/mysql.socket', username: 'root')
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 do |table|
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 = 1000
58
+ comments_size = 1_000
59
59
 
60
60
  posts = []
61
- posts_size.times do |i|
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 << Comment.new(body: "Comment #{i}", post_id: post_ids[i % 100], status: %w[approved deleted][i % 2], rating: i % 5 + 1)
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
 
@@ -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
- self.eager_group_definitions[attr] = Definition.new(association, aggregate_function, column_name, scope)
23
+ eager_group_definitions[attr] = Definition.new(association, aggregate_function, column_name, scope)
24
24
 
25
- define_method attr, lambda { |*args|
26
- query_result_cache = instance_variable_get("@#{attr}")
27
- return query_result_cache if args.blank? && query_result_cache.present?
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
- preload_eager_group(attr, *args)
30
- instance_variable_get("@#{attr}")
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 = eager_group_value.is_a?(Array) ? [eager_group_value.shift, eager_group_value] : [eager_group_value, nil]
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 = association_class.joins(reflection.through_reflection.name)
33
- .where(foreign_key => record_ids)
34
- .where(polymophic_as_condition(reflection.through_reflection))
35
- .group(foreign_key)
36
- .send(definition.aggregation_function, definition.column_name)
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 = association_class.where(reflection.foreign_key => record_ids)
39
- .where(polymophic_as_condition(reflection))
40
- .group(reflection.foreign_key)
41
- .send(definition.aggregation_function, definition.column_name)
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 = association_class.find(aggregate_hash.values).each_with_object({}) { |o, h| h[o.id] = o }
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|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EagerGroup
4
- VERSION = '0.7.1'
4
+ VERSION = '0.7.2'
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.1
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-08-22 00:00:00.000000000 Z
11
+ date: 2019-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord