Sha256: 3fe1604c82a1bd75f3a75b8eac3ac903fe487e07b862bb15987d7e132c726b41

Contents?: true

Size: 833 Bytes

Versions: 4

Compression:

Stored size: 833 Bytes

Contents

module Btspm
  module EagerLoadable

    def eager_load(collection, *associations)
      EagerLoader.load(collection, *associations)
    end

    class EagerLoader
      def initialize(coll, *assocs)
        @collection = coll
        @associations = assocs
      end

      def self.load(collection, *associations)
        preloader = new(collection, *associations)
        preloader.load
        collection
      end

      def load
        preloader = ActiveRecord::Associations::Preloader.new
        preloader.preload(collection, associations)
      end

      private

      def true_collection?
        !@collection.is_a?(ActiveRecord::Base)
      end

      def collection
        true_collection? ? @collection : [@collection]
      end

      attr_reader :associations
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
btspm-0.0.4 lib/btspm/eager_loadable.rb
btspm-0.0.3 lib/btspm/eager_loadable.rb
btspm-0.0.2 lib/btspm/eager_loadable.rb
btspm-0.0.1 lib/btspm/eager_loadable.rb