Class: Dase::Preloader

Inherits:
ActiveRecord::Associations::Preloader
  • Object
show all
Defined in:
lib/dase/preloader.rb

Defined Under Namespace

Classes: HasMany, HasManyThrough

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Preloader

Returns a new instance of Preloader.



26
27
28
# File 'lib/dase/preloader.rb', line 26

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



24
25
26
# File 'lib/dase/preloader.rb', line 24

def options
  @options
end

Instance Method Details

#preloader_for(reflection, owners, rhs_klass) ⇒ Object

an overloaded version of ActiveRecord::Associations::Preloader’s preloader_for which returns a class of a custom preloader for a given association



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dase/preloader.rb', line 32

def preloader_for(reflection, owners, rhs_klass)
  return NullPreloader unless rhs_klass

  if owners.first.association(reflection.name).loaded?
    return AlreadyLoaded
  end

  case reflection.macro
    when :has_many
      reflection.options[:through] ? HasManyThrough : HasMany
    when :has_one, :belongs_to
      raise ArgumentError, "You can't use includes_count_of with a #{reflection.macro} association"
    else
      raise NotImplementedError, "#{reflection.macro} not supported"
  end
end