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

@ -21,6 +21,14 @@ def #{query_method}(*args, &block)
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) }

@ -116,6 +116,12 @@ def test_finding_with_order_concatenated
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