Module: Miscellany::ArbitraryPrefetch

Defined in:
lib/miscellany/active_record/arbitrary_prefetch.rb

Defined Under Namespace

Modules: ActiveRecordPatches Classes: PrefetcherContext

Constant Summary collapse

ACTIVE_RECORD_VERSION =
::Gem::Version.new(::ActiveRecord::VERSION::STRING).release
PRE_RAILS_6_2 =
ACTIVE_RECORD_VERSION < ::Gem::Version.new('6.2.0')

Class Method Summary collapse

Class Method Details

.apply_patches(mod, install_base, base_module: nil) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/miscellany/active_record/arbitrary_prefetch.rb', line 201

def self.apply_patches(mod, install_base, base_module: nil)
  return unless mod.is_a?(Module)

  base_module ||= mod

  if mod.name.ends_with? "Patch"
    base_full_name = base_module.to_s
    mod_full_name = mod.to_s
    mod_rel_name = mod_full_name.sub(base_full_name, '')
    mod_rel_bits = mod_rel_name.split('::').select(&:present?).map do |bit|
      bit.ends_with?('Patch') ? bit[0..-6] : bit
    end
    final_mod_name = [install_base, *mod_rel_bits].select(&:present?).join("::")
    install_mod = final_mod_name.constantize

    if mod.is_a?(ActiveSupport::Concern)
      install_mod.include(mod)
    else
      install_mod.prepend(mod)
    end
  end

  mod.constants.map {|const| mod.const_get(const) }.each do |const|
    apply_patches(const, install_base, base_module: base_module || mod)
  end
end

.installObject



228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/miscellany/active_record/arbitrary_prefetch.rb', line 228

def self.install
  apply_patches(ActiveRecordPatches, ::ActiveRecord)

  return unless defined? ::Goldiloader

  ::Goldiloader::AssociationLoader.module_eval do
    def self.has_association?(model, association_name)
      model.association(association_name)
      true
    rescue ::ActiveRecord::AssociationNotFoundError => _err
      false
    end
  end
end