Project

assorted

0.01
No commit activity in last 3 years
No release in over 3 years
Add sorting scopes `asc` and `desc` to your ActiveRecord models.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0
~> 1.6
~> 10.0
~> 3.1
~> 1.3

Runtime

 Project Readme

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