mongoid/mongoid — Issue #1651: One-string order_by
Author: semaperepelitsa
Created: 2012-01-27 05:52:03+00:00
State: open

Criteria#order_by is designed to be similar to SQL's "ORDER BY" but I'm not totally satisfied by it. I have a select field with order statements like "price asc", "created_at desc". I want to call just `order_by(params[:order])` but it does not accept a one-string argument. I've patched it to work like I want. Maybe it is a good idea to take this into core.

```ruby
class Mongoid::Criteria
module OrderableByString
# order_by("foo asc") equals order_by("foo", "asc")
def order_by(*args)
string = args.first
if string.is_a?(String) and args.size == 1
field, direction = string.split(" ")
super(field, direction)
else
super
end
end
alias order order_by
end

include OrderableByString
end
```