Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Updated OneToMany::Relationship#inverse to match using child_key too
Browse files Browse the repository at this point in the history
  • Loading branch information
dkubb committed Sep 2, 2009
1 parent 06450cc commit 380ad05
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
8 changes: 8 additions & 0 deletions lib/dm-core/associations/one_to_many.rb
Expand Up @@ -166,6 +166,14 @@ def inverse_class
def inverse_name
super || Extlib::Inflection.underscore(Extlib::Inflection.demodulize(source_model.name)).to_sym
end

# TODO: document
# @api private
def child_properties
super || parent_key.map do |parent_property|
"#{inverse_name}_#{parent_property.name}".to_sym
end
end
end # class Relationship

class Collection < DataMapper::Collection
Expand Down
14 changes: 8 additions & 6 deletions lib/dm-core/associations/relationship.rb
Expand Up @@ -507,7 +507,11 @@ def inverse?(other)
# TODO: document
# @api private
def inverse_name
options[:inverse]
if options[:inverse].kind_of?(Relationship)
options[:inverse].name
else
options[:inverse]
end
end

# TODO: document
Expand Down Expand Up @@ -567,12 +571,10 @@ def cmp_model?(other, operator, type)
# TODO: document
# @api private
def cmp_key?(other, operator, type)
method = "#{type}_properties"

self_key = send(method)
other_key = other.send(method)
property_method = "#{type}_properties"

return true if self_key.nil? || other_key.nil?
self_key = send(property_method)
other_key = other.send(property_method)

self_key.send(operator, other_key)
end
Expand Down
4 changes: 4 additions & 0 deletions spec/semipublic/associations/relationship_spec.rb
Expand Up @@ -88,6 +88,10 @@ def n

describe 'with no matching relationship', 'from the parent side' do
before :all do
# added to force OneToMany::Relationship#inverse to consider the
# child_key differences
@comment_model.belongs_to(:other_article, @article_model, :child_key => [ :other_article_id ])

@relationship = @article_model.has(n, :comments)

@inverse = @relationship.inverse
Expand Down

0 comments on commit 380ad05

Please sign in to comment.