ar_lazy_preload 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 18eb45f9e1c299f719a7bdde76d9da3c9de0175919ad71556c4dfee750405d35
4
- data.tar.gz: cec719881cff10f3687c8a22461d0b27acf9ab064104cbb5fb380eb6bd836701
3
+ metadata.gz: 7fa14f78a46e62ec3f8cb5053ea2887bbb6a5b12b77bb714efed694c21c36432
4
+ data.tar.gz: f82efd9fef819d6c4c909e366161dfc9e39d679554ae0073af3519aaad0814e6
5
5
  SHA512:
6
- metadata.gz: 71d13fa6015289abc893816b695e920fb59c42d96691d5cd86c3ce41c2c5e4bb34e5c6ea343094aabd39136e4e01ef1e88cd1f5e50bf97968f074844b61b3d87
7
- data.tar.gz: 5e4e2f0f95b42796d7f165afb7009db76fbbcfa083e2552c2b7c90e7bc9442445d533cdfd498dc8a39b4734e8c0e50641ea8d77f48a429a85568fb0421e72c3c
6
+ metadata.gz: 657987c4f3d36a53247aede5ffcd42cf362c732f286c87f7fada29e1ddcf775a49d39724a733c1281b0329c00f2bbab9ea74146e03b0a518756e91acaaa65820
7
+ data.tar.gz: de59065ed0ac071d39223f31f7028142d25d2fb7ac6e5af0149aff2211fdcf92ecf36aa5ba9a463883031e60d85b010df58dd55f7a9a3df228039f5de8fa0891
@@ -7,52 +7,42 @@ module ArLazyPreload
7
7
  # belonging to the same context and association name it will create and attach a new context to
8
8
  # the associated records based on the parent association tree.
9
9
  class AssociatedContextBuilder
10
+ # Initiates lazy preload context the records loaded lazily
11
+ def self.prepare(*args)
12
+ new(*args).perform
13
+ end
14
+
10
15
  attr_reader :parent_context, :association_name
11
16
 
17
+ # :parent_context - root context
18
+ # :association_name - lazily preloaded association name
12
19
  def initialize(parent_context:, association_name:)
13
20
  @parent_context = parent_context
14
21
  @association_name = association_name
15
22
  end
16
23
 
17
- delegate :records, :association_tree, :model, to: :parent_context
18
-
19
24
  # Takes all the associated records for the records, attached to the :parent_context and creates
20
25
  # a preloading context for them
21
26
  def perform
22
- return if child_association_tree.blank? || associated_records.blank?
27
+ records_by_class = parent_context.records.group_by(&:class)
23
28
 
24
- Context.new(
25
- model: reflection.klass,
26
- records: associated_records,
27
- association_tree: child_association_tree
28
- )
29
+ associated_records = records_by_class.map do |klass, klass_records|
30
+ associated_records_for(klass, klass_records)
31
+ end.flatten
32
+
33
+ Context.register(records: associated_records, association_tree: child_association_tree)
29
34
  end
30
35
 
31
36
  private
32
37
 
33
38
  def child_association_tree
34
- @child_association_tree ||= association_tree_builder.subtree_for(association_name)
35
- end
36
-
37
- def association_tree_builder
38
- @association_tree_builder ||= AssociationTreeBuilder.new(association_tree)
39
- end
40
-
41
- def associated_records
42
- @associated_records ||=
43
- if reflection.collection?
44
- record_associations.map(&:target).flatten
45
- else
46
- record_associations
47
- end
48
- end
49
-
50
- def reflection
51
- @reflection = model.reflect_on_association(association_name)
39
+ AssociationTreeBuilder.new(parent_context.association_tree).subtree_for(association_name)
52
40
  end
53
41
 
54
- def record_associations
55
- @record_associations ||= records.map { |record| record.send(association_name) }
42
+ def associated_records_for(klass, records)
43
+ record_associations = records.map { |record| record.send(association_name) }
44
+ reflection = klass.reflect_on_association(association_name)
45
+ reflection.collection? ? record_associations.map(&:target).flatten : record_associations
56
46
  end
57
47
  end
58
48
  end
@@ -9,13 +9,17 @@ module ArLazyPreload
9
9
  # Calling #preload_association method will cause loading of ALL associated objects for EACH
10
10
  # ecord when requested association is found in the association tree.
11
11
  class Context
12
- attr_reader :model, :records, :association_tree
12
+ # Initiates lazy preload context for given records
13
+ def self.register(records:, association_tree:)
14
+ return if records.empty? || association_tree.empty?
15
+ ArLazyPreload::Context.new(records: records, association_tree: association_tree)
16
+ end
17
+
18
+ attr_reader :records, :association_tree
13
19
 
14
- # :model - ActiveRecord class which records belong to
15
20
  # :records - array of ActiveRecord instances
16
21
  # :association_tree - list of symbols or hashes representing a tree of preloadable associations
17
- def initialize(model:, records:, association_tree:)
18
- @model = model
22
+ def initialize(records:, association_tree:)
19
23
  @records = records.compact
20
24
  @association_tree = association_tree
21
25
 
@@ -26,13 +30,8 @@ module ArLazyPreload
26
30
  # objects in the context it if needed.
27
31
  def try_preload_lazily(association_name)
28
32
  return unless association_needs_preload?(association_name)
29
-
30
33
  preloader.preload(records, association_name)
31
-
32
- AssociatedContextBuilder.new(
33
- parent_context: self,
34
- association_name: association_name
35
- ).perform
34
+ AssociatedContextBuilder.prepare(parent_context: self, association_name: association_name)
36
35
  end
37
36
 
38
37
  private
@@ -4,14 +4,8 @@ module ArLazyPreload
4
4
  # ActiveRecord::Association patch with a hook for lazy preloading
5
5
  module Association
6
6
  def load_target
7
- owner.try_preload_lazily(association_name)
7
+ owner.try_preload_lazily(reflection.name)
8
8
  super
9
9
  end
10
-
11
- private
12
-
13
- def association_name
14
- @association_name ||= reflection.name
15
- end
16
10
  end
17
11
  end
@@ -8,7 +8,7 @@ module ArLazyPreload
8
8
  super(*args)
9
9
 
10
10
  context = owner.lazy_preload_context
11
- return if context.blank?
11
+ return if context.nil?
12
12
 
13
13
  association_tree_builder = AssociationTreeBuilder.new(context.association_tree)
14
14
  subtree = association_tree_builder.subtree_for(reflection.name)
@@ -9,9 +9,6 @@ module ArLazyPreload
9
9
 
10
10
  attr_accessor :lazy_preload_context
11
11
 
12
- # When context has been set, this method would cause preloading association with a given name
13
- def try_preload_lazily(association_name)
14
- lazy_preload_context.try_preload_lazily(association_name) if lazy_preload_context.present?
15
- end
12
+ delegate :try_preload_lazily, to: :lazy_preload_context, allow_nil: true
16
13
  end
17
14
  end
@@ -9,9 +9,9 @@ module ArLazyPreload
9
9
  # for lazy preloading to loaded each record
10
10
  def load
11
11
  need_context = !loaded?
12
- old_load_result = super
13
- setup_lazy_preload_context if need_context
14
- old_load_result
12
+ result = super
13
+ Context.register(records: @records, association_tree: lazy_preload_values) if need_context
14
+ result
15
15
  end
16
16
 
17
17
  # Specify relationships to be loaded lazily when association is loaded for the first time. For
@@ -49,15 +49,5 @@ module ArLazyPreload
49
49
  private
50
50
 
51
51
  attr_writer :lazy_preload_values
52
-
53
- def setup_lazy_preload_context
54
- return if lazy_preload_values.blank? || @records.blank?
55
-
56
- ArLazyPreload::Context.new(
57
- model: model,
58
- records: @records,
59
- association_tree: lazy_preload_values
60
- )
61
- end
62
52
  end
63
53
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ArLazyPreload
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_lazy_preload
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-31 00:00:00.000000000 Z
11
+ date: 2018-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec