https://github.com/bsm/sortable-by
https://github.com/bsm/sortable-by
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/bsm/sortable-by
- Owner: bsm
- License: mit
- Created: 2015-08-10T16:49:11.000Z (almost 11 years ago)
- Default Branch: main
- Last Pushed: 2024-08-02T01:42:03.000Z (almost 2 years ago)
- Last Synced: 2025-03-22T19:02:18.609Z (about 1 year ago)
- Language: Ruby
- Size: 43.9 KB
- Stars: 3
- Watchers: 2
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Sortable By
[](https://github.com/bsm/sortable-by/actions/workflows/test.yml)
[](https://opensource.org/licenses/MIT)
ActiveRecord plugin to parse the sort order from a query parameter, match against a white-list and generate a scope. Useful for [JSON-API][jsonapi] compatibility.
[jsonapi]: http://jsonapi.org/format/#fetching-sorting
## Installation
Add `gem 'sortable-by'` to your Gemfile.
## Usage
Simple:
```ruby
class Post < ActiveRecord::Base
sortable_by :title, :id
end
Post.sorted_by('title') # => ORDER BY LOWER(posts.title) ASC
Post.sorted_by('-title') # => ORDER BY LOWER(posts.title) DESC
Post.sorted_by('title,-id') # => ORDER BY LOWER(posts.title) ASC, id DESC
Post.sorted_by(['title', '-id']) # => ORDER BY LOWER(posts.title) ASC, id DESC
Post.sorted_by('bad,title') # => ORDER BY LOWER(posts.title) ASC
Post.sorted_by(nil) # => ORDER BY LOWER(posts.title) ASC
```
Case-sensitive:
```ruby
class Post < ActiveRecord::Base
sortable_by do |x|
x.field :title, case_sensitive: true
x.field :id
end
end
Post.sorted_by('title') # => ORDER BY posts.title ASC
```
With custom default:
```ruby
class Post < ActiveRecord::Base
sortable_by :id, :topic, :created_at, default: 'topic,-created_at'
end
Post.sorted_by(nil) # => ORDER BY LOWER(posts.topic) ASC, posts.created_at DESC
```
Composition:
```ruby
class App < ActiveRecord::Base
sortable_by :name, default: '-version' do |x|
x.field :version, as: %i[major minor patch]]
end
end
App.sorted_by('version') # => ORDER BY apps.major ASC, apps.minor ASC, apps.patch ASC
App.sorted_by(nil) # => ORDER BY apps.major DESC, apps.minor DESC, apps.patch DESC
```
Associations (custom scope):
```ruby
class Product < ActiveRecord::Base
belongs_to :shop
sortable_by do |x|
x.field :shop, as: Shop.arel_table[:name], scope: -> { joins(:shop) }
end
end
```