Class: Priloo::Preloaders::ArAssociationPreloader

Inherits:
BasePreloader
  • Object
show all
Defined in:
lib/priloo/preloaders/ar_association_preloader.rb

Overview

This class implements preloading for ActiveRecord associations

Instance Attribute Summary collapse

Attributes inherited from BasePreloader

#merge_key

Instance Method Summary collapse

Methods inherited from BasePreloader

#dependencies, #inject, #multiplicity

Constructor Details

#initialize(ar_class, name) ⇒ ArAssociationPreloader

Returns a new instance of ArAssociationPreloader.



9
10
11
12
13
14
# File 'lib/priloo/preloaders/ar_association_preloader.rb', line 9

def initialize(ar_class, name)
    super([self.class, ar_class, name])

    @name = name
    @ar_class = ar_class
end

Instance Attribute Details

#ar_classObject (readonly)

Returns the value of attribute ar_class.



7
8
9
# File 'lib/priloo/preloaders/ar_association_preloader.rb', line 7

def ar_class
  @ar_class
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/priloo/preloaders/ar_association_preloader.rb', line 7

def name
  @name
end

Instance Method Details

#extract(target) ⇒ Object



20
21
22
# File 'lib/priloo/preloaders/ar_association_preloader.rb', line 20

def extract(target)
    target.send(name)
end

#injected?(target) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/priloo/preloaders/ar_association_preloader.rb', line 16

def injected?(target)
    target.association_cached?(name)
end

#preload(ar_list) ⇒ Object



24
25
26
27
28
29
# File 'lib/priloo/preloaders/ar_association_preloader.rb', line 24

def preload(ar_list)
    # Rails does not provide any way to preload an association without immediately
    # storing the result in the instances.
    ActiveRecord::Associations::Preloader.new.preload(ar_list.map(&:_preloadable_target), name)
    ar_list.map(&name)
end