aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Holder <mxhold@gmail.com>2014-08-30 13:58:18 -0400
committerMax Holder <mxhold@gmail.com>2014-08-30 13:58:18 -0400
commit112cb940aa17a0979b9f6282c07ace2f6a0896c1 (patch)
tree9a0f95749fbcebb1ad711f4ccfcc44f1472f55c7
parent36836fa5e7c084c0dce2818577e6fd0cf815f786 (diff)
downloadrails-112cb940aa17a0979b9f6282c07ace2f6a0896c1.tar.gz
rails-112cb940aa17a0979b9f6282c07ace2f6a0896c1.tar.bz2
rails-112cb940aa17a0979b9f6282c07ace2f6a0896c1.zip
Add SelectManager#distinct_on to set/unset Arel::Nodes::DistinctOn quantifier
-rw-r--r--lib/arel/select_manager.rb9
-rw-r--r--test/test_select_manager.rb21
2 files changed, 30 insertions, 0 deletions
diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb
index 4a652f2c9c..5a05e7e181 100644
--- a/lib/arel/select_manager.rb
+++ b/lib/arel/select_manager.rb
@@ -155,6 +155,15 @@ module Arel
self
end
+ def distinct_on(value)
+ if value
+ @ctx.set_quantifier = Arel::Nodes::DistinctOn.new(value)
+ else
+ @ctx.set_quantifier = nil
+ end
+ self
+ end
+
def order *expr
# FIXME: We SHOULD NOT be converting these to SqlLiteral automatically
@ast.orders.concat expr.map { |x|
diff --git a/test/test_select_manager.rb b/test/test_select_manager.rb
index 3380bbec6f..1ffb56fd9f 100644
--- a/test/test_select_manager.rb
+++ b/test/test_select_manager.rb
@@ -1158,5 +1158,26 @@ module Arel
manager.distinct(false).must_equal manager
end
end
+
+ describe 'distinct_on' do
+ it 'sets the quantifier' do
+ manager = Arel::SelectManager.new Table.engine
+ table = Table.new :users
+
+ manager.distinct_on(table['id'])
+ manager.ast.cores.last.set_quantifier.must_equal Arel::Nodes::DistinctOn.new(table['id'])
+
+ manager.distinct_on(false)
+ manager.ast.cores.last.set_quantifier.must_equal nil
+ end
+
+ it "chains" do
+ manager = Arel::SelectManager.new Table.engine
+ table = Table.new :users
+
+ manager.distinct_on(table['id']).must_equal manager
+ manager.distinct_on(false).must_equal manager
+ end
+ end
end
end