class ActiveAny::Associations::Preloader
Constants
- NULL_RELATION
Public Instance Methods
preload(records, associations, scope = NULL_RELATION)
click to toggle source
# File lib/active_any/associations/preloader.rb, line 14 def preload(records, associations, scope = NULL_RELATION) records = Array.wrap(records).compact.uniq associations = Array.wrap(associations) if records.empty? [] else associations.flat_map do |association| preloaders_on association, records, scope end end end
Private Instance Methods
grouped_records(association, records)
click to toggle source
# File lib/active_any/associations/preloader.rb, line 50 def grouped_records(association, records) h = {} records.each do |record| next unless record assoc = record.association(association) klasses = h[assoc.reflection] ||= {} (klasses[assoc.klass] ||= []) << record end h end
preloader_for(reflection, owners, rhs_klass)
click to toggle source
# File lib/active_any/associations/preloader.rb, line 92 def preloader_for(reflection, owners, rhs_klass) return NullPreloader unless rhs_klass if owners.first.association(reflection.name).loaded? return AlreadyLoaded end reflection.check_preloadable! case reflection.macro when :has_many # reflection.options[:through] ? HasManyThrough : HasMany HasMany when :has_one # reflection.options[:through] ? HasOneThrough : HasOne HasOne when :belongs_to BelongsTo end end
preloaders_for_one(association, records, scope)
click to toggle source
# File lib/active_any/associations/preloader.rb, line 40 def preloaders_for_one(association, records, scope) grouped_records(association, records).flat_map do |reflection, klasses| klasses.map do |rhs_klass, rs| loader = preloader_for(reflection, rs, rhs_klass).new(rhs_klass, rs, reflection, scope) loader.run self loader end end end
preloaders_on(association, records, scope)
click to toggle source
# File lib/active_any/associations/preloader.rb, line 29 def preloaders_on(association, records, scope) case association when Symbol preloaders_for_one(association, records, scope) when String preloaders_for_one(association.to_sym, records, scope) else raise ArgumentError, "#{association.inspect} was not recognized for preload" end end