https://github.com/theopencms/simple_sort
Gem helps provide simple sorting on AR collections
https://github.com/theopencms/simple_sort
Last synced: 11 months ago
JSON representation
Gem helps provide simple sorting on AR collections
- Host: GitHub
- URL: https://github.com/theopencms/simple_sort
- Owner: TheOpenCMS
- License: mit
- Created: 2017-03-12T14:16:21.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-10-01T15:27:51.000Z (over 8 years ago)
- Last Synced: 2025-07-05T15:12:10.511Z (11 months ago)
- Language: Ruby
- Size: 40 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
```ruby
This gem is a part of TheOpenCMS project. https://github.com/TheOpenCMS
```
# SimpleSort
Gem provides simple sort on Active Record collections.
# How it works
Add `SimpleSort` functionality to your Model
```ruby
class Product < ApplicationRecord
include ::SimpleSort::Base
end
```
Use method `simple_sort` with your Model in a controller like this:
```ruby
class ProductsController < ApplicationController
def index
@products = Product.simple_sort(params)
end
end
```
* Use `simple_sort_url` helper in your views to provide sorting for required field
* Use `reset_simple_sort` helper to reset current sort params
```ruby
= link_to 'Reset sort params', url_for(reset_simple_sort)
= link_to 'Title ↕', simple_sort_url(:title)
= link_to 'Created At ↕', simple_sort_url(:crated_at)
= link_to 'Updated At ↕', simple_sort_url(:updated_at)
```
# Bonus methods for Models
Also gem provides some dead-simple but helpful methods
* `max2min` for descending order `DESC` (1000, 102, 15, 3)
* `min2max` for ascending order `ASC` (3, 15, 102, 1000)
* `random` random record from a database (`mysql`, `psql`)
```ruby
class ProductsController < ApplicationController
def index
@products = Product.max2min(:created_at)
@articles = Article.min2max(:title)
@banners = Banner.random.limit(3)
end
end
```
### How to test
```
git clone https://github.com/TheOpenCMS/simple_sort.git
cd simple_sort/tests/dummy_app/
bundle
RAILS_ENV=test bundle exec rake db:setup
bundle exec rspec
```
### Contributors
* Author: [@the-teacher](https://github.com/the-teacher)
* [@devico](https://github.com/devico)
### MIT License
* [MIT License](./LICENSE.txt)