Skip to content
Add sorting scopes `asc` and `desc` to your ActiveRecord models.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
gemfiles
lib
spec
.gitignore
.rspec
.travis.yml
Appraisals
Gemfile
LICENSE.txt
README.md
Rakefile
assorted.gemspec

README.md

Assorted

Build Status Code Climate

Assorted is a tiny gem adding some convenience to your ActiveRecord objects with asc and desc scopes to sort your queries. By default these sort by created_at, but you can sort by whichever colum makes sense for your needs. See Usage below for more details.

Installation

Add this line to your application's Gemfile:

gem 'assorted'

And then execute:

$ bundle

Or install it yourself as:

$ gem install assorted

Usage

By default, asc and desc sort by created_at.

SomeModel.asc  # equivalent to SomeModel.order("created_at asc")
SomeModel.desc # equivalent to SomeModel.order("created_at desc")

You can change this per query by passing an alternate column.

SomeModel.asc(:updated_at)  # equivalent to SomeModel.order("updated_at asc")
SomeModel.desc(:updated_at) # equivalent to SomeModel.order("updated_at desc")

To change the default sort column for your entire application, use Assorted.options.

# config/intializers/assorted.rb, for example
Assorted.options[:default_sort_column] = :id

# then, elsewhere in your app
SomeModel.asc  # equivalent to SomeModel.order("id asc")
SomeModel.desc # equivalent to SomeModel.order("id desc")

To change the default sort column for a given class, specify with assorted in your model.

class SomeModel
  assorted default_sort_column: :average_score
end

SomeModel.asc  # equivalent to SomeModel.order("average_score asc")
SomeModel.desc # equivalent to SomeModel.order("average_score desc")

Contributing

  1. Fork it ( https://github.com/dribbble/assorted/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request
You can’t perform that action at this time.