Created
May 14, 2011 02:34
-
-
Save AquaGeek/971779 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #6426
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 1b036c92ff9791a63b6ba51a97f4f223f7456f4d Mon Sep 17 00:00:00 2001 | |
From: Igor Alexandrov <igor.alexandrov@gmail.com> | |
Date: Sun, 13 Feb 2011 17:59:12 +0300 | |
Subject: [PATCH] Allow array as order param for postgresql | |
--- | |
activerecord/lib/active_record/base.rb | 6 +++++- | |
.../connection_adapters/postgresql_adapter.rb | 19 ++++++++++++++----- | |
2 files changed, 19 insertions(+), 6 deletions(-) | |
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb | |
index ac82cc1..376ea1e 100755 | |
--- a/activerecord/lib/active_record/base.rb | |
+++ b/activerecord/lib/active_record/base.rb | |
@@ -1769,7 +1769,11 @@ module ActiveRecord #:nodoc: | |
scope = scope(:find) if :auto == scope | |
scoped_order = scope[:order] if scope | |
if order | |
- sql << " ORDER BY #{order}" | |
+ if order.is_a?(Array) | |
+ sql << " ORDER BY #{order.join(', ')}" | |
+ else | |
+ sql << " ORDER BY #{order}" | |
+ end | |
if scoped_order && scoped_order != order | |
sql << ", #{scoped_order}" | |
end | |
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb | |
index a348318..e3986d7 100644 | |
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb | |
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb | |
@@ -890,10 +890,14 @@ module ActiveRecord | |
# Construct a clean list of column names from the ORDER BY clause, removing | |
# any ASC/DESC modifiers | |
- order_columns = order_by.split(',').collect { |s| s.split.first } | |
- order_columns.delete_if &:blank? | |
- order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" } | |
- | |
+ if order_by.is_a?(Array) | |
+ order_columns = order_by.zip((0...order_by.size).to_a).map { |s,i| "#{s.gsub(/ASC|DESC/, '').strip} AS alias_#{i}" } | |
+ else | |
+ order_columns = order_by.split(',').collect { |s| s.split.first } | |
+ order_columns.delete_if &:blank? | |
+ order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" } | |
+ end | |
+ | |
# Return a DISTINCT ON() clause that's distinct on the columns we want but includes | |
# all the required columns for the ORDER BY to work properly. | |
sql = "DISTINCT ON (#{columns}) #{columns}, " | |
@@ -907,7 +911,12 @@ module ActiveRecord | |
def add_order_by_for_association_limiting!(sql, options) #:nodoc: | |
return sql if options[:order].blank? | |
- order = options[:order].split(',').collect { |s| s.strip }.reject(&:blank?) | |
+ if options[:order].is_a?(Array) | |
+ order = options[:order].clone | |
+ else | |
+ order = options[:order].split(',').collect { |s| s.strip }.reject(&:blank?) | |
+ end | |
+ | |
order.map! { |s| 'DESC' if s =~ /\bdesc$/i } | |
order = order.zip((0...order.size).to_a).map { |s,i| "id_list.alias_#{i} #{s}" }.join(', ') | |
-- | |
1.7.3.2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment