goldiloader 4.0.0 → 4.1.2

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: 2b1702016efdeb47b12f583ff299bc46125714acc053b7c8594a9cf9a4fcba07
4
- data.tar.gz: 6485555c4d71199683907813d9563fdecce3c8bada819d7ae1180f48ad5afadb
3
+ metadata.gz: 14cd0ebc332f63f4f019029af0c75254207ad6b3957d3204b458200ffe89aa61
4
+ data.tar.gz: 151fe68d70ebc800bd471204106d49e7208678b6f0d5e1495a08302859f8574d
5
5
  SHA512:
6
- metadata.gz: 949fc36b600662dde9597b87c26430949bbdc12dff2d2eb9845405dcb273c2c9621edbb8d850f95dd602c892687dffc95849243bbd94c4e3473c31932f48464a
7
- data.tar.gz: 5d645a33f703ad458096af205535efcd5cebb2ab802ae0af8203cc6236f388a73d79ec0c08681c447dbbe4b057a29a0bd373c001ce277dd34285447614f138a8
6
+ metadata.gz: 4b387491503a50e6108c0fb3df8419eaea1032a302908afbaa0302582e37819250eecf569fb53c7eb29f4d204fe2d799b4fd0e5f71f9afc026b104370e5132b6
7
+ data.tar.gz: 62554443b9814f9487f63a39e2cd6c0da8bd85fc0985fc87f078e21b812f91d453676c72db9794d8d40fb5d04edd3e7220cad9aaf2c7bb94c374604b6b8de815
@@ -68,23 +68,26 @@ module Goldiloader
68
68
  ActiveRecord::Relation::Merger.prepend(::Goldiloader::MergerPatch)
69
69
 
70
70
  module AssociationReflectionPatch
71
- def eager_loadable?
72
- return @eager_loadable if instance_variable_defined?(:@eager_loadable)
73
-
74
- @eager_loadable = if scope.nil?
75
- # Associations without any scoping options are eager loadable
76
- true
77
- elsif scope.arity > 0
78
- # The scope will be evaluated for every model instance so it can't
79
- # be eager loaded
80
- false
81
- else
82
- scope_info = Goldiloader::ScopeInfo.new(scope_for(klass.unscoped))
83
- scope_info.auto_include? &&
84
- !scope_info.limit? &&
85
- !scope_info.offset? &&
86
- (!has_one? || !scope_info.order?)
87
- end
71
+ # Note we need to pass the association's target class as an argument since it won't be known
72
+ # outside the context of an association instance for polymorphic associations.
73
+ def eager_loadable?(target_klass)
74
+ @eager_loadable_cache ||= Hash.new do |cache, target_klass_key|
75
+ cache[target_klass_key] = if scope.nil?
76
+ # Associations without any scoping options are eager loadable
77
+ true
78
+ elsif scope.arity > 0
79
+ # The scope will be evaluated for every model instance so it can't
80
+ # be eager loaded
81
+ false
82
+ else
83
+ scope_info = Goldiloader::ScopeInfo.new(scope_for(target_klass_key.unscoped))
84
+ scope_info.auto_include? &&
85
+ !scope_info.limit? &&
86
+ !scope_info.offset? &&
87
+ (!has_one? || !scope_info.order?)
88
+ end
89
+ end
90
+ @eager_loadable_cache[target_klass]
88
91
  end
89
92
  end
90
93
  ActiveRecord::Reflection::AssociationReflection.include(::Goldiloader::AssociationReflectionPatch)
@@ -111,11 +114,13 @@ module Goldiloader
111
114
  private
112
115
 
113
116
  def eager_loadable?
114
- reflection.eager_loadable? &&
117
+ klass && reflection.eager_loadable?(klass) &&
115
118
  (::Goldiloader::Compatibility.destroyed_model_associations_eager_loadable? || !owner.destroyed?)
116
119
  end
117
120
 
118
121
  def load_with_auto_include
122
+ return yield unless Goldiloader.enabled?
123
+
119
124
  if loaded? && !stale_target?
120
125
  target
121
126
  elsif !auto_include?
@@ -15,7 +15,7 @@ module Goldiloader
15
15
  private
16
16
 
17
17
  def eager_load(models, association_name)
18
- if Goldiloader::Compatibility.pre_rails_6_2?
18
+ if Goldiloader::Compatibility.pre_rails_7?
19
19
  ::ActiveRecord::Associations::Preloader.new.preload(models, [association_name])
20
20
  else
21
21
  ::ActiveRecord::Associations::Preloader.new(records: models, associations: [association_name]).call
@@ -37,7 +37,13 @@ module Goldiloader
37
37
  end
38
38
 
39
39
  def register_models(models)
40
- Array.wrap(models).each do |model|
40
+ # Don't use Array() or Array.wrap() because they will check respond_to?(:to_ary)
41
+ # which for ActiveStorage::Attachment will delegate to the blob association which
42
+ # triggers an infinite eager loading loop on the association
43
+ models = [models] unless models.is_a?(Array)
44
+ models.each do |model|
45
+ next if model.nil?
46
+
41
47
  model.auto_include_context = self
42
48
  self.models << model
43
49
  end
@@ -3,11 +3,22 @@
3
3
  module Goldiloader
4
4
  module Compatibility
5
5
  ACTIVE_RECORD_VERSION = ::Gem::Version.new(::ActiveRecord::VERSION::STRING).release
6
- PRE_RAILS_6_2 = ACTIVE_RECORD_VERSION < ::Gem::Version.new('6.2.0')
7
6
  RAILS_5_2_0 = ACTIVE_RECORD_VERSION == ::Gem::Version.new('5.2.0')
8
7
 
9
- def self.pre_rails_6_2?
10
- PRE_RAILS_6_2
8
+ def self.pre_rails_7?
9
+ ::ActiveRecord::VERSION::MAJOR < 7
10
+ end
11
+
12
+ def self.rails_5_2?
13
+ ::ActiveRecord::VERSION::MAJOR == 5 && ::ActiveRecord::VERSION::MINOR == 2
14
+ end
15
+
16
+ def self.rails_6_1?
17
+ ::ActiveRecord::VERSION::MAJOR == 6 && ::ActiveRecord::VERSION::MINOR == 1
18
+ end
19
+
20
+ def self.rails_6_1_or_greater?
21
+ ::ActiveRecord::VERSION::MAJOR > 6 || rails_6_1?
11
22
  end
12
23
 
13
24
  # See https://github.com/rails/rails/pull/32375
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Goldiloader
4
- VERSION = '4.0.0'
4
+ VERSION = '4.1.2'
5
5
  end
data/lib/goldiloader.rb CHANGED
@@ -8,3 +8,37 @@ require 'goldiloader/scope_info'
8
8
  require 'goldiloader/association_options'
9
9
  require 'goldiloader/association_loader'
10
10
  require 'goldiloader/active_record_patches'
11
+
12
+ module Goldiloader
13
+ class << self
14
+
15
+ # Sets the process-wide enabled status
16
+ attr_accessor :globally_enabled
17
+
18
+ def enabled?
19
+ Thread.current.fetch(:goldiloader_enabled, globally_enabled)
20
+ end
21
+
22
+ def enabled=(val)
23
+ Thread.current[:goldiloader_enabled] = val
24
+ end
25
+
26
+ def enabled
27
+ old_enabled = Thread.current[:goldiloader_enabled]
28
+ self.enabled = true
29
+ yield
30
+ ensure
31
+ self.enabled = old_enabled
32
+ end
33
+
34
+ def disabled
35
+ old_enabled = Thread.current[:goldiloader_enabled]
36
+ self.enabled = false
37
+ yield
38
+ ensure
39
+ self.enabled = old_enabled
40
+ end
41
+ end
42
+
43
+ self.globally_enabled = true
44
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: goldiloader
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Turkel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-08 00:00:00.000000000 Z
11
+ date: 2021-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -50,6 +50,26 @@ dependencies:
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
52
  version: '7.1'
53
+ - !ruby/object:Gem::Dependency
54
+ name: activestorage
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '5.2'
60
+ - - "<"
61
+ - !ruby/object:Gem::Version
62
+ version: '7.1'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '5.2'
70
+ - - "<"
71
+ - !ruby/object:Gem::Version
72
+ version: '7.1'
53
73
  - !ruby/object:Gem::Dependency
54
74
  name: appraisal
55
75
  requirement: !ruby/object:Gem::Requirement