Skip to content

Add sorting scopes `asc` and `desc` to your ActiveRecord models.

License

Notifications You must be signed in to change notification settings

dribbble/assorted

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Add sorting scopes `asc` and `desc` to your ActiveRecord models.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages