Class: RPCMapper::Association::Has
- Defined in:
- lib/rpc_mapper/association.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #foreign_key ⇒ Object
- #polymorphic? ⇒ Boolean
- #polymorphic_type ⇒ Object
-
#scope(object) ⇒ Object
Returns a scope on the target containing this association.
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_key ⇒ Object
167 168 169 170 171 172 173 |
# File 'lib/rpc_mapper/association.rb', line 167 def foreign_key super || if self.polymorphic? "#{[:as]}_id" else "#{RPCMapper::Base.base_class_name(source_klass).downcase}_id" end.to_sym end |
#polymorphic? ⇒ Boolean
208 209 210 |
# File 'lib/rpc_mapper/association.rb', line 208 def polymorphic? !![:as] end |
#polymorphic_type ⇒ Object
204 205 206 |
# File 'lib/rpc_mapper/association.rb', line 204 def polymorphic_type :"#{[:as]}_type" 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( => source[:id])
Or for the polymorphic :comments association:
where(:parent_id => source[:id], :parent_type => source.class)
194 195 196 197 198 199 200 201 202 |
# File 'lib/rpc_mapper/association.rb', line 194 def scope(object) return nil unless object[self.primary_key] s = base_scope(object).where(self.foreign_key => object[self.primary_key]) if polymorphic? s = s.where( polymorphic_type => RPCMapper::Base.base_class_name(object.class) ) end s end |