Module: AssociationSelection::V5::Preloader
- Defined in:
- lib/association_selection/v5/preloader.rb
Constant Summary collapse
- NULL_RELATION =
Struct.new(:values, :where_clause, :joins_values) .new({}, ::ActiveRecord::Relation::WhereClause.empty, [])
Instance Attribute Summary collapse
-
#assoc_select_fields ⇒ Object
Returns the value of attribute assoc_select_fields.
-
#relation_klass ⇒ Object
Returns the value of attribute relation_klass.
Instance Method Summary collapse
Instance Attribute Details
#assoc_select_fields ⇒ Object
Returns the value of attribute assoc_select_fields.
6 7 8 |
# File 'lib/association_selection/v5/preloader.rb', line 6 def assoc_select_fields @assoc_select_fields end |
#relation_klass ⇒ Object
Returns the value of attribute relation_klass.
6 7 8 |
# File 'lib/association_selection/v5/preloader.rb', line 6 def relation_klass @relation_klass end |
Instance Method Details
#preload(records, associations, preload_scope = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/association_selection/v5/preloader.rb', line 11 def preload(records, associations, preload_scope = nil) records = Array.wrap(records).compact.uniq associations = Array.wrap(associations) preload_scope ||= nil return [] if records.empty? any_specified_fields = relation_klass.present? && assoc_select_fields.is_a?(Hash) indexed_reflections = relation_klass.reflections.map { |k, r| [k, r.class_name] }.to_h if any_specified_fields associations.flat_map do |association| if any_specified_fields && indexed_reflections.key?(association.to_s) preloaders_on( association, records, indexed_reflections[association.to_s].constantize.select(assoc_select_fields[association]) ) else preloaders_on association, records, preload_scope end end end |