No commit activity in last 3 years
No release in over 3 years
Provides methods to sort mongoid collections.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0

Runtime

 Project Readme

MongoidSortable

Build Status

Installation

Add this line to your application's Gemfile:

gem 'mongoid_sortable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mongoid_sortable

Usage

Model code:

class Book
  include Mongoid::Document
  include MongoidSortable::Sorting
  
  sortable_by :title, :created_at
end

Controller code:

@books = Book.all

View code:

# Generates ?order_by=created_at_asc or ?order_by=created_at_desc
# depending on current @book criteria order
sorting_link(@book, :created_at, 'Book Timestamp')

# Customize link
sorting_link(@book, :created_at, 'Book Timestamp') do |name, path, current_order, is_active|
  # Whatever you want to generate based on block parameters
end

By default, sorting link has html classes sorting_link asc/desc and active if sorting by that field is enabled now.


There is ability to customize queries generation.

sortable_by :domestic_gross, :foreign_gross do |gross_type|
  "movie_details.grosses.#{gross_type}"
end

Using this with domestic_gross_desc, for example, generates desceting sorting by movie_details.grosses.domestic_gross_desc field. It can be used to get rid of long url parameters which reflects your internal document structure.

Also you can set block that will be used to generate sorting field for all parameters that don't match to explicit sortable_by

default_sorting do |param|
  "movie_details.#{param}"
end

So, for example, sorting(order_by: ticket_cost_asc) will generate query with ascenting movie_details.ticket_cost sorting.

Of course, you can add more complicated logic inside of sortable_by and default_sorting block, all that you need is to return string with field name.

Contributing

  1. Fork it
  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 new Pull Request