Permalink
Browse files

infer references from Relation#order

  • Loading branch information...
1 parent a84a20e commit 46ea4442f3abc33d15e03487bae1c80346eab49a @jonleighton jonleighton committed Jan 14, 2012
@@ -106,8 +106,14 @@ def group(*args)
def order(*args)
return self if args.blank?
+ args = args.flatten
+ references = args.reject { |arg| Arel::Node === arg }
+ .map { |arg| arg =~ /^([a-zA-Z]\w*)\.(\w+)/ && $1 }
+ .compact
+
relation = clone
- relation.order_values += args.flatten
+ relation = relation.references(references) if references.any?
+ relation.order_values += args
relation
end
@@ -1176,17 +1176,37 @@ def test_references_doesnt_trigger_eager_loading_if_reference_not_included
assert !scope.eager_loading?
end
- def test_automatically_added_references
+ def test_automatically_added_where_references
scope = Post.where(:comments => { :body => "Bla" })
assert_equal ['comments'], scope.references_values
scope = Post.where('comments.body' => 'Bla')
assert_equal ['comments'], scope.references_values
+ end
+ def test_automatically_added_having_references
scope = Post.having(:comments => { :body => "Bla" })
assert_equal ['comments'], scope.references_values
scope = Post.having('comments.body' => 'Bla')
assert_equal ['comments'], scope.references_values
end
+
+ def test_automatically_added_order_references
+ scope = Post.order('comments.body')
+ assert_equal ['comments'], scope.references_values
+
+ scope = Post.order('comments.body', 'yaks.body')
+ assert_equal ['comments', 'yaks'], scope.references_values
+
+ # Don't infer yaks, let's not go down that road again...
+ scope = Post.order('comments.body, yaks.body')
+ assert_equal ['comments'], scope.references_values
+
+ scope = Post.order('comments.body asc')
+ assert_equal ['comments'], scope.references_values
+
+ scope = Post.order('foo(comments.body)')
+ assert_equal [], scope.references_values
+ end
end

0 comments on commit 46ea444

Please sign in to comment.