Returns a set of keys that identify the target model
@return [DataMapper::PropertySet]
a set of properties that identify the target model
@api semipublic
# File lib/dm-core/associations/many_to_many.rb, line 15 def child_key return @child_key if defined?(@child_key) repository_name = child_repository_name || parent_repository_name properties = child_model.properties(repository_name) @child_key = if @child_properties child_key = properties.values_at(*@child_properties) properties.class.new(child_key).freeze else properties.key end end
Eager load the collection using the source as a base
@param [Resource, Collection] source
the source to query with
@param [Query, Hash] other_query
optional query to restrict the collection
@return [ManyToMany::Collection]
the loaded collection for the source
@api private
# File lib/dm-core/associations/many_to_many.rb, line 158 def eager_load(source, other_query = nil) # FIXME: enable SEL for m:m relationships source.model.all(query_for(source, other_query)) end
Initialize the chain for “many to many” relationships
@api public
# File lib/dm-core/associations/many_to_many.rb, line 130 def finalize through via end
@api semipublic
# File lib/dm-core/associations/many_to_many.rb, line 110 def links return @links if defined?(@links) @links = [] links = [ through, via ] while relationship = links.shift if relationship.respond_to?(:links) links.unshift(*relationship.links) else @links << relationship end end @links.freeze end
@api private
# File lib/dm-core/associations/many_to_many.rb, line 141 def query # TODO: consider making this a query_for method, so that ManyToMany::Relationship#query only # returns the query supplied in the definition @many_to_many_query ||= super.merge(:links => links).freeze end
@api private
# File lib/dm-core/associations/many_to_many.rb, line 136 def source_scope(source) { through.inverse => source } end
Intermediate association for through model relationships
Example: for :bugs association in
class Software::Engineer
include DataMapper::Resource has n, :missing_tests has n, :bugs, :through => :missing_tests
end
through is :missing_tests
TODO: document a case when through option is a model and not an association name
@api semipublic
# File lib/dm-core/associations/many_to_many.rb, line 51 def through return @through if defined?(@through) @through = options[:through] if @through.kind_of?(Associations::Relationship) return @through end model = source_model repository_name = source_repository_name relationships = model.relationships(repository_name) name = through_relationship_name @through = relationships[name] || DataMapper.repository(repository_name) do model.has(min..max, name, through_model, one_to_many_options) end @through.child_key @through end
@api semipublic
# File lib/dm-core/associations/many_to_many.rb, line 76 def via return @via if defined?(@via) @via = options[:via] if @via.kind_of?(Associations::Relationship) return @via end name = self.name through = self.through repository_name = through.relative_target_repository_name through_model = through.target_model relationships = through_model.relationships(repository_name) singular_name = DataMapper::Inflector.singularize(name.to_s).to_sym @via = relationships[@via] || relationships[name] || relationships[singular_name] @via ||= if anonymous_through_model? DataMapper.repository(repository_name) do through_model.belongs_to(singular_name, target_model, many_to_one_options) end else raise UnknownRelationshipError, "No relationships named #{name} or #{singular_name} in #{through_model}" end @via.child_key @via end
Generated with the Darkfish Rdoc Generator 2.