diff options
author | Santiago Pastorino <santiago@wyeworks.com> | 2010-06-25 19:34:51 -0300 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-06-26 00:59:12 +0200 |
commit | 65aa6a7db16b7387d5a3bf1ad7f5a602804a2f21 (patch) | |
tree | a66aa73a99c67b4828e861a176e5fd7d6b00158f /activerecord | |
parent | a2513aea07255877b66b846fcf7ebb35629199df (diff) | |
download | rails-65aa6a7db16b7387d5a3bf1ad7f5a602804a2f21.tar.gz rails-65aa6a7db16b7387d5a3bf1ad7f5a602804a2f21.tar.bz2 rails-65aa6a7db16b7387d5a3bf1ad7f5a602804a2f21.zip |
reorder method added to ActiveRelation
[#4972 state:committed]
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 6 |
2 files changed, 14 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 4dbb30c777..f7c4c7991b 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -21,6 +21,14 @@ module ActiveRecord CEVAL end + def reorder(*args, &block) + new_relation = clone + new_relation.send(:apply_modules, Module.new(&block)) if block_given? + value = Array.wrap(args.flatten).reject {|x| x.blank? } + new_relation.order_values = value if value.present? + new_relation + end + def select(*args) if block_given? to_a.select { |*block_args| yield(*block_args) } diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 821b4585a7..d37f7bd09c 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -116,6 +116,12 @@ class RelationTest < ActiveRecord::TestCase assert_equal topics(:fourth).title, topics.first.title end + def test_finding_with_reorder + topics = Topic.order('author_name').order('title').reorder('id') + assert_equal 4, topics.to_a.size + assert_equal topics(:first).title, topics.first.title + end + def test_finding_with_order_and_take entrants = Entrant.order("id ASC").limit(2).to_a |