1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

reorder method added to ActiveRelation

[#4972 state:committed]

Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
Santiago Pastorino 2010-06-25 19:34:51 -03:00 committed by José Valim
parent a2513aea07
commit 65aa6a7db1
2 changed files with 14 additions and 0 deletions

View file

@ -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) }

View file

@ -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