Class: RPCMapper::Association::Has

Inherits:
Base
  • Object
show all
Defined in:
lib/rpc_mapper/association.rb

Direct Known Subclasses

HasMany, HasOne

Instance Attribute Summary

Attributes inherited from Base

#id, #options, #source_klass

Instance Method Summary collapse

Methods inherited from Base

#collection?, #eager_loadable?, #initialize, #primary_key, #target_klass, #type

Constructor Details

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

Instance Method Details

#foreign_keyObject



107
108
109
# File 'lib/rpc_mapper/association.rb', line 107

def foreign_key
  super || (self.polymorphic? ? "#{options[:as]}_id" : "#{RPCMapper::Base.base_class_name(source_klass).downcase}_id").to_sym
end

#polymorphic?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/rpc_mapper/association.rb', line 129

def polymorphic?
  !!options[:as]
end

#scope(object) ⇒ Object

Returns a scope on the target containing this association

Builds conditions on top of the base_scope generated from any finder options set with the association

has_many :widgets, :class_name => “Widget”, :foreign_key => :widget_id has_many :comments, :as => :parent

In addition to any finder options included with the association options the following will be added:

where(widget_id => source[:id])

Or for the polymorphic :comments association:

where(:parent_id => source[:id], :parent_type => source.class)


123
124
125
126
127
# File 'lib/rpc_mapper/association.rb', line 123

def scope(object)
  s = base_scope(object).where(self.foreign_key => object[self.primary_key]) if object[self.primary_key]
  s = s.where(:"#{options[:as]}_type" => RPCMapper::Base.base_class_name(object.class)) if s && polymorphic?
  s
end