class SmartPreloads::Loader

Attributes

collection[R]

Public Class Methods

new(collection, nested_associations = nil) click to toggle source
# File lib/smart_preloads/loader.rb, line 5
def initialize(collection, nested_associations = nil)
  @collection = collection
  @nested_associations = nested_associations
end

Public Instance Methods

load(association) click to toggle source
# File lib/smart_preloads/loader.rb, line 14
def load(association)
  association = nest(association)
  return unless association
  ActiveRecord::Associations::Preloader.new.preload(@collection, association)
end
load_default() click to toggle source
# File lib/smart_preloads/loader.rb, line 10
def load_default
  load(nil)
end
nest(association) click to toggle source
# File lib/smart_preloads/loader.rb, line 25
def nest(association)
  return association unless @nested_associations
  result = association
  Array(@nested_associations).reverse.each do |parent|
    if result
      result = { parent => result }
    else
      result = parent
    end
  end
  result
end
nested(nested_associations) click to toggle source
# File lib/smart_preloads/loader.rb, line 20
def nested(nested_associations)
  self.class.new(@collection, Array(@nested_associations) +
                              Array(nested_associations))
end