Browse files

Updated Collection to handle belongs_to relationship methods

  • Loading branch information...
1 parent e4c58c7 commit 29054e71feb57f815d919d8f00b0754a8ba3cb5d Dan Kubb committed Nov 26, 2008
Showing with 10 additions and 11 deletions.
  1. +9 −6 lib/dm-core/collection.rb
  2. +1 −5 spec/public/shared/collection_shared_spec.rb
View
15 lib/dm-core/collection.rb
@@ -1112,7 +1112,7 @@ def query_to_hash(query)
def method_missing(method, *args, &block)
if model.respond_to?(method)
delegate_to_model(method, *args, &block)
- elsif relationship = relationships[method]
+ elsif relationship = relationships[method] || relationships[method.to_s.singular.to_sym]
delegate_to_relationship(relationship, *args)
else
super
@@ -1135,24 +1135,27 @@ def delegate_to_model(method, *args, &block)
# @return [DataMapper::Collection] the associated Resources
#
def delegate_to_relationship(relationship, *args)
- klass = model == relationship.child_model ? relationship.parent_model : relationship.child_model
+ target_class = model == relationship.child_model ? relationship.parent_model : relationship.child_model
+ target_key = model == relationship.child_model ? relationship.child_key : relationship.parent_key
# TODO: when self.query includes an offset/limit use it as a
# subquery to scope the results rather than a join
- query = Query.new(repository, klass)
- query.conditions.push(*self.query.conditions)
+ keys = map { |r| r.key }
+ query = Query.new(repository, target_class, target_key.zip(keys.transpose).to_hash)
+
query.update(relationship.query)
+
if args.last.kind_of?(Hash)
query.update(args.pop)
end
query.update(
- :fields => klass.properties(repository.name).defaults,
+ :fields => target_class.properties(repository.name).defaults,
:links => [ relationship ] + self.query.links
)
- klass.all(query)
+ target_class.all(query)
end
end # class Collection
end # module DataMapper
View
6 spec/public/shared/collection_shared_spec.rb
@@ -1170,7 +1170,6 @@
describe 'with a belongs_to relationship method' do
before do
- pending 'TODO: fix for Many To Many' if @articles.class == DataMapper::Associations::ManyToMany::Proxy
@return = @collection = @articles.original
end
@@ -1179,10 +1178,7 @@
end
it 'should return expected Collection' do
- skip = [ DataMapper::Collection, DataMapper::Associations::OneToMany::Proxy ]
- pending_if 'TODO: fix logic to return correct entries', skip.any? { |c| @articles.class == c } do
- @collection.should == []
- end
+ @collection.should == []
end
end

0 comments on commit 29054e7

Please sign in to comment.