Sorting behavior for Rails APIs
Add this line to your application's Gemfile:
gem 'rails_api_sortable', git: 'git@cagit.careerbuilder.com:zwelch/rails_api_sortable.git'
And then execute:
$ bundle
Or install it yourself as:
$ gem install 'rails_api_sortable', git: 'git@cagit.careerbuilder.com:zwelch/rails_api_sortable.git'
RailsApiSortable adds a sort_by
method to ActionController::Base
or ActionController::API
if you're using rails-api.
sort_by
combines sort and sort_direction query string parameters to be used by ActiveRecord.
# /users?sort=name
User.order(sort_by) #=> User.order(name: :asc)
# /users?sort=name&sort_direction=asc
User.order(sort_by) #=> User.order(name: :asc)
# /users?sort=name&sort_direction=desc
User.order(sort_by) #=> User.order(name: :desc)
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Add tests for your feature.
- Add your feature.
- Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request