ar_lazy_preload 2.1.1 → 2.1.3

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: 927bf2110e3c5733916353191cbc1c1b9543dab216b6aef4b120cd6383822796
4
- data.tar.gz: 24e78260d612a9d212c4b53937abff9cf6f68e32aa5728c60b4d686ea49efcf8
3
+ metadata.gz: aafa6eee8d36facbfcb8ebdf81ebb1d2f82655210304afacdd6d4943d1e844a3
4
+ data.tar.gz: d30aa21d55b033a80cf58d2fd4801ba065a68f737e03d9ee2886bf6045ea9c22
5
5
  SHA512:
6
- metadata.gz: 88ca2ecb46671b3ec6f671d06c540a24bbcade260d68309993d2049e76730926b173074b3255cafc51e2d4e8636f5b7a3fbf4b17c1c838b5a4935c0545ffbf81
7
- data.tar.gz: 5266eb7b7901631633a4c8ee554935c29757c60955f2749910ffd2e3688bc66a30edd330e9703c7775020868a0836f8d181cd6ed1f357719098f04a4791a437c
6
+ metadata.gz: 396b3e22f3aae6648b6e7297afa6956a416b37be3d89897f44c53f182120ee26c8f0bd70b70f1dca9761ec09e91b707c499720db46c607019f3984b4d030d49e
7
+ data.tar.gz: 2f58018265351c27246a5b2b606f6ea976bdca74ad045f08a4725f71d3ba7897f2b8fbe2e906994904b602e98e970db406d8d8b987d424d796fa02e9a731f283
data/README.md CHANGED
@@ -13,7 +13,10 @@ Used in production by:
13
13
  - Toptal
14
14
  - _Want to be here? Let me know_ 🙂
15
15
 
16
- You can support my open–source work [here](https://boosty.to/dmitry_tsepelev).
16
+ ## Professional Support
17
+
18
+ Dealing with performance issues in your Rails app beyond N+1 queries?
19
+ I'm available for consulting — [get in touch](https://dmitrytsepelev.dev/consulting).
17
20
 
18
21
  ## Why should I use it?
19
22
 
@@ -64,6 +67,10 @@ posts = User.preload_associations_lazily.flat_map(&:posts)
64
67
  # => SELECT * FROM posts WHERE user_id in (...)
65
68
  ```
66
69
 
70
+ ## Companion gem for non-association N+1s
71
+
72
+ [`N1Loader`](https://github.com/djezzzl/n1_loader) complements ArLazyPreload for batched lazy loading in places where regular ActiveRecord associations are not available (for example, computed values, custom loaders, service/API calls, and GraphQL fields). ArLazyPreload can stay focused on association loading while N1Loader handles the remaining N+1-prone derived data.
73
+
67
74
  ## Gotchas
68
75
 
69
76
  1. Lazy preloading [does not work](https://github.com/DmitryTsepelev/ar_lazy_preload/pull/40/files) for ActiveRecord < 6 when `.includes` is called earlier:
@@ -6,6 +6,7 @@ module ArLazyPreload
6
6
  module AssociationRelation
7
7
  def initialize(*args)
8
8
  super(*args)
9
+ self.preloads_associations_lazily = proxy_association.try(:preloads_associations_lazily?)
9
10
  setup_preloading_context unless ArLazyPreload.config.auto_preload?
10
11
  end
11
12
 
@@ -23,9 +23,14 @@ module ArLazyPreload
23
23
  def skip_preload
24
24
  lazy_preload_context&.records&.delete(self)
25
25
  self.lazy_preload_context = nil
26
+ @skip_preload = true
26
27
  self
27
28
  end
28
29
 
30
+ def skip_preload?
31
+ @skip_preload || false
32
+ end
33
+
29
34
  def try_setup_auto_preload_context
30
35
  ArLazyPreload::Context.register(records: [self]) if ArLazyPreload.config.auto_preload?
31
36
  end
@@ -3,6 +3,19 @@
3
3
  module ArLazyPreload
4
4
  # ActiveRecord::CollectionAssociation patch with a hook for lazy preloading
5
5
  module CollectionAssociation
6
+ attr_writer :preloads_associations_lazily
7
+
8
+ def preloads_associations_lazily?
9
+ @preloads_associations_lazily ||= false
10
+ end
11
+
12
+ # The statement cache loads the target without Relation#load, so the records get their
13
+ # context here.
14
+ def load_target
15
+ need_context = preloads_associations_lazily? && !loaded?
16
+ super.tap { |records| Context.register_auto_preload(records: records) if need_context }
17
+ end
18
+
6
19
  # rubocop:disable Metrics/AbcSize
7
20
  def ids_reader
8
21
  return super if owner.lazy_preload_context.blank?
@@ -3,6 +3,13 @@
3
3
  module ArLazyPreload
4
4
  # ActiveRecord::CollectionProxy patch for getting records for preload
5
5
  module CollectionProxy
6
+ def preload_associations_lazily
7
+ proxy_association.preloads_associations_lazily = true
8
+ reset_scope
9
+ Context.register_auto_preload(records: target) if loaded?
10
+ self
11
+ end
12
+
6
13
  private
7
14
 
8
15
  def ar_lazy_preload_records
@@ -23,7 +23,7 @@ module ArLazyPreload
23
23
  # Enhanced #load method will check if association has not been loaded yet and add a context
24
24
  # for lazy preloading to loaded each record
25
25
  def load
26
- need_context = !loaded?
26
+ need_context = !loaded? || scheduled?
27
27
  result = super
28
28
  if need_context
29
29
  Context.register(
@@ -44,7 +44,14 @@ module ArLazyPreload
44
44
  #
45
45
  # Same effect can be achieved by User.lazy_preload(posts: :comments)
46
46
  def preload_associations_lazily
47
- spawn.tap { |relation| relation.preloads_associations_lazily = true }
47
+ return spawn.tap { |relation| relation.preloads_associations_lazily = true } unless loaded?
48
+
49
+ self.preloads_associations_lazily = true
50
+ return self if scheduled?
51
+
52
+ preload_associations(records)
53
+ Context.register_auto_preload(records: records)
54
+ self
48
55
  end
49
56
 
50
57
  # Specify relationships to be loaded lazily when association is loaded for the first time. For
@@ -23,7 +23,7 @@ module ArLazyPreload
23
23
 
24
24
  # Takes all the associated records for the records, attached to the :parent_context and creates
25
25
  # a preloading context for them
26
- # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
26
+ # rubocop:disable Metrics/MethodLength
27
27
  def perform
28
28
  associated_records = parent_context.records.flat_map do |record|
29
29
  next if record.nil?
@@ -32,7 +32,7 @@ module ArLazyPreload
32
32
  next if reflection.nil?
33
33
 
34
34
  record_association = record.association(association_name)
35
- reflection.collection? ? record_association.target : record_association.reader
35
+ record_association.target
36
36
  end
37
37
 
38
38
  Context.register(
@@ -41,7 +41,7 @@ module ArLazyPreload
41
41
  auto_preload: parent_context.auto_preload?
42
42
  )
43
43
  end
44
- # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
44
+ # rubocop:enable Metrics/MethodLength
45
45
 
46
46
  private
47
47
 
@@ -20,5 +20,18 @@ module ArLazyPreload
20
20
  )
21
21
  end
22
22
  end
23
+
24
+ # Initiates auto preload context for given records, unless they already have one or skip
25
+ # preloading
26
+ def self.register_auto_preload(records:)
27
+ records_without_context, records_with_context =
28
+ records.partition { |record| record.lazy_preload_context.nil? }
29
+ # The whole context is replaced, so its records keep loading their associations together
30
+ lazy_contexts = records_with_context.map(&:lazy_preload_context).uniq.reject(&:auto_preload?)
31
+
32
+ records_to_register =
33
+ records_without_context.reject(&:skip_preload?) + lazy_contexts.flat_map(&:records)
34
+ register(records: records_to_register, auto_preload: true)
35
+ end
23
36
  end
24
37
  end
@@ -63,35 +63,25 @@ module ArLazyPreload
63
63
 
64
64
  # Prepare context for intermediate associations in through chains
65
65
  def prepare_through_association_contexts(association_name, filtered_records)
66
- filtered_records.group_by(&:class).each do |klass, klass_records|
67
- chain = through_association_chain(association_name, klass)
68
- next if chain.empty?
66
+ intermediate_owners(association_name, filtered_records).each do |name, owners|
67
+ next if association_loaded?(name)
69
68
 
70
- chain.each do |intermediate_name|
71
- register_intermediate_context(klass, klass_records, intermediate_name)
72
- end
69
+ register_intermediate_records(owners, name)
73
70
  end
74
71
  end
75
72
 
76
- def register_intermediate_context(klass, klass_records, intermediate_name)
77
- return if association_loaded?(intermediate_name)
78
-
79
- reflection = klass.reflect_on_association(intermediate_name)
80
- return if reflection.nil?
81
-
82
- loaded_association_names.add(intermediate_name)
83
-
84
- intermediate_records = collect_intermediate_records(klass_records, reflection)
85
- return if intermediate_records.empty?
86
-
87
- ArLazyPreload::Context.register(records: intermediate_records, auto_preload: true)
73
+ def intermediate_owners(association_name, filtered_records)
74
+ filtered_records.group_by(&:class).each_with_object({}) do |(klass, klass_records), owners|
75
+ through_association_chain(association_name, klass).each do |name|
76
+ (owners[name] ||= []).concat(klass_records)
77
+ end
78
+ end
88
79
  end
89
80
 
90
- def collect_intermediate_records(klass_records, reflection)
91
- klass_records.flat_map do |record|
92
- record_association = record.association(reflection.name)
93
- reflection.collection? ? record_association.target : record_association.reader
94
- end.compact
81
+ def register_intermediate_records(owners, name)
82
+ associations = owners.map { |owner| owner.association(name) }.select(&:loaded?)
83
+ targets = associations.flat_map(&:target).compact.reject(&:lazy_preload_context)
84
+ ArLazyPreload::Context.register(records: targets, auto_preload: true)
95
85
  end
96
86
 
97
87
  # Extract chain of intermediate association names for through associations
@@ -103,7 +93,7 @@ module ArLazyPreload
103
93
  current_reflection = reflection
104
94
 
105
95
  while current_reflection&.options&.key?(:through)
106
- through_name = current_reflection.options[:through]
96
+ through_name = current_reflection.options[:through].to_sym
107
97
  chain.unshift(through_name)
108
98
  current_reflection = klass.reflect_on_association(through_name)
109
99
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ArLazyPreload
4
- VERSION = "2.1.1"
4
+ VERSION = "2.1.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_lazy_preload
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev