Sha256: 6aff5556aa151f59de1dc9f463197157d026cde469fcd0fda260c6ac0009f930

Contents?: true

Size: 847 Bytes

Versions: 4

Compression:

Stored size: 847 Bytes

Contents

module EdgeRider
  module PreloadAssociations
    module PreloadAssociationsInstanceMethod
      def preload_associations(*args)
        self.class.preload_associations([self], *args)
      end
    end

    def preload_associations(*args)
      preloader = ActiveRecord::Associations::Preloader

      if preloader.method_defined?(:run) # Rails 3.2 / Rails 4
        preloader.new(*args).run
      elsif preloader.method_defined?(:preload) # Rails 5 to Rails 6.1
        preloader.new.preload(*args)
      else # Rails 7+
        records = args.first
        associations = args.second
        options = args[2] || {}
        preloader.new(records: records, associations: associations, **options).call
      end
    end

    ActiveRecord::Base.send(:extend, self)
    ActiveRecord::Base.send(:include, PreloadAssociationsInstanceMethod)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
edge_rider-2.3.0 lib/edge_rider/preload_associations.rb
edge_rider-2.2.0 lib/edge_rider/preload_associations.rb
edge_rider-2.1.1 lib/edge_rider/preload_associations.rb
edge_rider-2.1.0 lib/edge_rider/preload_associations.rb