goldiloader 3.1.0 → 4.1.0

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
- SHA1:
3
- metadata.gz: 4aa4a5f31beb13f5c8988e170cc8de16e2441305
4
- data.tar.gz: da00673ed5e3081b0ab5d9befc34f833a31ca244
2
+ SHA256:
3
+ metadata.gz: d30741df3c728239a50f4668f908f40cb0be785d5403848648faccc32b329d9d
4
+ data.tar.gz: 7032140f7401f0e47ac717b48d1089a6211d1ea7c78c3c64a1c0879882a47fbc
5
5
  SHA512:
6
- metadata.gz: a3f519f3637cf37c7e4db93dca4430ef8f0ed8de7552ceb53aaa0ff476db8e4445e9ac334da081115d57865d6cc8ba3dbb8cad6179cfce193b6d43953f2b2dd8
7
- data.tar.gz: 4008049d2133866071087e6095667e3157b716ebc920fe26c2f3d2d1e983fca7b07683fb6b48b241febcfc59be5d066dbed7b1accdee0d4302d7f59dbe8b123f
6
+ metadata.gz: b9ae29eddaba860fa1a9075aae6788629f56e2d47bb71afef20dc39f587070c8572820694d1650a50d625d142739de419c7c9617a6cae6b6a4257fd753a3b2a8
7
+ data.tar.gz: cf79ee0b429d1716371ae655cae72dc811fec672d53c69409f24a46301326e90c5a2c02f60332b7ec3e4581c7a984e2cfbdb658fdbca11c8df340e53f9c217bc
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 = enabled?
28
+ self.enabled = true
29
+ yield
30
+ ensure
31
+ self.enabled = old_enabled
32
+ end
33
+
34
+ def disabled
35
+ old_enabled = 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
@@ -47,21 +47,12 @@ module Goldiloader
47
47
  end
48
48
 
49
49
  def auto_include_value
50
- # Note: Don't use get_value because that doesn't work properly with defaulting boolean values
51
50
  @values.fetch(:auto_include, true)
52
51
  end
53
52
 
54
53
  def auto_include_value=(value)
55
- if Goldiloader::Compatibility.rails_4?
56
- raise ::ActiveRecord::Relation::ImmutableRelation if @loaded
57
- check_cached_relation
58
- @values[:auto_include] = value
59
- elsif Goldiloader::Compatibility.rails_5_0?
60
- assert_mutability!
61
- @values[:auto_include] = value
62
- else
63
- set_value(:auto_include, value)
64
- end
54
+ assert_mutability!
55
+ @values[:auto_include] = value
65
56
  end
66
57
  end
67
58
  ::ActiveRecord::Relation.prepend(::Goldiloader::RelationPatch)
@@ -77,29 +68,26 @@ module Goldiloader
77
68
  ActiveRecord::Relation::Merger.prepend(::Goldiloader::MergerPatch)
78
69
 
79
70
  module AssociationReflectionPatch
80
- def eager_loadable?
81
- return @eager_loadable if instance_variable_defined?(:@eager_loadable)
82
-
83
- @eager_loadable = if scope.nil?
84
- # Associations without any scoping options are eager loadable
85
- true
86
- elsif scope.arity > 0
87
- # The scope will be evaluated for every model instance so it can't
88
- # be eager loaded
89
- false
90
- else
91
- scope_info = if Goldiloader::Compatibility.rails_4?
92
- Goldiloader::ScopeInfo.new(klass.unscoped.instance_exec(&scope) || klass.unscoped)
93
- else
94
- Goldiloader::ScopeInfo.new(scope_for(klass.unscoped))
95
- end
96
- scope_info.auto_include? &&
97
- !scope_info.limit? &&
98
- !scope_info.offset? &&
99
- (!has_one? || !scope_info.order?) &&
100
- (::Goldiloader::Compatibility.group_eager_loadable? || !scope_info.group?) &&
101
- (::Goldiloader::Compatibility.from_eager_loadable? || !scope_info.from?)
102
- 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]
103
91
  end
104
92
  end
105
93
  ActiveRecord::Reflection::AssociationReflection.include(::Goldiloader::AssociationReflectionPatch)
@@ -126,11 +114,13 @@ module Goldiloader
126
114
  private
127
115
 
128
116
  def eager_loadable?
129
- reflection.eager_loadable? &&
117
+ klass && reflection.eager_loadable?(klass) &&
130
118
  (::Goldiloader::Compatibility.destroyed_model_associations_eager_loadable? || !owner.destroyed?)
131
119
  end
132
120
 
133
121
  def load_with_auto_include
122
+ return yield unless Goldiloader.enabled?
123
+
134
124
  if loaded? && !stale_target?
135
125
  target
136
126
  elsif !auto_include?
@@ -162,12 +152,7 @@ module Goldiloader
162
152
 
163
153
  module CollectionAssociationPatch
164
154
  # Force these methods to load the entire association for fully_load associations
165
- association_methods = [:size, :ids_reader, :empty?]
166
- if Goldiloader::Compatibility::ACTIVE_RECORD_VERSION < ::Gem::Version.new('5.1')
167
- association_methods.concat([:first, :second, :third, :fourth, :fifth, :last])
168
- end
169
-
170
- association_methods.each do |method|
155
+ [:size, :ids_reader, :empty?].each do |method|
171
156
  define_method(method) do |*args, &block|
172
157
  load_target if fully_load?
173
158
  super(*args, &block)
@@ -178,10 +163,8 @@ module Goldiloader
178
163
  load_with_auto_include { super }
179
164
  end
180
165
 
181
- if Goldiloader::Compatibility::ACTIVE_RECORD_VERSION >= ::Gem::Version.new('5.1')
182
- def find_from_target?
183
- fully_load? || super
184
- end
166
+ def find_from_target?
167
+ fully_load? || super
185
168
  end
186
169
  end
187
170
  ::ActiveRecord::Associations::CollectionAssociation.prepend(::Goldiloader::CollectionAssociationPatch)
@@ -15,17 +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])
19
- end
20
-
21
- def first_model_with_association(models, association_name)
22
- models.find { |model| has_association?(model, association_name) }
23
- end
24
-
25
- def associated_models(models, association_name)
26
- # We can't just do model.send(association_name) because the association method may have been
27
- # overridden
28
- models.map { |model| model.association(association_name).target }.flatten.compact.uniq
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
29
23
  end
30
24
 
31
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,34 +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
- PRE_RAILS_5_2 = ACTIVE_RECORD_VERSION < ::Gem::Version.new('5.2.0')
8
- POST_RAILS_5_1_4 = ACTIVE_RECORD_VERSION > ::Gem::Version.new('5.1.5')
9
- PRE_RAILS_5_1_5 = ACTIVE_RECORD_VERSION < ::Gem::Version.new('5.1.5')
10
- FROM_EAGER_LOADABLE = ACTIVE_RECORD_VERSION >= ::Gem::Version.new('5.1.5') ||
11
- (ACTIVE_RECORD_VERSION >= ::Gem::Version.new('5.0.7') && ACTIVE_RECORD_VERSION < ::Gem::Version.new('5.1.0'))
12
- GROUP_EAGER_LOADABLE = FROM_EAGER_LOADABLE
13
8
 
14
- def self.rails_4?
15
- ::ActiveRecord::VERSION::MAJOR == 4
16
- end
17
-
18
- def self.rails_5_0?
19
- ::ActiveRecord::VERSION::MAJOR == 5 && ::ActiveRecord::VERSION::MINOR == 0
9
+ def self.pre_rails_6_2?
10
+ PRE_RAILS_6_2
20
11
  end
21
12
 
22
13
  # See https://github.com/rails/rails/pull/32375
23
14
  def self.destroyed_model_associations_eager_loadable?
24
15
  !RAILS_5_2_0
25
16
  end
26
-
27
- def self.from_eager_loadable?
28
- FROM_EAGER_LOADABLE
29
- end
30
-
31
- def self.group_eager_loadable?
32
- GROUP_EAGER_LOADABLE
33
- end
34
17
  end
35
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.1.0'
4
+ VERSION = '4.1.0'
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.1.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Turkel
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-11 00:00:00.000000000 Z
11
+ date: 2021-05-11 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.1'
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.1'
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.1'
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.1'
52
+ version: '7.1'
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: appraisal
55
55
  requirement: !ruby/object:Gem::Requirement
@@ -79,19 +79,19 @@ dependencies:
79
79
  - !ruby/object:Gem::Version
80
80
  version: '0'
81
81
  - !ruby/object:Gem::Dependency
82
- name: coveralls
82
+ name: coveralls_reborn
83
83
  requirement: !ruby/object:Gem::Requirement
84
84
  requirements:
85
85
  - - ">="
86
86
  - !ruby/object:Gem::Version
87
- version: '0'
87
+ version: 0.18.0
88
88
  type: :development
89
89
  prerelease: false
90
90
  version_requirements: !ruby/object:Gem::Requirement
91
91
  requirements:
92
92
  - - ">="
93
93
  - !ruby/object:Gem::Version
94
- version: '0'
94
+ version: 0.18.0
95
95
  - !ruby/object:Gem::Dependency
96
96
  name: database_cleaner
97
97
  requirement: !ruby/object:Gem::Requirement
@@ -148,20 +148,34 @@ dependencies:
148
148
  - - "~>"
149
149
  - !ruby/object:Gem::Version
150
150
  version: '3'
151
+ - !ruby/object:Gem::Dependency
152
+ name: rspec_junit_formatter
153
+ requirement: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ type: :development
159
+ prerelease: false
160
+ version_requirements: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
151
165
  - !ruby/object:Gem::Dependency
152
166
  name: salsify_rubocop
153
167
  requirement: !ruby/object:Gem::Requirement
154
168
  requirements:
155
- - - '='
169
+ - - "~>"
156
170
  - !ruby/object:Gem::Version
157
- version: 0.52.1.1
171
+ version: 1.0.1
158
172
  type: :development
159
173
  prerelease: false
160
174
  version_requirements: !ruby/object:Gem::Requirement
161
175
  requirements:
162
- - - '='
176
+ - - "~>"
163
177
  - !ruby/object:Gem::Version
164
- version: 0.52.1.1
178
+ version: 1.0.1
165
179
  - !ruby/object:Gem::Dependency
166
180
  name: simplecov
167
181
  requirement: !ruby/object:Gem::Requirement
@@ -180,16 +194,16 @@ dependencies:
180
194
  name: sqlite3
181
195
  requirement: !ruby/object:Gem::Requirement
182
196
  requirements:
183
- - - ">="
197
+ - - "~>"
184
198
  - !ruby/object:Gem::Version
185
- version: '0'
199
+ version: '1.3'
186
200
  type: :development
187
201
  prerelease: false
188
202
  version_requirements: !ruby/object:Gem::Requirement
189
203
  requirements:
190
- - - ">="
204
+ - - "~>"
191
205
  - !ruby/object:Gem::Version
192
- version: '0'
206
+ version: '1.3'
193
207
  description: Automatically eager loads Rails associations as associations are traversed
194
208
  email:
195
209
  - jturkel@salsify.com
@@ -214,7 +228,7 @@ metadata:
214
228
  changelog_uri: https://github.com/salsify/goldiloader/blob/master/CHANGELOG.md
215
229
  source_code_uri: https://github.com/salsify/goldiloader/
216
230
  bug_tracker_uri: https://github.com/salsify/goldiloader/issues
217
- post_install_message:
231
+ post_install_message:
218
232
  rdoc_options: []
219
233
  require_paths:
220
234
  - lib
@@ -222,16 +236,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
222
236
  requirements:
223
237
  - - ">="
224
238
  - !ruby/object:Gem::Version
225
- version: '2.3'
239
+ version: '2.6'
226
240
  required_rubygems_version: !ruby/object:Gem::Requirement
227
241
  requirements:
228
242
  - - ">="
229
243
  - !ruby/object:Gem::Version
230
244
  version: '0'
231
245
  requirements: []
232
- rubyforge_project:
233
- rubygems_version: 2.6.14
234
- signing_key:
246
+ rubygems_version: 3.1.4
247
+ signing_key:
235
248
  specification_version: 4
236
249
  summary: Automatic Rails association eager loading
237
250
  test_files: []