reorder method added to ActiveRelation
[#4972 state:committed] Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
parent
a2513aea07
commit
65aa6a7db1
@ -21,6 +21,14 @@ def #{query_method}(*args, &block)
|
|||||||
CEVAL
|
CEVAL
|
||||||
end
|
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)
|
def select(*args)
|
||||||
if block_given?
|
if block_given?
|
||||||
to_a.select { |*block_args| yield(*block_args) }
|
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
|
assert_equal topics(:fourth).title, topics.first.title
|
||||||
end
|
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
|
def test_finding_with_order_and_take
|
||||||
entrants = Entrant.order("id ASC").limit(2).to_a
|
entrants = Entrant.order("id ASC").limit(2).to_a
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user