Class: RPCMapper::Association::HasManyThrough

Inherits:
HasMany show all
Defined in:
lib/rpc_mapper/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 RPCMapper::Association::Base

Instance Attribute Details

#proxy_associationObject

Returns the value of attribute proxy_association.



242
243
244
# File 'lib/rpc_mapper/association.rb', line 242

def proxy_association
  @proxy_association
end

#target_associationObject

Returns the value of attribute target_association.



243
244
245
# File 'lib/rpc_mapper/association.rb', line 243

def target_association
  @target_association
end

Instance Method Details

#polymorphic?Boolean

Returns:

  • (Boolean)


323
324
325
# File 'lib/rpc_mapper/association.rb', line 323

def polymorphic?
  false
end

#scope(object) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/rpc_mapper/association.rb', line 266

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 => 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 =>
        RPCMapper::Base.base_class_name(proxy_association.target_klass(object))
    )
  end

  relation
end

#target_is_has?Boolean

Returns:

  • (Boolean)


315
316
317
# File 'lib/rpc_mapper/association.rb', line 315

def target_is_has?
  target_association.is_a?(Has)
end

#target_klassObject



307
308
309
# File 'lib/rpc_mapper/association.rb', line 307

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

#target_typeObject



311
312
313
# File 'lib/rpc_mapper/association.rb', line 311

def target_type
  target_association.type
end

#typeObject



319
320
321
# File 'lib/rpc_mapper/association.rb', line 319

def type
  :has_many_through
end