diff options
author | Neeraj Singh <neerajdotname@gmail.com> | 2010-09-09 16:49:44 -0400 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-09-27 10:49:49 -0700 |
commit | fbd1d306b95cc2efb6422e12d26d5818a3a42343 (patch) | |
tree | 50187de29ec641831a05715ace1f1ac5cdc5ad5c /activerecord/lib/active_record | |
parent | e3d6434dd9ef052ce703b36f968eb9f6fe1f0a11 (diff) | |
download | rails-fbd1d306b95cc2efb6422e12d26d5818a3a42343.tar.gz rails-fbd1d306b95cc2efb6422e12d26d5818a3a42343.tar.bz2 rails-fbd1d306b95cc2efb6422e12d26d5818a3a42343.zip |
Three performance improvements:
* for simple cases like User.last and User.order('name desc').last no need to perform Array#join operation.
* Instead of performing String#blank? do Array#empty?
* no need to create variable relation
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 6a33edeb97..c97b1a24d2 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -135,14 +135,13 @@ module ActiveRecord end def reverse_order - order_clause = arel.order_clauses.join(', ') - relation = except(:order) + order_clause = arel.order_clauses - order = order_clause.blank? ? + order = order_clause.empty? ? "#{@klass.table_name}.#{@klass.primary_key} DESC" : - reverse_sql_order(order_clause) + reverse_sql_order(order_clause).join(', ') - relation.order(Arel::SqlLiteral.new(order)) + except(:order).order(Arel::SqlLiteral.new(order)) end def arel @@ -283,15 +282,15 @@ module ActiveRecord end def reverse_sql_order(order_query) - order_query.split(',').each { |s| + order_query.join(', ').split(',').collect { |s| if s.match(/\s(asc|ASC)$/) - s.gsub!(/\s(asc|ASC)$/, ' DESC') + s.gsub(/\s(asc|ASC)$/, ' DESC') elsif s.match(/\s(desc|DESC)$/) - s.gsub!(/\s(desc|DESC)$/, ' ASC') + s.gsub(/\s(desc|DESC)$/, ' ASC') else - s.concat(' DESC') + s + ' DESC' end - }.join(',') + } end def array_of_strings?(o) |