Class: PolymorphicPreloader
- Inherits:
-
Object
- Object
- PolymorphicPreloader
- Defined in:
- lib/polymorphic_preloader.rb,
lib/polymorphic_preloader/version.rb
Constant Summary collapse
- VERSION =
'0.0.1'
Instance Attribute Summary collapse
-
#association_name ⇒ Object
Returns the value of attribute association_name.
-
#grouped_objects ⇒ Object
Returns the value of attribute grouped_objects.
Instance Method Summary collapse
-
#initialize(objects, association_name) ⇒ PolymorphicPreloader
constructor
A new instance of PolymorphicPreloader.
- #preload!(associations) ⇒ Object
Constructor Details
#initialize(objects, association_name) ⇒ PolymorphicPreloader
Returns a new instance of PolymorphicPreloader.
6 7 8 9 |
# File 'lib/polymorphic_preloader.rb', line 6 def initialize(objects, association_name) self.association_name = association_name self.grouped_objects = objects.group_by { |object| object.send("#{association_name}_type") } end |
Instance Attribute Details
#association_name ⇒ Object
Returns the value of attribute association_name.
4 5 6 |
# File 'lib/polymorphic_preloader.rb', line 4 def association_name @association_name end |
#grouped_objects ⇒ Object
Returns the value of attribute grouped_objects.
4 5 6 |
# File 'lib/polymorphic_preloader.rb', line 4 def grouped_objects @grouped_objects end |
Instance Method Details
#preload!(associations) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/polymorphic_preloader.rb', line 11 def preload!(associations) grouped_objects.each_pair do |object_type, objects_with_same_association| target_associations = associations[object_type.to_s.underscore.to_sym] if target_associations.present? preloader.preload objects_with_same_association, association_name => target_associations end end end |