class DeepPreloader::WorklistEntry

Attributes

association_reflection[R]
child_spec[R]
model[R]

Public Class Methods

new(model, association_reflection, child_spec) click to toggle source
# File lib/deep_preloader.rb, line 204
def initialize(model, association_reflection, child_spec)
  @model = model
  @association_reflection = association_reflection
  @child_spec = child_spec
end

Public Instance Methods

association_macro() click to toggle source
# File lib/deep_preloader.rb, line 214
def association_macro
  @association_reflection.macro
end
association_name() click to toggle source
# File lib/deep_preloader.rb, line 210
def association_name
  @association_reflection.name
end
belongs_to?() click to toggle source
# File lib/deep_preloader.rb, line 222
def belongs_to?
  association_macro == :belongs_to
end
children() click to toggle source

Conceal the difference between singular and collection associations so that `load_children` can always `group_by` the key

# File lib/deep_preloader.rb, line 236
def children
  target = model.association(association_name).target

  if collection?
    target
  elsif target
    [target]
  else
    []
  end
end
children=(targets) click to toggle source
# File lib/deep_preloader.rb, line 248
def children=(targets)
  if collection?
    target = targets
  else
    if targets.size > 1
      raise RuntimeError.new('Internal preloader error: attempted to attach multiple children to a singular association')
    end

    target = targets.first
  end

  ActiveRecord::Base.logger&.debug("attaching children to #{model.inspect}.#{association_name}: #{targets}") if DEBUG

  association = model.association(association_name)
  association.loaded!
  association.target = target
  targets.each { |t| association.set_inverse_instance(t) }
end
collection?() click to toggle source
# File lib/deep_preloader.rb, line 226
def collection?
  association_macro == :has_many
end
key() click to toggle source
# File lib/deep_preloader.rb, line 230
def key
  model.read_attribute(parent_key_column)
end
loaded?() click to toggle source
# File lib/deep_preloader.rb, line 218
def loaded?
  model.association(association_name).loaded?
end
parent_key_column() click to toggle source
# File lib/deep_preloader.rb, line 267
def parent_key_column
  case association_macro
  when :belongs_to
    @association_reflection.foreign_key
  when :has_one, :has_many
    @association_reflection.active_record_primary_key
  else
    raise "Unsupported association type #{@association_reflection.macro}"
  end
end