goldiloader 3.2.0 → 4.1.1

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: f5ec15cfc51915f390119375f633b3438f08226e9ebac6e8523c46e69f91e16c
4
- data.tar.gz: 61894b325643cc364eaea59e9b3d16aa25f32a76aabbd0f30a126355dbd6d365
3
+ metadata.gz: 452d46a8d8dc72a51e686d522826ad8f80ad3e686ef03d61f34c8044458f1d3f
4
+ data.tar.gz: f4c428261a9d7d69858e065da735795b7be00bae3d6c4910b5e9d31064558e51
5
5
  SHA512:
6
- metadata.gz: b792fb0a79df0076eddbe2e451d7df31583bb2a7d6cd90c2aa50c3f55bc97839367f6f006e82688eab3f2d0e3e056a5a3ba9b881095f8ae88dbed3aedc44d1f5
7
- data.tar.gz: 920bb6c789be76a3e10c9bff4d74026d7ea7ceb18575217bccf5bfa3bf05653e093f0ea3a4188c5e14167e81d7fecced7578fcd81eca9abe19126543f54362a0
6
+ metadata.gz: ebf67521e4fed82d0d90b5f1597b296e0de04f53ab35b975c5e48383fa6392b0a4d0ed91225e16e471bfa87840144d68d590711e92353cd6138a637b94b5aa30
7
+ data.tar.gz: ee9fac3561f9e8c875f4bcbaca2fa9cbf7be9d3450f440e77eddcc3816da80fd4a38d534128e78fa154ce6ebaa83f99e502b838b7be106aa8bbe7db75466022e
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
@@ -51,13 +51,7 @@ module Goldiloader
51
51
  end
52
52
 
53
53
  def auto_include_value=(value)
54
- if Goldiloader::Compatibility.rails_4?
55
- raise ::ActiveRecord::Relation::ImmutableRelation if @loaded
56
- check_cached_relation
57
- else
58
- assert_mutability!
59
- end
60
-
54
+ assert_mutability!
61
55
  @values[:auto_include] = value
62
56
  end
63
57
  end
@@ -74,29 +68,26 @@ module Goldiloader
74
68
  ActiveRecord::Relation::Merger.prepend(::Goldiloader::MergerPatch)
75
69
 
76
70
  module AssociationReflectionPatch
77
- def eager_loadable?
78
- return @eager_loadable if instance_variable_defined?(:@eager_loadable)
79
-
80
- @eager_loadable = if scope.nil?
81
- # Associations without any scoping options are eager loadable
82
- true
83
- elsif scope.arity > 0
84
- # The scope will be evaluated for every model instance so it can't
85
- # be eager loaded
86
- false
87
- else
88
- scope_info = if Goldiloader::Compatibility.rails_4?
89
- Goldiloader::ScopeInfo.new(klass.unscoped.instance_exec(&scope) || klass.unscoped)
90
- else
91
- Goldiloader::ScopeInfo.new(scope_for(klass.unscoped))
92
- end
93
- scope_info.auto_include? &&
94
- !scope_info.limit? &&
95
- !scope_info.offset? &&
96
- (!has_one? || !scope_info.order?) &&
97
- (::Goldiloader::Compatibility.group_eager_loadable? || !scope_info.group?) &&
98
- (::Goldiloader::Compatibility.from_eager_loadable? || !scope_info.from?)
99
- 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]
100
91
  end
101
92
  end
102
93
  ActiveRecord::Reflection::AssociationReflection.include(::Goldiloader::AssociationReflectionPatch)
@@ -123,11 +114,13 @@ module Goldiloader
123
114
  private
124
115
 
125
116
  def eager_loadable?
126
- reflection.eager_loadable? &&
117
+ klass && reflection.eager_loadable?(klass) &&
127
118
  (::Goldiloader::Compatibility.destroyed_model_associations_eager_loadable? || !owner.destroyed?)
128
119
  end
129
120
 
130
121
  def load_with_auto_include
122
+ return yield unless Goldiloader.enabled?
123
+
131
124
  if loaded? && !stale_target?
132
125
  target
133
126
  elsif !auto_include?
@@ -159,12 +152,7 @@ module Goldiloader
159
152
 
160
153
  module CollectionAssociationPatch
161
154
  # Force these methods to load the entire association for fully_load associations
162
- association_methods = [:size, :ids_reader, :empty?]
163
- if Goldiloader::Compatibility::ACTIVE_RECORD_VERSION < ::Gem::Version.new('5.1')
164
- association_methods.concat([:first, :second, :third, :fourth, :fifth, :last])
165
- end
166
-
167
- association_methods.each do |method|
155
+ [:size, :ids_reader, :empty?].each do |method|
168
156
  define_method(method) do |*args, &block|
169
157
  load_target if fully_load?
170
158
  super(*args, &block)
@@ -175,10 +163,8 @@ module Goldiloader
175
163
  load_with_auto_include { super }
176
164
  end
177
165
 
178
- if Goldiloader::Compatibility::ACTIVE_RECORD_VERSION >= ::Gem::Version.new('5.1')
179
- def find_from_target?
180
- fully_load? || super
181
- end
166
+ def find_from_target?
167
+ fully_load? || super
182
168
  end
183
169
  end
184
170
  ::ActiveRecord::Associations::CollectionAssociation.prepend(::Goldiloader::CollectionAssociationPatch)
@@ -15,7 +15,11 @@ module Goldiloader
15
15
  private
16
16
 
17
17
  def eager_load(models, association_name)
18
- ::ActiveRecord::Associations::Preloader.new.preload(models, [association_name])
18
+ if Goldiloader::Compatibility.pre_rails_6_2?
19
+ ::ActiveRecord::Associations::Preloader.new.preload(models, [association_name])
20
+ else
21
+ ::ActiveRecord::Associations::Preloader.new(records: models, associations: [association_name]).call
22
+ end
19
23
  end
20
24
 
21
25
  def load?(model, association_name)
@@ -18,11 +18,7 @@ module Goldiloader
18
18
  end
19
19
 
20
20
  def register
21
- if Goldiloader::Compatibility.rails_4?
22
- ActiveRecord::Associations::Builder::Association.valid_options.concat(OPTIONS)
23
- else
24
- ActiveRecord::Associations::Builder::Association.extensions << AssociationBuilderExtension
25
- end
21
+ ActiveRecord::Associations::Builder::Association.extensions << AssociationBuilderExtension
26
22
  end
27
23
  end
28
24
  end
@@ -2,27 +2,17 @@
2
2
 
3
3
  module Goldiloader
4
4
  module Compatibility
5
- ACTIVE_RECORD_VERSION = ::Gem::Version.new(::ActiveRecord::VERSION::STRING)
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')
6
7
  RAILS_5_2_0 = ACTIVE_RECORD_VERSION == ::Gem::Version.new('5.2.0')
7
- FROM_EAGER_LOADABLE = ACTIVE_RECORD_VERSION >= ::Gem::Version.new('5.1.5') ||
8
- (ACTIVE_RECORD_VERSION >= ::Gem::Version.new('5.0.7') && ACTIVE_RECORD_VERSION < ::Gem::Version.new('5.1.0'))
9
- GROUP_EAGER_LOADABLE = FROM_EAGER_LOADABLE
10
8
 
11
- def self.rails_4?
12
- ::ActiveRecord::VERSION::MAJOR == 4
9
+ def self.pre_rails_6_2?
10
+ PRE_RAILS_6_2
13
11
  end
14
12
 
15
13
  # See https://github.com/rails/rails/pull/32375
16
14
  def self.destroyed_model_associations_eager_loadable?
17
15
  !RAILS_5_2_0
18
16
  end
19
-
20
- def self.from_eager_loadable?
21
- FROM_EAGER_LOADABLE
22
- end
23
-
24
- def self.group_eager_loadable?
25
- GROUP_EAGER_LOADABLE
26
- end
27
17
  end
28
18
  end
@@ -21,11 +21,7 @@ module Goldiloader
21
21
  end
22
22
 
23
23
  def from?
24
- if Goldiloader::Compatibility.rails_4?
25
- scope.from_value.present?
26
- else
27
- scope.from_clause.present?
28
- end
24
+ scope.from_clause.present?
29
25
  end
30
26
 
31
27
  def group?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Goldiloader
4
- VERSION = '3.2.0'
4
+ VERSION = '4.1.1'
5
5
  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: 3.2.0
4
+ version: 4.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Turkel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-10 00:00:00.000000000 Z
11
+ date: 2021-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,40 +16,40 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '4.2'
19
+ version: '5.2'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '6.3'
22
+ version: '7.1'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '4.2'
29
+ version: '5.2'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '6.3'
32
+ version: '7.1'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: activesupport
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
- version: '4.2'
39
+ version: '5.2'
40
40
  - - "<"
41
41
  - !ruby/object:Gem::Version
42
- version: '6.3'
42
+ version: '7.1'
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: '4.2'
49
+ version: '5.2'
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
- version: '6.3'
52
+ version: '7.1'
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: appraisal
55
55
  requirement: !ruby/object:Gem::Requirement
@@ -166,16 +166,16 @@ dependencies:
166
166
  name: salsify_rubocop
167
167
  requirement: !ruby/object:Gem::Requirement
168
168
  requirements:
169
- - - '='
169
+ - - "~>"
170
170
  - !ruby/object:Gem::Version
171
- version: 0.52.1.1
171
+ version: 1.0.1
172
172
  type: :development
173
173
  prerelease: false
174
174
  version_requirements: !ruby/object:Gem::Requirement
175
175
  requirements:
176
- - - '='
176
+ - - "~>"
177
177
  - !ruby/object:Gem::Version
178
- version: 0.52.1.1
178
+ version: 1.0.1
179
179
  - !ruby/object:Gem::Dependency
180
180
  name: simplecov
181
181
  requirement: !ruby/object:Gem::Requirement
@@ -236,14 +236,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
236
236
  requirements:
237
237
  - - ">="
238
238
  - !ruby/object:Gem::Version
239
- version: '2.3'
239
+ version: '2.6'
240
240
  required_rubygems_version: !ruby/object:Gem::Requirement
241
241
  requirements:
242
242
  - - ">="
243
243
  - !ruby/object:Gem::Version
244
244
  version: '0'
245
245
  requirements: []
246
- rubygems_version: 3.0.8
246
+ rubygems_version: 3.1.4
247
247
  signing_key:
248
248
  specification_version: 4
249
249
  summary: Automatic Rails association eager loading