Class: DeepPreloader::WorklistEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/deep_preloader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, association_reflection, child_spec) ⇒ WorklistEntry

Returns a new instance of WorklistEntry.



204
205
206
207
208
# 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

Instance Attribute Details

#association_reflectionObject (readonly)

Returns the value of attribute association_reflection.



202
203
204
# File 'lib/deep_preloader.rb', line 202

def association_reflection
  @association_reflection
end

#child_specObject (readonly)

Returns the value of attribute child_spec.



202
203
204
# File 'lib/deep_preloader.rb', line 202

def child_spec
  @child_spec
end

#modelObject (readonly)

Returns the value of attribute model.



202
203
204
# File 'lib/deep_preloader.rb', line 202

def model
  @model
end

Instance Method Details

#association_macroObject



214
215
216
# File 'lib/deep_preloader.rb', line 214

def association_macro
  @association_reflection.macro
end

#association_nameObject



210
211
212
# File 'lib/deep_preloader.rb', line 210

def association_name
  @association_reflection.name
end

#belongs_to?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/deep_preloader.rb', line 222

def belongs_to?
  association_macro == :belongs_to
end

#childrenObject

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



236
237
238
239
240
241
242
243
244
245
246
# 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) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# 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?Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/deep_preloader.rb', line 226

def collection?
  association_macro == :has_many
end

#keyObject



230
231
232
# File 'lib/deep_preloader.rb', line 230

def key
  model.read_attribute(parent_key_column)
end

#loaded?Boolean

Returns:

  • (Boolean)


218
219
220
# File 'lib/deep_preloader.rb', line 218

def loaded?
  model.association(association_name).loaded?
end

#parent_key_columnObject



267
268
269
270
271
272
273
274
275
276
# 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