Class: Perry::Association::HasManyThrough

Inherits:
HasMany show all
Defined in:
lib/perry/association.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#id, #options, #source_klass

Instance Method Summary collapse

Methods inherited from HasMany

#collection?

Methods inherited from Has

#foreign_key, #polymorphic_type

Methods inherited from Base

#collection?, #eager_loadable?, #foreign_key, #initialize, #primary_key

Constructor Details

This class inherits a constructor from Perry::Association::Base

Instance Attribute Details

#proxy_associationObject

Returns the value of attribute proxy_association.



271
272
273
# File 'lib/perry/association.rb', line 271

def proxy_association
  @proxy_association
end

#target_associationObject

Returns the value of attribute target_association.



272
273
274
# File 'lib/perry/association.rb', line 272

def target_association
  @target_association
end

Instance Method Details

#polymorphic?Boolean

Returns:

  • (Boolean)


352
353
354
# File 'lib/perry/association.rb', line 352

def polymorphic?
  false
end

#scope(object) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/perry/association.rb', line 295

def scope(object)
  # Which attribute's values should be used on the proxy
  key = if target_is_has?
    target_association.primary_key.to_sym
  else
    target_association.foreign_key.to_sym
  end

  # Fetch the ids of all records on the proxy using the correct key
  proxy_ids = proc do
    proxy_association.scope(object).select(key).collect(&key)
  end

  # Use these ids to build a scope on the target object
  relation = target_klass.scoped

  if target_is_has?
    relation = relation.where(
      proc do
        { target_association.foreign_key => proxy_ids.call }
      end
    )
  else
    relation = relation.where(
      proc do
        { target_association.primary_key(options[:source_type]) => proxy_ids.call }
      end
    )
  end

  # Add polymorphic type condition if target is polymorphic and has
  if target_association.polymorphic? && target_is_has?
    relation = relation.where(
      target_association.polymorphic_type =>
        Perry::Base.base_class_name(proxy_association.target_klass(object))
    )
  end

  relation
end

#target_is_has?Boolean

Returns:

  • (Boolean)


344
345
346
# File 'lib/perry/association.rb', line 344

def target_is_has?
  target_association.is_a?(Has)
end

#target_klassObject



336
337
338
# File 'lib/perry/association.rb', line 336

def target_klass
  target_association.target_klass(options[:source_type])
end

#target_typeObject



340
341
342
# File 'lib/perry/association.rb', line 340

def target_type
  target_association.type
end

#typeObject



348
349
350
# File 'lib/perry/association.rb', line 348

def type
  :has_many_through
end