Class: Priloo::Preloaders::ArAssociationPreloader
- Inherits:
-
BasePreloader
- Object
- BasePreloader
- Priloo::Preloaders::ArAssociationPreloader
- Defined in:
- lib/priloo/preloaders/ar_association_preloader.rb
Overview
This class implements preloading for ActiveRecord associations
Instance Attribute Summary collapse
-
#ar_class ⇒ Object
readonly
Returns the value of attribute ar_class.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Attributes inherited from BasePreloader
Instance Method Summary collapse
- #extract(target) ⇒ Object
-
#initialize(ar_class, name) ⇒ ArAssociationPreloader
constructor
A new instance of ArAssociationPreloader.
- #injected?(target) ⇒ Boolean
- #preload(ar_list) ⇒ Object
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_class ⇒ Object (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 |
#name ⇒ Object (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
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 |