From 9b7da5fc03164d5c05802f6beaabfe5a7176e255 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 16 Nov 2008 17:38:19 -0600 Subject: [PATCH] allow spaces after :order option in associations --- activerecord/lib/active_record/base.rb | 10 +++++----- .../associations/has_many_associations_test.rb | 8 ++++++++ activerecord/test/models/company.rb | 1 + 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 68f44ef..432a9ff 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1475,11 +1475,11 @@ module ActiveRecord #:nodoc: def reverse_sql_order(order_query) reversed_query = order_query.split(/,/).each { |s| - if s.match(/\s(asc|ASC)$/) - s.gsub!(/\s(asc|ASC)$/, ' DESC') - elsif s.match(/\s(desc|DESC)$/) - s.gsub!(/\s(desc|DESC)$/, ' ASC') - elsif !s.match(/\s(asc|ASC|desc|DESC)$/) + if s.match(/\s(asc|ASC)\s*$/) + s.gsub!(/\s(asc|ASC)\s*$/, ' DESC') + elsif s.match(/\s(desc|DESC)\s*$/) + s.gsub!(/\s(desc|DESC)\s*$/, ' ASC') + elsif !s.match(/\s(asc|ASC|desc|DESC)\s*$/) s.concat(' DESC') end }.join(',') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 59784e1..87e2b51 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1060,6 +1060,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert firm.clients.loaded? end + def test_calling_last_on_association_with_order_options_ending_in_space_should_not_fail + firm = companies(:first_firm) + firm.clients_sorted_asc_with_space.first + firm.clients_sorted_asc_with_space.last + assert true + end + def test_joins_with_namespaced_model_should_use_correct_type old = ActiveRecord::Base.store_full_sti_class ActiveRecord::Base.store_full_sti_class = true @@ -1078,6 +1085,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase ActiveRecord::Base.store_full_sti_class = old end + uses_mocha 'mocking Comment.transaction' do def test_association_proxy_transaction_method_starts_transaction_in_association_class Comment.expects(:transaction) diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index 0e3fafa..3754353 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -38,6 +38,7 @@ class Firm < Company "SELECT COUNT(*) FROM companies WHERE firm_id = 1 " + "AND (#{QUOTED_TYPE} = 'Client' OR #{QUOTED_TYPE} = 'SpecialClient' OR #{QUOTED_TYPE} = 'VerySpecialClient' )" has_many :clients_sorted_desc, :class_name => "Client", :order => "id DESC" + has_many :clients_sorted_asc_with_space,:class_name=>"Client",:order=>" id ASC " has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id" has_many :unvalidated_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :validate => false has_many :dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :destroy -- 1.5.4.3