miscellany 0.1.2 → 0.1.3

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: ee3173a46bc8dfb9c07bc8e6533776c332b6267e8d2465bada2e9d395192f199
4
- data.tar.gz: cfef42a0855f7fe772e0ae50d08aaaca4fa6dba6a69caf189c1fbea1d076a168
3
+ metadata.gz: 592322f721c82c3a296438d483a372ac5dc4b06d2da56e1b4d614999ed7f1cb4
4
+ data.tar.gz: affc6540ac3f73d4096a544ab45d67e617da481aa97334e399bea704c5055c1e
5
5
  SHA512:
6
- metadata.gz: 1035b6798d130d137a6feef1892dde982f04394fbdc228a63ab275685ba9cf460b84f909bdcae6bb5f41a78e952c237d331aaaf94ba4241d0c10bf984e24c3fe
7
- data.tar.gz: d07f4e9beacb29caa4d5ca6e22e265240dc87857bab04b5165ed8d1587090811495095ba0a95579bb8ceac2fc25e7fec2c7af1c04ce06ea370fa5137d30c5ea9
6
+ metadata.gz: e7c5dd2f97d9998656c7eae1a1ef69c7091c09fdc0d34f7b98ccce3d18067751d67ee0b6790fa2c355149703becfe124a32eb6f65fc9b620cd5425ea66e8f6e4
7
+ data.tar.gz: 2cb31549f3dda188c255e0e3fb11d2770039eda248f63612e88bcfea13422e26228efc451369e69f2e688d5a0627ccbb815457d73b7b443fc19bec1b312d357e
@@ -16,6 +16,9 @@
16
16
  # if you define a prefetch, it won't actually be loaded until you attempt to access it on one of the models.
17
17
  module Miscellany
18
18
  module ArbitraryPrefetch
19
+ ACTIVE_RECORD_VERSION = ::Gem::Version.new(::ActiveRecord::VERSION::STRING).release
20
+ PRE_RAILS_6_2 = ACTIVE_RECORD_VERSION < ::Gem::Version.new('6.2.0')
21
+
19
22
  class PrefetcherContext
20
23
  attr_accessor :model, :target_attribute
21
24
  attr_reader :options
@@ -49,7 +52,7 @@ module Miscellany
49
52
  @reflection ||= begin
50
53
  queryset = @queryset
51
54
  source_refl = model.reflections[@source_key.to_s]
52
- scope = lambda { |*_args|
55
+ scope = lambda {|*_args|
53
56
  qs = queryset
54
57
  qs = qs.merge(source_refl.scope_for(model.unscoped)) if source_refl.scope
55
58
  qs
@@ -83,14 +86,16 @@ module Miscellany
83
86
  return super if loaded?
84
87
 
85
88
  records = super
86
- preloader = nil
87
89
  (@values[:prefetches] || {}).each do |_key, opts|
88
90
  pfc = PrefetcherContext.new(model, opts)
89
91
  pfc.link_models(records)
90
92
 
91
93
  unless defined?(Goldiloader)
92
- preloader ||= build_preloader
93
- preloader.preload(records, opts[:attribute])
94
+ if PRE_RAILS_6_2
95
+ ::ActiveRecord::Associations::Preloader.new.preload(records, [opts[:attribute]])
96
+ else
97
+ ::ActiveRecord::Associations::Preloader.new(records: records, associations: [opts[:attribute]]).call
98
+ end
94
99
  end
95
100
  end
96
101
  records
@@ -143,15 +148,43 @@ module Miscellany
143
148
  end
144
149
  end
145
150
 
151
+ module ActiveRecordPreloaderPatch
152
+ if ACTIVE_RECORD_VERSION >= ::Gem::Version.new('6.0.0')
153
+ def grouped_records(association, records, polymorphic_parent)
154
+ h = {}
155
+ records.each do |record|
156
+ next unless record
157
+ reflection = record.class._reflect_on_association(association)
158
+ reflection ||= record.association(association)&.reflection
159
+ next if polymorphic_parent && !reflection || !record.association(association).klass
160
+ (h[reflection] ||= []) << record
161
+ end
162
+ h
163
+ end
164
+ end
165
+ end
166
+
167
+ module ActiveRecordReflectionPatch
168
+ def check_preloadable!
169
+ return if scope && scope.arity < 0
170
+ super
171
+ end
172
+ end
173
+
146
174
  def self.install
147
175
  ::ActiveRecord::Base.include(ActiveRecordBasePatch)
176
+
148
177
  ::ActiveRecord::Relation.prepend(ActiveRecordRelationPatch)
149
178
  ::ActiveRecord::Relation::Merger.prepend(ActiveRecordMergerPatch)
150
179
 
180
+ ::ActiveRecord::Associations::Preloader.prepend(ActiveRecordPreloaderPatch)
181
+
182
+ ::ActiveRecord::Reflection::AssociationReflection.prepend(ActiveRecordReflectionPatch)
183
+
151
184
  return unless defined? ::Goldiloader
152
185
 
153
186
  ::Goldiloader::AssociationLoader.module_eval do
154
- def self.has_association?(model, association_name) # rubocop:disable Naming/PredicateName
187
+ def self.has_association?(model, association_name)
155
188
  model.association(association_name)
156
189
  true
157
190
  rescue ::ActiveRecord::AssociationNotFoundError => _err
@@ -1,3 +1,3 @@
1
1
  module Miscellany
2
- VERSION = "0.1.2".freeze
2
+ VERSION = "0.1.3".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miscellany
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ethan Knapp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-05 00:00:00.000000000 Z
11
+ date: 2021-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -270,7 +270,6 @@ files:
270
270
  - lib/miscellany/controller/http_error_handling.rb
271
271
  - lib/miscellany/controller/json_uploads.rb
272
272
  - lib/miscellany/controller/sliced_response.rb
273
- - lib/miscellany/controller/spa_render.rb
274
273
  - lib/miscellany/param_validator.rb
275
274
  - lib/miscellany/version.rb
276
275
  - miscellany.gemspec
@@ -1,18 +0,0 @@
1
- module Miscellany
2
- module SpaRender
3
- extend ActiveSupport::Concern
4
-
5
- def render(*args, **kwargs)
6
- if kwargs[:spa].present?
7
- js_env(kwargs.delete(:env))
8
- kwargs[:template] ||= 'miscellany/spa_page'
9
- kwargs[:locals] ||= {}
10
- kwargs[:locals][:pack_name] ||= kwargs.delete(:spa)
11
- kwargs[:formats] = [:html]
12
- super(*args, **kwargs)
13
- else
14
- super
15
- end
16
- end
17
- end
18
- end